Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,11 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
from langchain.chains.question_answering import load_qa_chain
|
| 3 |
-
from langchain_openai import ChatOpenAI
|
| 4 |
from langchain.vectorstores import FAISS
|
| 5 |
from langchain.document_loaders import PyPDFLoader
|
| 6 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 7 |
-
from langchain_openai import OpenAIEmbeddings
|
| 8 |
-
import os
|
| 9 |
-
from dotenv import load_dotenv
|
| 10 |
|
| 11 |
load_dotenv()
|
| 12 |
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
|
@@ -14,48 +13,45 @@ os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
|
| 14 |
llm = ChatOpenAI(model_name="gpt-4", temperature=0)
|
| 15 |
embedding = OpenAIEmbeddings()
|
| 16 |
|
| 17 |
-
# Global
|
| 18 |
-
db = None
|
| 19 |
|
| 20 |
def upload_pdf(file):
|
| 21 |
global db
|
| 22 |
-
|
| 23 |
-
loader = PyPDFLoader(pdf_path)
|
| 24 |
documents = loader.load()
|
| 25 |
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 26 |
docs = splitter.split_documents(documents)
|
| 27 |
db = FAISS.from_documents(docs, embedding)
|
| 28 |
-
return "β
PDF processed.
|
| 29 |
|
| 30 |
def chat_with_pdf(message, history):
|
| 31 |
global db
|
| 32 |
if not db:
|
| 33 |
-
return "β Please upload
|
|
|
|
| 34 |
retriever = db.as_retriever()
|
| 35 |
chain = load_qa_chain(llm, chain_type="stuff")
|
| 36 |
docs = retriever.get_relevant_documents(message)
|
| 37 |
response = chain.run(input_documents=docs, question=message)
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
with gr.Blocks(title="π PDF Chatbot") as demo:
|
| 41 |
-
gr.Markdown("## π Interactive PDF Reader + Chatbot\nUpload a PDF and chat with it using GPT-4.")
|
| 42 |
-
|
| 43 |
with gr.Row():
|
| 44 |
-
|
| 45 |
upload_btn = gr.Button("Process PDF")
|
| 46 |
-
|
| 47 |
status = gr.Textbox(label="Status", interactive=False)
|
| 48 |
-
|
| 49 |
-
chatbot = gr.
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
)
|
| 58 |
-
|
| 59 |
-
upload_btn.click(fn=upload_pdf, inputs=pdf_file, outputs=status)
|
| 60 |
|
| 61 |
demo.launch(share=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
from langchain.chains.question_answering import load_qa_chain
|
| 5 |
+
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
|
| 6 |
from langchain.vectorstores import FAISS
|
| 7 |
from langchain.document_loaders import PyPDFLoader
|
| 8 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
load_dotenv()
|
| 11 |
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
|
|
|
| 13 |
llm = ChatOpenAI(model_name="gpt-4", temperature=0)
|
| 14 |
embedding = OpenAIEmbeddings()
|
| 15 |
|
| 16 |
+
db = None # Global DB
|
|
|
|
| 17 |
|
| 18 |
def upload_pdf(file):
|
| 19 |
global db
|
| 20 |
+
loader = PyPDFLoader(file.name)
|
|
|
|
| 21 |
documents = loader.load()
|
| 22 |
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 23 |
docs = splitter.split_documents(documents)
|
| 24 |
db = FAISS.from_documents(docs, embedding)
|
| 25 |
+
return "β
PDF processed. Ask your questions below."
|
| 26 |
|
| 27 |
def chat_with_pdf(message, history):
|
| 28 |
global db
|
| 29 |
if not db:
|
| 30 |
+
return history + [[message, "β Please upload a PDF first."]]
|
| 31 |
+
|
| 32 |
retriever = db.as_retriever()
|
| 33 |
chain = load_qa_chain(llm, chain_type="stuff")
|
| 34 |
docs = retriever.get_relevant_documents(message)
|
| 35 |
response = chain.run(input_documents=docs, question=message)
|
| 36 |
+
history.append([message, response])
|
| 37 |
+
return history
|
| 38 |
+
|
| 39 |
+
with gr.Blocks(title="π Interactive PDF Chatbot") as demo:
|
| 40 |
+
gr.Markdown("## π Chat with a PDF using GPT-4")
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
with gr.Row():
|
| 43 |
+
pdf = gr.File(label="Upload PDF", file_types=[".pdf"])
|
| 44 |
upload_btn = gr.Button("Process PDF")
|
|
|
|
| 45 |
status = gr.Textbox(label="Status", interactive=False)
|
| 46 |
+
|
| 47 |
+
chatbot = gr.Chatbot(label="Chat History", height=400)
|
| 48 |
+
msg = gr.Textbox(label="Ask a question", placeholder="Type your question and press Enter...", lines=2)
|
| 49 |
+
send_btn = gr.Button("Send")
|
| 50 |
+
|
| 51 |
+
upload_btn.click(upload_pdf, inputs=pdf, outputs=status)
|
| 52 |
+
msg.submit(chat_with_pdf, inputs=[msg, chatbot], outputs=chatbot)
|
| 53 |
+
send_btn.click(chat_with_pdf, inputs=[msg, chatbot], outputs=chatbot)
|
| 54 |
+
send_btn.click(lambda: "", None, msg) # Clear input after send
|
| 55 |
+
msg.submit(lambda: "", None, msg) # Clear input after enter
|
|
|
|
|
|
|
| 56 |
|
| 57 |
demo.launch(share=True)
|