Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -483,26 +483,45 @@ def setup_qa(db):
|
|
483 |
return RetrievalQA.from_chain_type(llm=llm, retriever=retriever, return_source_documents=True)
|
484 |
|
485 |
# --- Process Answer ---
|
486 |
-
def process_answer(
|
487 |
-
|
488 |
-
|
|
|
489 |
|
490 |
-
|
491 |
-
|
492 |
-
|
493 |
|
494 |
-
|
495 |
-
|
496 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
497 |
|
498 |
-
{user_question}
|
499 |
-
"""
|
500 |
-
try:
|
501 |
-
result = qa({"query": prompt})
|
502 |
-
return result['result']
|
503 |
-
except Exception as e:
|
504 |
-
logging.error(f"Error generating answer: {e}")
|
505 |
-
return "Sorry, I couldn't generate an answer due to an internal error."
|
506 |
|
507 |
# --- UI Layout ---
|
508 |
with st.sidebar:
|
|
|
483 |
return RetrievalQA.from_chain_type(llm=llm, retriever=retriever, return_source_documents=True)
|
484 |
|
485 |
# --- Process Answer ---
|
486 |
+
def process_answer(question, full_text):
|
487 |
+
# STEP 1: Chunk the PDF text
|
488 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
|
489 |
+
docs = text_splitter.create_documents([full_text])
|
490 |
|
491 |
+
# STEP 2: Create embeddings
|
492 |
+
embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
|
493 |
+
db = Chroma.from_documents(docs, embeddings)
|
494 |
|
495 |
+
# STEP 3: Retrieve relevant chunks using the question
|
496 |
+
retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 5})
|
497 |
+
relevant_docs = retriever.get_relevant_documents(question)
|
498 |
+
|
499 |
+
# STEP 4: Format the context
|
500 |
+
context = "\n\n".join([doc.page_content for doc in relevant_docs])
|
501 |
+
|
502 |
+
# STEP 5: Prompting
|
503 |
+
prompt_template = """
|
504 |
+
You are a helpful assistant that answers questions based on the context below.
|
505 |
+
|
506 |
+
Context:
|
507 |
+
{context}
|
508 |
+
|
509 |
+
Question: {question}
|
510 |
+
|
511 |
+
Answer:
|
512 |
+
""".strip()
|
513 |
+
|
514 |
+
prompt = prompt_template.format(context=context, question=question)
|
515 |
+
|
516 |
+
# STEP 6: Load the model and generate response
|
517 |
+
llm = HuggingFacePipeline.from_model_id(
|
518 |
+
model_id="MBZUAI/LaMini-T5-738M",
|
519 |
+
task="text2text-generation",
|
520 |
+
model_kwargs={"temperature": 0.3, "max_length": 256},
|
521 |
+
)
|
522 |
+
|
523 |
+
return llm.invoke(prompt)
|
524 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
525 |
|
526 |
# --- UI Layout ---
|
527 |
with st.sidebar:
|