Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,81 +8,98 @@ from langchain.prompts import PromptTemplate
|
|
| 8 |
from langchain_community.llms import HuggingFaceEndpoint
|
| 9 |
from langchain.chains import RetrievalQA
|
| 10 |
|
| 11 |
-
# PDF
|
| 12 |
def process_file(file_path):
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
@cl.on_chat_start
|
| 55 |
async def start():
|
| 56 |
await cl.Message("π Welcome! Please upload a PDF to get started.").send()
|
| 57 |
|
| 58 |
-
|
| 59 |
-
content="π Upload
|
| 60 |
accept=["application/pdf"],
|
| 61 |
max_size_mb=20,
|
| 62 |
timeout=180
|
| 63 |
).send()
|
| 64 |
|
| 65 |
-
if
|
| 66 |
-
file =
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
| 72 |
else:
|
| 73 |
-
await cl.Message("
|
| 74 |
|
| 75 |
-
# On
|
| 76 |
@cl.on_message
|
| 77 |
async def handle_message(message: cl.Message):
|
| 78 |
if not cl.user_session.get("ready"):
|
| 79 |
-
await cl.Message("β οΈ Please upload a PDF first.").send()
|
| 80 |
return
|
| 81 |
|
| 82 |
qa_chain = cl.user_session.get("qa_chain")
|
| 83 |
try:
|
|
|
|
| 84 |
response = qa_chain(message.content)
|
| 85 |
answer = response["result"]
|
| 86 |
-
await cl.Message(content=answer).send()
|
| 87 |
except Exception as e:
|
| 88 |
-
await cl.Message(
|
|
|
|
| 8 |
from langchain_community.llms import HuggingFaceEndpoint
|
| 9 |
from langchain.chains import RetrievalQA
|
| 10 |
|
| 11 |
+
# Helper function to process PDF and build the QA chain
|
| 12 |
def process_file(file_path):
|
| 13 |
+
try:
|
| 14 |
+
loader = PyPDFLoader(file_path)
|
| 15 |
+
documents = loader.load()
|
| 16 |
+
|
| 17 |
+
cl.run_sync(cl.Message("π PDF loaded successfully. Splitting text...").send())
|
| 18 |
+
|
| 19 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 20 |
+
texts = text_splitter.split_documents(documents)
|
| 21 |
+
|
| 22 |
+
cl.run_sync(cl.Message("π Creating embeddings and FAISS vector store...").send())
|
| 23 |
+
|
| 24 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 25 |
+
vectorstore = FAISS.from_documents(texts, embeddings)
|
| 26 |
+
|
| 27 |
+
custom_prompt_template = """
|
| 28 |
+
Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know.
|
| 29 |
+
{context}
|
| 30 |
+
Question: {question}
|
| 31 |
+
Helpful Answer:
|
| 32 |
+
"""
|
| 33 |
+
prompt = PromptTemplate(
|
| 34 |
+
template=custom_prompt_template,
|
| 35 |
+
input_variables=["context", "question"]
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
llm = HuggingFaceEndpoint(
|
| 39 |
+
repo_id="meta-llama/Meta-Llama-3-8B-Instruct",
|
| 40 |
+
temperature=0.5,
|
| 41 |
+
max_new_tokens=500,
|
| 42 |
+
top_k=50,
|
| 43 |
+
huggingfacehub_api_token=os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
cl.run_sync(cl.Message("βοΈ Building RetrievalQA chain...").send())
|
| 47 |
+
|
| 48 |
+
qa = RetrievalQA.from_chain_type(
|
| 49 |
+
llm=llm,
|
| 50 |
+
chain_type="stuff",
|
| 51 |
+
retriever=vectorstore.as_retriever(search_kwargs={"k": 3}),
|
| 52 |
+
chain_type_kwargs={"prompt": prompt},
|
| 53 |
+
return_source_documents=True
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
cl.run_sync(cl.Message("β
QA chain is ready. You can ask your question now.").send())
|
| 57 |
+
|
| 58 |
+
return qa
|
| 59 |
+
|
| 60 |
+
except Exception as e:
|
| 61 |
+
cl.run_sync(cl.Message(f"β Error processing file: {e}").send())
|
| 62 |
+
return None
|
| 63 |
+
|
| 64 |
+
# On chat start β ask user for PDF
|
| 65 |
@cl.on_chat_start
|
| 66 |
async def start():
|
| 67 |
await cl.Message("π Welcome! Please upload a PDF to get started.").send()
|
| 68 |
|
| 69 |
+
file_message = await cl.AskFileMessage(
|
| 70 |
+
content="π Upload a PDF file below:",
|
| 71 |
accept=["application/pdf"],
|
| 72 |
max_size_mb=20,
|
| 73 |
timeout=180
|
| 74 |
).send()
|
| 75 |
|
| 76 |
+
if file_message.files:
|
| 77 |
+
file = file_message.files[0]
|
| 78 |
+
file_path = file.path
|
| 79 |
+
|
| 80 |
+
await cl.Message(f"π Received file: `{file.name}`. Now processing...").send()
|
| 81 |
+
qa_chain = process_file(file_path)
|
| 82 |
|
| 83 |
+
if qa_chain:
|
| 84 |
+
cl.user_session.set("qa_chain", qa_chain)
|
| 85 |
+
cl.user_session.set("ready", True)
|
| 86 |
+
else:
|
| 87 |
+
await cl.Message("β οΈ File could not be processed. Please try again.").send()
|
| 88 |
else:
|
| 89 |
+
await cl.Message("β οΈ No file uploaded. Please upload a PDF file to proceed.").send()
|
| 90 |
|
| 91 |
+
# On message β handle question
|
| 92 |
@cl.on_message
|
| 93 |
async def handle_message(message: cl.Message):
|
| 94 |
if not cl.user_session.get("ready"):
|
| 95 |
+
await cl.Message("β οΈ Please upload a PDF file first.").send()
|
| 96 |
return
|
| 97 |
|
| 98 |
qa_chain = cl.user_session.get("qa_chain")
|
| 99 |
try:
|
| 100 |
+
await cl.Message("π¬ Thinking...").send()
|
| 101 |
response = qa_chain(message.content)
|
| 102 |
answer = response["result"]
|
| 103 |
+
await cl.Message(content=f"π§ Answer: {answer}").send()
|
| 104 |
except Exception as e:
|
| 105 |
+
await cl.Message(f"β Error during question answering: {e}").send()
|