Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,117 +1,122 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
| 6 |
from langchain_chroma import Chroma
|
| 7 |
-
from
|
| 8 |
-
from
|
| 9 |
-
from langchain.llms import OpenAI
|
| 10 |
-
from langchain.document_loaders import TextLoader
|
| 11 |
-
from langchain.docstore.document import Document
|
| 12 |
-
from transformers import AutoTokenizer
|
| 13 |
-
from langchain.document_loaders import PyPDFLoader
|
| 14 |
from langchain.memory import ConversationBufferMemory
|
| 15 |
from langchain.chains import ConversationalRetrievalChain
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
# price is a factor for our company, so we're going to use a low cost model
|
| 20 |
MODEL = "gpt-4o-mini"
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
# Load environment variables in a file called .env
|
| 24 |
|
| 25 |
load_dotenv(override=True)
|
| 26 |
|
|
|
|
|
|
|
| 27 |
|
| 28 |
def process_pdf(pdf_file):
|
| 29 |
try:
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
pages = loader.load()
|
|
|
|
| 32 |
if not pages:
|
| 33 |
-
raise ValueError("No text found in
|
| 34 |
-
|
|
|
|
| 35 |
chunk_size=500,
|
| 36 |
-
chunk_overlap=50
|
| 37 |
)
|
| 38 |
-
chunks =
|
| 39 |
-
|
| 40 |
-
raise ValueError("Unable to split the PDF into chunks.")
|
| 41 |
if not chunks:
|
| 42 |
-
raise ValueError(
|
|
|
|
|
|
|
| 43 |
embeddings = OpenAIEmbeddings()
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
# Return the conversation chain
|
| 71 |
-
|
| 72 |
return conversation_chain
|
|
|
|
| 73 |
except Exception as e:
|
| 74 |
raise RuntimeError(f"PDF processing failed: {str(e)}")
|
| 75 |
|
| 76 |
-
|
| 77 |
def upload_pdf(file):
|
| 78 |
global chain
|
| 79 |
if file is None:
|
| 80 |
chain = None
|
| 81 |
-
return "
|
| 82 |
chain = process_pdf(file)
|
| 83 |
-
return "processed
|
|
|
|
| 84 |
|
| 85 |
-
# ask_question function
|
| 86 |
def ask_question(message, history):
|
| 87 |
if chain is None:
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
return history, history, ""
|
| 97 |
-
|
|
|
|
| 98 |
with gr.Blocks() as demo:
|
| 99 |
-
gr.Markdown("## Chat with your
|
| 100 |
-
|
| 101 |
file_input = gr.File(label="Upload your PDF", file_types=[".pdf"])
|
| 102 |
-
# Status text
|
| 103 |
status = gr.Textbox(label="Status", interactive=False)
|
| 104 |
|
| 105 |
-
chatbot = gr.Chatbot(label="Chat history
|
| 106 |
-
msg=gr.Textbox(label="Ask anything related to
|
| 107 |
clear = gr.Button("Clear chat")
|
| 108 |
|
| 109 |
-
state = gr.State([])
|
| 110 |
|
| 111 |
file_input.change(upload_pdf, inputs=[file_input], outputs=[status])
|
| 112 |
-
msg.submit(ask_question, [msg, state], [chatbot, state, msg])
|
| 113 |
-
clear.click(lambda: ([],[]), None, [chatbot, state])
|
| 114 |
-
chain = None # global QA chain
|
| 115 |
|
| 116 |
-
|
| 117 |
-
demo.launch(inline=False)
|
|
|
|
| 1 |
import os
|
| 2 |
+
import shutil
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
import gradio as gr
|
| 5 |
+
|
| 6 |
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
| 7 |
from langchain_chroma import Chroma
|
| 8 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 9 |
+
from langchain_community.document_loaders import PyPDFLoader
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
from langchain.memory import ConversationBufferMemory
|
| 11 |
from langchain.chains import ConversationalRetrievalChain
|
| 12 |
|
| 13 |
+
# Low-cost model
|
|
|
|
|
|
|
| 14 |
MODEL = "gpt-4o-mini"
|
| 15 |
+
DB_DIR = "vector_db"
|
|
|
|
|
|
|
| 16 |
|
| 17 |
load_dotenv(override=True)
|
| 18 |
|
| 19 |
+
chain = None # global chain (OK for single-user; see note below)
|
| 20 |
+
|
| 21 |
|
| 22 |
def process_pdf(pdf_file):
|
| 23 |
try:
|
| 24 |
+
if not os.getenv("OPENAI_API_KEY"):
|
| 25 |
+
raise RuntimeError(
|
| 26 |
+
"OPENAI_API_KEY is not set. Add it to your environment or as a Secret on HF Spaces."
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
file_path = pdf_file.name # gr.File gives a temp file with .name path
|
| 30 |
+
loader = PyPDFLoader(file_path)
|
| 31 |
pages = loader.load()
|
| 32 |
+
|
| 33 |
if not pages:
|
| 34 |
+
raise ValueError("No text found in PDF (may be scanned or protected).")
|
| 35 |
+
|
| 36 |
+
splitter = RecursiveCharacterTextSplitter(
|
| 37 |
chunk_size=500,
|
| 38 |
+
chunk_overlap=50,
|
| 39 |
)
|
| 40 |
+
chunks = splitter.split_documents(pages)
|
| 41 |
+
|
|
|
|
| 42 |
if not chunks:
|
| 43 |
+
raise ValueError("Unable to split PDF into chunks (empty/protected PDF).")
|
| 44 |
+
|
| 45 |
+
# Embeddings (you can also specify: model="text-embedding-3-small")
|
| 46 |
embeddings = OpenAIEmbeddings()
|
| 47 |
+
|
| 48 |
+
# Reset persisted DB each upload
|
| 49 |
+
if os.path.exists(DB_DIR):
|
| 50 |
+
shutil.rmtree(DB_DIR, ignore_errors=True)
|
| 51 |
+
|
| 52 |
+
vectorstore = Chroma.from_documents(
|
| 53 |
+
documents=chunks,
|
| 54 |
+
embedding=embeddings,
|
| 55 |
+
persist_directory=DB_DIR,
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
llm = ChatOpenAI(model=MODEL, temperature=0.2)
|
| 59 |
+
|
| 60 |
+
memory = ConversationBufferMemory(
|
| 61 |
+
memory_key="chat_history",
|
| 62 |
+
return_messages=True,
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
retriever = vectorstore.as_retriever(search_kwargs={"k": 4})
|
| 66 |
+
|
| 67 |
+
conversation_chain = ConversationalRetrievalChain.from_llm(
|
| 68 |
+
llm=llm,
|
| 69 |
+
retriever=retriever,
|
| 70 |
+
memory=memory,
|
| 71 |
+
)
|
| 72 |
+
|
|
|
|
|
|
|
| 73 |
return conversation_chain
|
| 74 |
+
|
| 75 |
except Exception as e:
|
| 76 |
raise RuntimeError(f"PDF processing failed: {str(e)}")
|
| 77 |
|
| 78 |
+
|
| 79 |
def upload_pdf(file):
|
| 80 |
global chain
|
| 81 |
if file is None:
|
| 82 |
chain = None
|
| 83 |
+
return "Please upload a PDF."
|
| 84 |
chain = process_pdf(file)
|
| 85 |
+
return "PDF processed. Ask questions now."
|
| 86 |
+
|
| 87 |
|
|
|
|
| 88 |
def ask_question(message, history):
|
| 89 |
if chain is None:
|
| 90 |
+
history = history or []
|
| 91 |
+
history.append({"role": "assistant", "content": "Upload the PDF first."})
|
| 92 |
+
return history, history, ""
|
| 93 |
+
|
| 94 |
+
try:
|
| 95 |
+
result = chain.invoke({"question": message})
|
| 96 |
+
answer = result.get("answer", "No answer found.")
|
| 97 |
+
except Exception as e:
|
| 98 |
+
answer = f"Error: {str(e)}"
|
| 99 |
+
|
| 100 |
+
history = history or []
|
| 101 |
+
history.append({"role": "user", "content": message})
|
| 102 |
+
history.append({"role": "assistant", "content": answer})
|
| 103 |
return history, history, ""
|
| 104 |
+
|
| 105 |
+
|
| 106 |
with gr.Blocks() as demo:
|
| 107 |
+
gr.Markdown("## Chat with your PDF")
|
| 108 |
+
|
| 109 |
file_input = gr.File(label="Upload your PDF", file_types=[".pdf"])
|
|
|
|
| 110 |
status = gr.Textbox(label="Status", interactive=False)
|
| 111 |
|
| 112 |
+
chatbot = gr.Chatbot(label="Chat history", type="messages")
|
| 113 |
+
msg = gr.Textbox(label="Ask anything related to the PDF...")
|
| 114 |
clear = gr.Button("Clear chat")
|
| 115 |
|
| 116 |
+
state = gr.State([])
|
| 117 |
|
| 118 |
file_input.change(upload_pdf, inputs=[file_input], outputs=[status])
|
| 119 |
+
msg.submit(ask_question, inputs=[msg, state], outputs=[chatbot, state, msg])
|
| 120 |
+
clear.click(lambda: ([], []), inputs=None, outputs=[chatbot, state])
|
|
|
|
| 121 |
|
| 122 |
+
demo.launch(inline=False)
|
|
|