diff --git a/README.md b/README.md index d74515e..125cd6a 100644 Binary files a/README.md and b/README.md differ diff --git a/app.py b/app.py index 8f94a85..89b7496 100644 --- a/app.py +++ b/app.py @@ -1,12 +1,71 @@ #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 +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): + 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) + + + return result + + return wrapper + +@run_with_timer(max_execution_time=20) def op_readme(): llm = OpenAI() @@ -26,12 +85,13 @@ def op_readme(): ) from langchain.chains import LLMChain + chain = LLMChain(llm=llm, prompt=prompt) - # 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,18 +103,26 @@ 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"] == "": - 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.") @@ -66,3 +134,6 @@ def op_readme(): mime='text/markdown' ) + + + diff --git a/requirements.txt b/requirements.txt index e92d54d..3a46e9d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,6 @@ pandas requests paytmchecksum langchain +openai +requests +urllib