Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,13 +19,16 @@ def process_file(file_path):
|
|
| 19 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 20 |
vectorstore = FAISS.from_documents(texts, embeddings)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
Use the following pieces of context to answer the question at the end. If you don't know the answer, just say you don't know.
|
| 24 |
{context}
|
| 25 |
Question: {question}
|
| 26 |
Helpful Answer:
|
| 27 |
"""
|
| 28 |
-
prompt = PromptTemplate(
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
llm = HuggingFaceEndpoint(
|
| 31 |
repo_id="meta-llama/Meta-Llama-3-8B-Instruct",
|
|
@@ -35,7 +38,7 @@ def process_file(file_path):
|
|
| 35 |
huggingfacehub_api_token=os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
| 36 |
)
|
| 37 |
|
| 38 |
-
|
| 39 |
llm=llm,
|
| 40 |
chain_type="stuff",
|
| 41 |
retriever=vectorstore.as_retriever(search_kwargs={"k": 3}),
|
|
@@ -43,46 +46,43 @@ def process_file(file_path):
|
|
| 43 |
return_source_documents=True
|
| 44 |
)
|
| 45 |
|
| 46 |
-
return
|
| 47 |
|
| 48 |
-
# Chat start
|
| 49 |
@cl.on_chat_start
|
| 50 |
async def start():
|
| 51 |
-
await cl.Message(
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
accept=["application/pdf"],
|
| 55 |
-
max_size_mb=
|
| 56 |
timeout=180
|
| 57 |
).send()
|
| 58 |
|
| 59 |
-
|
| 60 |
-
await cl.Message(content="⚠️ No file was uploaded.").send()
|
| 61 |
-
return
|
| 62 |
-
|
| 63 |
-
file = files[0]
|
| 64 |
-
file_path = file.path
|
| 65 |
-
|
| 66 |
-
qa_chain = process_file(file_path)
|
| 67 |
-
cl.user_session.set("qa_chain", qa_chain)
|
| 68 |
-
cl.user_session.set("ready", True)
|
| 69 |
-
|
| 70 |
-
await cl.Message(content=f"✅ `{file.name}` processed successfully. You can now ask questions.").send()
|
| 71 |
-
|
| 72 |
-
# Handle questions
|
| 73 |
@cl.on_message
|
| 74 |
async def handle_message(message: cl.Message):
|
| 75 |
-
if
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
return
|
| 78 |
|
| 79 |
-
|
| 80 |
-
if not
|
| 81 |
-
await cl.Message(
|
| 82 |
return
|
| 83 |
|
| 84 |
-
|
| 85 |
-
response = qa_chain(
|
| 86 |
-
|
| 87 |
-
|
| 88 |
|
|
|
|
| 19 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 20 |
vectorstore = FAISS.from_documents(texts, embeddings)
|
| 21 |
|
| 22 |
+
custom_prompt_template = """
|
| 23 |
+
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.
|
| 24 |
{context}
|
| 25 |
Question: {question}
|
| 26 |
Helpful Answer:
|
| 27 |
"""
|
| 28 |
+
prompt = PromptTemplate(
|
| 29 |
+
template=custom_prompt_template,
|
| 30 |
+
input_variables=["context", "question"]
|
| 31 |
+
)
|
| 32 |
|
| 33 |
llm = HuggingFaceEndpoint(
|
| 34 |
repo_id="meta-llama/Meta-Llama-3-8B-Instruct",
|
|
|
|
| 38 |
huggingfacehub_api_token=os.environ.get("HUGGINGFACEHUB_API_TOKEN")
|
| 39 |
)
|
| 40 |
|
| 41 |
+
qa = RetrievalQA.from_chain_type(
|
| 42 |
llm=llm,
|
| 43 |
chain_type="stuff",
|
| 44 |
retriever=vectorstore.as_retriever(search_kwargs={"k": 3}),
|
|
|
|
| 46 |
return_source_documents=True
|
| 47 |
)
|
| 48 |
|
| 49 |
+
return qa
|
| 50 |
|
| 51 |
+
# Chat start — show file upload prompt
|
| 52 |
@cl.on_chat_start
|
| 53 |
async def start():
|
| 54 |
+
await cl.Message("👋 Welcome! Please upload a PDF to get started.").send()
|
| 55 |
+
|
| 56 |
+
# Ask user to upload a file
|
| 57 |
+
await cl.AskFileMessage(
|
| 58 |
+
content="📄 Upload a PDF file below:",
|
| 59 |
accept=["application/pdf"],
|
| 60 |
+
max_size_mb=20,
|
| 61 |
timeout=180
|
| 62 |
).send()
|
| 63 |
|
| 64 |
+
# Handle file and build QA chain
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
@cl.on_message
|
| 66 |
async def handle_message(message: cl.Message):
|
| 67 |
+
if files := await cl.user_session.get("files"):
|
| 68 |
+
file = files[0] # Get the first uploaded file
|
| 69 |
+
file_path = file.path
|
| 70 |
+
|
| 71 |
+
# Process and store the chain
|
| 72 |
+
qa_chain = process_file(file_path)
|
| 73 |
+
cl.user_session.set("qa_chain", qa_chain)
|
| 74 |
+
cl.user_session.set("ready", True)
|
| 75 |
+
await cl.Message(content=f"✅ File `{file.name}` processed. Ask your question!").send()
|
| 76 |
+
cl.user_session.set("files", None) # Clear after processing
|
| 77 |
return
|
| 78 |
|
| 79 |
+
# If file was already processed, answer question
|
| 80 |
+
if not cl.user_session.get("ready"):
|
| 81 |
+
await cl.Message("⚠️ Please upload a PDF file first.").send()
|
| 82 |
return
|
| 83 |
|
| 84 |
+
qa_chain = cl.user_session.get("qa_chain")
|
| 85 |
+
response = qa_chain(message.content)
|
| 86 |
+
answer = response["result"]
|
| 87 |
+
await cl.Message(content=answer).send()
|
| 88 |
|