Commit
·
c038b95
1
Parent(s):
b50eb7c
temp .2
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import time
|
|
|
4 |
from langchain.document_loaders import OnlinePDFLoader
|
5 |
from langchain.text_splitter import CharacterTextSplitter
|
6 |
from langchain.llms import OpenAI
|
@@ -10,13 +11,16 @@ from langchain.chains import ConversationalRetrievalChain
|
|
10 |
|
11 |
os.environ['OPENAI_API_KEY'] = os.getenv("Your_API_Key")
|
12 |
|
|
|
|
|
|
|
13 |
def loading_pdf():
|
14 |
-
return "Working the upload. Also, pondering the usefulness of sporks..."
|
15 |
|
16 |
def pdf_changes(pdf_doc):
|
17 |
loader = OnlinePDFLoader(pdf_doc.name)
|
18 |
documents = loader.load()
|
19 |
-
text_splitter = CharacterTextSplitter(chunk_size=
|
20 |
texts = text_splitter.split_documents(documents)
|
21 |
embeddings = OpenAIEmbeddings()
|
22 |
db = Chroma.from_documents(texts, embeddings)
|
@@ -34,18 +38,16 @@ def clear_data():
|
|
34 |
return "Data cleared"
|
35 |
|
36 |
def add_text(history, text):
|
|
|
|
|
37 |
history = history + [(text, None)]
|
38 |
return history, ""
|
39 |
|
40 |
def bot(history):
|
41 |
response = infer(history[-1][0], history)
|
42 |
-
formatted_response = "**
|
43 |
-
history[-1][1] =
|
44 |
-
|
45 |
-
for character in formatted_response:
|
46 |
-
history[-1][1] += character
|
47 |
-
time.sleep(0.05)
|
48 |
-
yield history
|
49 |
|
50 |
def infer(question, history):
|
51 |
res = []
|
@@ -58,6 +60,18 @@ def infer(question, history):
|
|
58 |
result = qa({"question": query, "chat_history": chat_history})
|
59 |
return result["answer"]
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
css = """
|
62 |
#col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
|
63 |
"""
|
@@ -66,8 +80,8 @@ title = """
|
|
66 |
<div style="text-align: center;max-width: 700px;">
|
67 |
<h1>CauseWriter Chat with PDF • OpenAI</h1>
|
68 |
<p style="text-align: center;">Upload a .PDF from your computer, click the "Load PDF to LangChain" button, <br />
|
69 |
-
when everything is ready, you can start asking questions about the pdf
|
70 |
-
This version is set to store chat history
|
71 |
</div>
|
72 |
"""
|
73 |
|
@@ -96,4 +110,4 @@ with gr.Blocks(css=css) as demo:
|
|
96 |
bot, chatbot, chatbot
|
97 |
)
|
98 |
|
99 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import time
|
4 |
+
import threading
|
5 |
from langchain.document_loaders import OnlinePDFLoader
|
6 |
from langchain.text_splitter import CharacterTextSplitter
|
7 |
from langchain.llms import OpenAI
|
|
|
11 |
|
12 |
os.environ['OPENAI_API_KEY'] = os.getenv("Your_API_Key")
|
13 |
|
14 |
+
# Global variable for tracking last interaction time
|
15 |
+
last_interaction_time = 0
|
16 |
+
|
17 |
def loading_pdf():
|
18 |
+
return "Working on the upload. Also, pondering the usefulness of sporks..."
|
19 |
|
20 |
def pdf_changes(pdf_doc):
|
21 |
loader = OnlinePDFLoader(pdf_doc.name)
|
22 |
documents = loader.load()
|
23 |
+
text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
24 |
texts = text_splitter.split_documents(documents)
|
25 |
embeddings = OpenAIEmbeddings()
|
26 |
db = Chroma.from_documents(texts, embeddings)
|
|
|
38 |
return "Data cleared"
|
39 |
|
40 |
def add_text(history, text):
|
41 |
+
global last_interaction_time
|
42 |
+
last_interaction_time = time.time()
|
43 |
history = history + [(text, None)]
|
44 |
return history, ""
|
45 |
|
46 |
def bot(history):
|
47 |
response = infer(history[-1][0], history)
|
48 |
+
formatted_response = "**Bot:** \n" + ' \n'.join(response.split('. '))
|
49 |
+
history[-1][1] = formatted_response
|
50 |
+
return history
|
|
|
|
|
|
|
|
|
51 |
|
52 |
def infer(question, history):
|
53 |
res = []
|
|
|
60 |
result = qa({"question": query, "chat_history": chat_history})
|
61 |
return result["answer"]
|
62 |
|
63 |
+
def auto_clear_data():
|
64 |
+
global qa, last_interaction_time
|
65 |
+
if time.time() - last_interaction_time > 600:
|
66 |
+
qa = None
|
67 |
+
|
68 |
+
def periodic_clear():
|
69 |
+
while True:
|
70 |
+
auto_clear_data()
|
71 |
+
time.sleep(60)
|
72 |
+
|
73 |
+
threading.Thread(target=periodic_clear).start()
|
74 |
+
|
75 |
css = """
|
76 |
#col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
|
77 |
"""
|
|
|
80 |
<div style="text-align: center;max-width: 700px;">
|
81 |
<h1>CauseWriter Chat with PDF • OpenAI</h1>
|
82 |
<p style="text-align: center;">Upload a .PDF from your computer, click the "Load PDF to LangChain" button, <br />
|
83 |
+
when everything is ready, you can start asking questions about the pdf. <br />
|
84 |
+
This version is set to store chat history and uses OpenAI as LLM.</p>
|
85 |
</div>
|
86 |
"""
|
87 |
|
|
|
110 |
bot, chatbot, chatbot
|
111 |
)
|
112 |
|
113 |
+
demo.launch()
|