From 0086d6f53f44759c2885c02a3eb959b37a2d084e Mon Sep 17 00:00:00 2001 From: Eyantra698Sumanto Date: Tue, 18 Apr 2023 13:57:42 +0530 Subject: [PATCH 1/4] Task 4: Connect Frontend with the Backend --- app.py | 68 +++++++++++++++++++++++++++++++++++++++++++----- requirements.txt | 2 ++ 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/app.py b/app.py index 8f94a85..a1e6a56 100644 --- a/app.py +++ b/app.py @@ -1,12 +1,63 @@ #make a stremlit aplication to import a pdf file and convert it to text and display text in another output window and an option to download file generated import streamlit as st import os - +import multiprocessing +from functools import wraps from langchain.llms import OpenAI from langchain.document_loaders import TextLoader from langchain.document_loaders import PyPDFLoader from langchain.prompts import PromptTemplate + +def parametrized(dec): + def layer(*args, **kwargs): + def repl(f): + return dec(f, *args, **kwargs) + return repl + return layer +def function_runner(*args, **kwargs): + """Used as a wrapper function to handle + returning results on the multiprocessing side""" + + send_end = kwargs.pop("__send_end") + function = kwargs.pop("__function") + try: + result = function(*args, **kwargs) + except Exception as e: + send_end.send(e) + return + send_end.send(result) + +@parametrized +def run_with_timer(func, max_execution_time): + @wraps(func) + def wrapper(*args, **kwargs): + recv_end, send_end = multiprocessing.Pipe(False) + kwargs["__send_end"] = send_end + kwargs["__function"] = func + + ## PART 2 + p = multiprocessing.Process(target=function_runner, args=args, kwargs=kwargs) + p.start() + p.join(max_execution_time) + if p.is_alive(): + p.terminate() + p.join() + #raise TimeExceededException("OpenAI taking too long to respond") + st.error("OpenAI taking too long to respond. Refresh and Restart again.") + result = recv_end.recv() + + if isinstance(result, Exception): + #raise result + st.error(result) + else: + st.error(result) + + return result + + return wrapper + +@run_with_timer(max_execution_time=10) def op_readme(): llm = OpenAI() @@ -26,12 +77,13 @@ def op_readme(): ) from langchain.chains import LLMChain - chain = LLMChain(llm=llm, prompt=prompt) - + + chain = LLMChain(llm=llm, prompt=prompt, max_iterations=2) # Run the chain only specifying the input variable. j=0 - - os.remove("result.md") + file="result.md" + if os.path.exists(file): + os.remove("result.md") with open('result.md', 'w') as f: for i in range(0,len(full_content),2000): @@ -43,13 +95,14 @@ def op_readme(): return "Success" +#signal.signal(signal.SIGALRM, timeout_handler) st.title("Slides Summarizer \n(PDF to Markdown)") os.environ["OPENAI_API_KEY"] = st.text_input("Enter your OpenAI API Key") - file = st.file_uploader("Upload Your Slides", type="pdf") if file: if os.environ["OPENAI_API_KEY"] == "": + print(openai.api_key ) st.write("Please enter your API key") else: with open("slides.pdf", "wb") as f: @@ -66,3 +119,6 @@ def op_readme(): mime='text/markdown' ) + + + diff --git a/requirements.txt b/requirements.txt index e92d54d..dbd24b1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,5 @@ pandas requests paytmchecksum langchain +openai +requests From 9234b40c9eae4a0b47c22315fdc6e1f31c6d925d Mon Sep 17 00:00:00 2001 From: Eyantra698Sumanto Date: Tue, 18 Apr 2023 16:34:41 +0530 Subject: [PATCH 2/4] Internet Check Added --- app.py | 27 +++++++++++++++++++++------ requirements.txt | 1 + 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/app.py b/app.py index a1e6a56..89b7496 100644 --- a/app.py +++ b/app.py @@ -8,6 +8,15 @@ from langchain.document_loaders import PyPDFLoader from langchain.prompts import PromptTemplate +import urllib.request +def connect(host='http://google.com'): + try: + urllib.request.urlopen(host) #Python 3.x + return True + except: + return False + + def parametrized(dec): def layer(*args, **kwargs): @@ -50,14 +59,13 @@ def wrapper(*args, **kwargs): if isinstance(result, Exception): #raise result st.error(result) - else: - st.error(result) + return result return wrapper -@run_with_timer(max_execution_time=10) +@run_with_timer(max_execution_time=20) def op_readme(): llm = OpenAI() @@ -78,7 +86,7 @@ def op_readme(): from langchain.chains import LLMChain - chain = LLMChain(llm=llm, prompt=prompt, max_iterations=2) + chain = LLMChain(llm=llm, prompt=prompt) # Run the chain only specifying the input variable. j=0 file="result.md" @@ -95,6 +103,8 @@ def op_readme(): return "Success" + + #signal.signal(signal.SIGALRM, timeout_handler) st.title("Slides Summarizer \n(PDF to Markdown)") os.environ["OPENAI_API_KEY"] = st.text_input("Enter your OpenAI API Key") @@ -102,12 +112,17 @@ def op_readme(): file = st.file_uploader("Upload Your Slides", type="pdf") if file: if os.environ["OPENAI_API_KEY"] == "": - print(openai.api_key ) - st.write("Please enter your API key") + #print(openai.api_key ) + st.error("Please enter your API key") else: with open("slides.pdf", "wb") as f: f.write(file.getbuffer()) st.success("Saved File Successfully") + if(not connect()): + st.error("No Internet!") + while(not connect()): + pass + st.success("Connected to Internet Successfully") pdf = op_readme() if pdf=="Success": st.write("File converted successfully. Download the file to view the summary.") diff --git a/requirements.txt b/requirements.txt index dbd24b1..3a46e9d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,4 @@ paytmchecksum langchain openai requests +urllib From 5d2f0c4248232a6a141ae4e11e05a1bdfa1822c7 Mon Sep 17 00:00:00 2001 From: Sumanto Kar Date: Tue, 18 Apr 2023 16:39:40 +0530 Subject: [PATCH 3/4] Update README.md --- README.md | Bin 34 -> 193 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/README.md b/README.md index d74515e102358d59137755bac45f8153eb90f1b2..e4669482094a8774cf8978945f2199dacc1a683a 100644 GIT binary patch literal 193 zcmaKlK?=e!5JmTe-r+-T;0=NZc4LvAAZA7y3^S5UbGa@&l2xWp1!IBD2rD*vt&rcRV;ku~>%#F)$ cDXttJNOw$PT!am&BD9clxb?mkh}t-O05PFIod5s; literal 34 mcmezWPnki1A($bDA(J76A(f$+!HB_+A)lcH%;sg_VgLY`mIn6# From abd236373c52c99d420a2c2606d944d2d9cf8dd1 Mon Sep 17 00:00:00 2001 From: Sumanto Kar Date: Tue, 18 Apr 2023 16:45:39 +0530 Subject: [PATCH 4/4] Update README.md --- README.md | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e466948..125cd6a 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,15 @@ # Slides2Notes - ## Added Code to Solve the Handle the Authentication Error - ## Added Code to Solve the OpenAI Quota Over Error - ## Added Code to check if Internet is available or not + ## Handle the Authentication Error + ![image](https://user-images.githubusercontent.com/58599984/232759618-0cb362de-150a-4f69-8141-58c01767a000.png) + + ## Solve the OpenAI Quota Over Error + ![image](https://user-images.githubusercontent.com/58599984/232760022-43c67780-ca71-4396-b794-355d45b97b64.png) + ## Check if Internet is available or not + ![image](https://user-images.githubusercontent.com/58599984/232760606-cbe44635-e310-4904-ac25-30644eda034b.png) + + ## Solve if OpenAI gets hanged in between +![image](https://user-images.githubusercontent.com/58599984/232760316-9cb37862-6fe6-4f00-b40c-b65658080575.png) + + +