Commit
·
e9619d2
1
Parent(s):
837d48f
clear data DB
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,42 +11,46 @@ 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)
|
23 |
retriever = db.as_retriever()
|
24 |
global qa
|
25 |
qa = ConversationalRetrievalChain.from_llm(
|
26 |
-
llm=OpenAI(temperature=0.5),
|
27 |
retriever=retriever,
|
28 |
return_source_documents=False)
|
29 |
return "Ready"
|
30 |
|
31 |
def clear_data():
|
32 |
-
global qa
|
33 |
qa = None
|
|
|
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 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
history[-1][1] += character
|
47 |
-
time.sleep(0.05)
|
48 |
-
yield history
|
49 |
|
50 |
def infer(question, history):
|
51 |
res = []
|
@@ -55,9 +60,22 @@ def infer(question, history):
|
|
55 |
|
56 |
chat_history = res
|
57 |
query = question
|
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 +84,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 |
|
@@ -82,7 +100,7 @@ with gr.Blocks(css=css) as demo:
|
|
82 |
load_pdf = gr.Button("Convert PDF to Magic AI language")
|
83 |
clear_btn = gr.Button("Clear Data")
|
84 |
|
85 |
-
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=
|
86 |
question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter")
|
87 |
submit_btn = gr.Button("Send Message")
|
88 |
|
|
|
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=1000, chunk_overlap=100)
|
24 |
texts = text_splitter.split_documents(documents)
|
25 |
embeddings = OpenAIEmbeddings()
|
26 |
+
global db
|
27 |
db = Chroma.from_documents(texts, embeddings)
|
28 |
retriever = db.as_retriever()
|
29 |
global qa
|
30 |
qa = ConversationalRetrievalChain.from_llm(
|
31 |
+
llm=OpenAI(temperature=0.2, model_name="gpt-3.5-turbo", max_tokens=-1, n=2),
|
32 |
retriever=retriever,
|
33 |
return_source_documents=False)
|
34 |
return "Ready"
|
35 |
|
36 |
def clear_data():
|
37 |
+
global qa, db
|
38 |
qa = None
|
39 |
+
db = None
|
40 |
return "Data cleared"
|
41 |
|
42 |
def add_text(history, text):
|
43 |
+
global last_interaction_time
|
44 |
+
last_interaction_time = time.time()
|
45 |
history = history + [(text, None)]
|
46 |
return history, ""
|
47 |
|
48 |
def bot(history):
|
49 |
response = infer(history[-1][0], history)
|
50 |
+
sentences = ' \n'.join(response.split('. '))
|
51 |
+
formatted_response = f"**Bot:**\n\n{sentences}"
|
52 |
+
history[-1][1] = formatted_response
|
53 |
+
return history
|
|
|
|
|
|
|
54 |
|
55 |
def infer(question, history):
|
56 |
res = []
|
|
|
60 |
|
61 |
chat_history = res
|
62 |
query = question
|
63 |
+
result = qa({"question": query, "chat_history": chat_history, "system:":"This is a world-class summarizing AI, be helpful."})
|
64 |
return result["answer"]
|
65 |
|
66 |
+
def auto_clear_data():
|
67 |
+
global qa, db, last_interaction_time
|
68 |
+
if time.time() - last_interaction_time > 1000:
|
69 |
+
qa = None
|
70 |
+
db = None
|
71 |
+
|
72 |
+
def periodic_clear():
|
73 |
+
while True:
|
74 |
+
auto_clear_data()
|
75 |
+
time.sleep(600)
|
76 |
+
|
77 |
+
threading.Thread(target=periodic_clear).start()
|
78 |
+
|
79 |
css = """
|
80 |
#col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
|
81 |
"""
|
|
|
84 |
<div style="text-align: center;max-width: 700px;">
|
85 |
<h1>CauseWriter Chat with PDF • OpenAI</h1>
|
86 |
<p style="text-align: center;">Upload a .PDF from your computer, click the "Load PDF to LangChain" button, <br />
|
87 |
+
when everything is ready, you can start asking questions about the pdf. <br />
|
88 |
+
This version is set to store chat history and uses OpenAI as LLM.</p>
|
89 |
</div>
|
90 |
"""
|
91 |
|
|
|
100 |
load_pdf = gr.Button("Convert PDF to Magic AI language")
|
101 |
clear_btn = gr.Button("Clear Data")
|
102 |
|
103 |
+
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=450)
|
104 |
question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter")
|
105 |
submit_btn = gr.Button("Send Message")
|
106 |
|