Update app.py
Browse files
app.py
CHANGED
@@ -1,106 +1,76 @@
|
|
1 |
-
import gradio as gr
|
2 |
import os
|
3 |
-
import
|
4 |
-
import
|
5 |
-
from
|
6 |
-
|
7 |
-
from langchain.
|
8 |
-
|
9 |
-
|
10 |
-
from langchain_community.llms import OpenAI
|
11 |
-
|
12 |
-
from langchain_community.embeddings import OpenAIEmbeddings
|
13 |
-
from langchain_community.vectorstores import Chroma
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
from langchain.chains import ConversationalRetrievalChain
|
18 |
-
|
19 |
-
|
20 |
-
return "Loading..."
|
21 |
-
|
22 |
-
def pdf_changes(pdf_doc, open_ai_key):
|
23 |
-
if openai_key is not None:
|
24 |
-
os.environ['OPENAI_API_KEY'] = open_ai_key
|
25 |
-
loader = OnlinePDFLoader(pdf_doc.name)
|
26 |
-
documents = loader.load()
|
27 |
-
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
|
28 |
-
texts = text_splitter.split_documents(documents)
|
29 |
-
embeddings = OpenAIEmbeddings()
|
30 |
-
db = Chroma.from_documents(texts, embeddings)
|
31 |
-
retriever = db.as_retriever()
|
32 |
-
global qa
|
33 |
-
qa = ConversationalRetrievalChain.from_llm(
|
34 |
-
llm=OpenAI(temperature=0.5),
|
35 |
-
retriever=retriever,
|
36 |
-
return_source_documents=False)
|
37 |
-
return "Ready"
|
38 |
-
else:
|
39 |
-
return "You forgot OpenAI API key"
|
40 |
|
41 |
-
def add_text(history, text):
|
42 |
-
history = history + [(text, None)]
|
43 |
-
return history, ""
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
|
55 |
-
def
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
result = qa({"question": query, "chat_history": chat_history})
|
66 |
-
#print(result)
|
67 |
-
return result["answer"]
|
68 |
-
|
69 |
-
css="""
|
70 |
-
#col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
|
71 |
-
"""
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
<h1>Chat with PDF • OpenAI</h1>
|
76 |
-
<p style="text-align: center;">Upload a .PDF from your computer, click the "Load PDF to LangChain" button, <br />
|
77 |
-
when everything is ready, you can start asking questions about the pdf ;) <br />
|
78 |
-
This version is set to store chat history, and uses OpenAI as LLM, don't forget to copy/paste your OpenAI API key</p>
|
79 |
-
</div>
|
80 |
-
"""
|
81 |
|
|
|
|
|
|
|
82 |
|
83 |
-
|
84 |
-
|
85 |
-
gr.HTML(title)
|
86 |
-
|
87 |
-
with gr.Column():
|
88 |
-
openai_key = gr.Textbox(label="You OpenAI API key", type="password")
|
89 |
-
pdf_doc = gr.File(label="Load a pdf", file_types=['.pdf'], type="filepath")
|
90 |
-
with gr.Row():
|
91 |
-
langchain_status = gr.Textbox(label="Status", placeholder="", interactive=False)
|
92 |
-
load_pdf = gr.Button("Load pdf to langchain")
|
93 |
-
|
94 |
-
chatbot = gr.Chatbot([], elem_id="chatbot")
|
95 |
-
|
96 |
-
question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
|
97 |
-
submit_btn = gr.Button("Send Message")
|
98 |
-
load_pdf.click(loading_pdf, None, langchain_status, queue=False)
|
99 |
-
load_pdf.click(pdf_changes, inputs=[pdf_doc, openai_key], outputs=[langchain_status], queue=False)
|
100 |
-
question.submit(add_text, [chatbot, question], [chatbot, question]).then(
|
101 |
-
bot, chatbot, chatbot
|
102 |
-
)
|
103 |
-
submit_btn.click(add_text, [chatbot, question], [chatbot, question]).then(
|
104 |
-
bot, chatbot, chatbot)
|
105 |
|
106 |
-
|
|
|
|
|
|
1 |
import os
|
2 |
+
import gradio as gr
|
3 |
+
from langchain.document_loaders import PyPDFLoader
|
4 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
5 |
+
from langchain.embeddings import OpenAIEmbeddings
|
6 |
+
from langchain.vectorstores import FAISS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
from langchain.chains import ConversationalRetrievalChain
|
8 |
+
from langchain.chat_models import ChatOpenAI
|
9 |
+
from langchain.memory import ConversationBufferMemory
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
|
|
|
|
|
|
11 |
|
12 |
+
class AdvancedPdfChatbot:
|
13 |
+
def __init__(self, openai_api_key):
|
14 |
+
os.environ["OPENAI_API_KEY"] = openai_api_key
|
15 |
+
self.embeddings = OpenAIEmbeddings()
|
16 |
+
self.text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
17 |
+
self.llm = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo")
|
18 |
+
self.memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
19 |
+
self.qa_chain = None
|
|
|
20 |
|
21 |
+
def load_and_process_pdf(self, pdf_path):
|
22 |
+
loader = PyPDFLoader(pdf_path)
|
23 |
+
documents = loader.load()
|
24 |
+
texts = self.text_splitter.split_documents(documents)
|
25 |
+
self.db = FAISS.from_documents(texts, self.embeddings)
|
26 |
+
self.setup_conversation_chain()
|
27 |
+
|
28 |
+
def setup_conversation_chain(self):
|
29 |
+
self.qa_chain = ConversationalRetrievalChain.from_llm(
|
30 |
+
self.llm,
|
31 |
+
retriever=self.db.as_retriever(),
|
32 |
+
memory=self.memory
|
33 |
+
)
|
34 |
+
|
35 |
+
def chat(self, query):
|
36 |
+
if not self.qa_chain:
|
37 |
+
return "Please upload a PDF first."
|
38 |
+
result = self.qa_chain({"question": query})
|
39 |
+
return result['answer']
|
40 |
+
|
41 |
+
# Initialize the chatbot
|
42 |
+
openai_api_key = "your-openai-api-key-here"
|
43 |
+
pdf_chatbot = AdvancedPdfChatbot(openai_api_key)
|
44 |
+
|
45 |
+
def upload_pdf(pdf_file):
|
46 |
+
if pdf_file is None:
|
47 |
+
return "Please upload a PDF file."
|
48 |
+
file_path = pdf_file.name
|
49 |
+
pdf_chatbot.load_and_process_pdf(file_path)
|
50 |
+
return "PDF uploaded and processed successfully. You can now start chatting!"
|
51 |
+
|
52 |
+
def respond(message, history):
|
53 |
+
bot_message = pdf_chatbot.chat(message)
|
54 |
+
history.append((message, bot_message))
|
55 |
+
return "", history
|
56 |
+
|
57 |
+
# Create the Gradio interface
|
58 |
+
with gr.Blocks() as demo:
|
59 |
+
gr.Markdown("# PDF Chatbot")
|
60 |
|
61 |
+
with gr.Row():
|
62 |
+
pdf_upload = gr.File(label="Upload PDF", file_types=[".pdf"])
|
63 |
+
upload_button = gr.Button("Process PDF")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
+
upload_status = gr.Textbox(label="Upload Status")
|
66 |
+
upload_button.click(upload_pdf, inputs=[pdf_upload], outputs=[upload_status])
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
chatbot_interface = gr.Chatbot()
|
69 |
+
msg = gr.Textbox()
|
70 |
+
clear = gr.Button("Clear")
|
71 |
|
72 |
+
msg.submit(respond, inputs=[msg, chatbot_interface], outputs=[msg, chatbot_interface])
|
73 |
+
clear.click(lambda: None, None, chatbot_interface, queue=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
+
if __name__ == "__main__":
|
76 |
+
demo.launch()
|