Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -224,17 +224,6 @@ class SmartDocumentRAG:
|
|
| 224 |
return ""
|
| 225 |
|
| 226 |
def answer_question(self, query: str) -> str:
|
| 227 |
-
"""
|
| 228 |
-
Answer the user's question based on processed documents.
|
| 229 |
-
|
| 230 |
-
Features:
|
| 231 |
-
- Returns document summary if query asks for summary.
|
| 232 |
-
- Uses semantic search to find relevant context.
|
| 233 |
-
- Uses QA pipeline with prompt-style input.
|
| 234 |
-
- Applies confidence threshold to reduce hallucinations.
|
| 235 |
-
- Returns a fallback message if answer is unreliable.
|
| 236 |
-
"""
|
| 237 |
-
|
| 238 |
if not query.strip():
|
| 239 |
return "β Please ask a valid question."
|
| 240 |
|
|
@@ -243,43 +232,52 @@ class SmartDocumentRAG:
|
|
| 243 |
|
| 244 |
query_lower = query.lower()
|
| 245 |
|
| 246 |
-
# Handle summary requests
|
| 247 |
if any(word in query_lower for word in ['summary', 'summarize', 'overview', 'about']):
|
| 248 |
if self.document_summary:
|
| 249 |
return f"π Document Summary:\n\n{self.document_summary}"
|
| 250 |
else:
|
| 251 |
return "β οΈ Summary not available. Please process documents first."
|
| 252 |
|
| 253 |
-
|
| 254 |
-
|
|
|
|
| 255 |
if not context:
|
| 256 |
return "π Sorry, no relevant information was found for your question. Try rephrasing."
|
| 257 |
|
| 258 |
try:
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
|
|
|
|
|
|
| 262 |
|
| 263 |
-
|
| 264 |
-
|
| 265 |
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
|
| 270 |
-
|
| 271 |
-
if len(answer) < 3 or (query_lower not in answer.lower() and score < 0.35):
|
| 272 |
-
return "π€ I couldn't find a confident answer to your question based on the documents."
|
| 273 |
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
|
| 279 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
|
| 281 |
except Exception as e:
|
| 282 |
-
# If model fails, fallback to simple answer or message
|
| 283 |
return f"β An error occurred while answering your question: {str(e)}"
|
| 284 |
|
| 285 |
def extract_direct_answer(self, query: str, context: str) -> str:
|
|
|
|
| 224 |
return ""
|
| 225 |
|
| 226 |
def answer_question(self, query: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
if not query.strip():
|
| 228 |
return "β Please ask a valid question."
|
| 229 |
|
|
|
|
| 232 |
|
| 233 |
query_lower = query.lower()
|
| 234 |
|
|
|
|
| 235 |
if any(word in query_lower for word in ['summary', 'summarize', 'overview', 'about']):
|
| 236 |
if self.document_summary:
|
| 237 |
return f"π Document Summary:\n\n{self.document_summary}"
|
| 238 |
else:
|
| 239 |
return "β οΈ Summary not available. Please process documents first."
|
| 240 |
|
| 241 |
+
context = self.find_relevant_content(query, k=5)
|
| 242 |
+
print(f"Context found (top 5 chunks): {context}")
|
| 243 |
+
|
| 244 |
if not context:
|
| 245 |
return "π Sorry, no relevant information was found for your question. Try rephrasing."
|
| 246 |
|
| 247 |
try:
|
| 248 |
+
if self.model_type in ["distilbert-qa", "fallback"]:
|
| 249 |
+
result = self.qa_pipeline(question=query, context=context)
|
| 250 |
+
print(f"QA Pipeline output: {result}")
|
| 251 |
+
answer = result.get('answer', '').strip()
|
| 252 |
+
score = result.get('score', 0.0)
|
| 253 |
|
| 254 |
+
if not answer or score < 0.05:
|
| 255 |
+
return "π€ I couldn't find a confident answer to your question based on the documents."
|
| 256 |
|
| 257 |
+
snippet = context[:300].strip()
|
| 258 |
+
if len(context) > 300:
|
| 259 |
+
snippet += "..."
|
| 260 |
|
| 261 |
+
return f"**Answer:** {answer}\n\n*Context snippet:* {snippet}"
|
|
|
|
|
|
|
| 262 |
|
| 263 |
+
elif self.model_type == "flan-t5":
|
| 264 |
+
prompt = (
|
| 265 |
+
f"Answer the question based on the context below.\n\n"
|
| 266 |
+
f"Context:\n{context}\n\n"
|
| 267 |
+
f"Question: {query}\nAnswer:"
|
| 268 |
+
)
|
| 269 |
+
result = self.qa_pipeline(prompt, max_length=200, num_return_sequences=1)
|
| 270 |
+
print(f"Generative pipeline output: {result}")
|
| 271 |
|
| 272 |
+
answer = result[0]['generated_text'].replace(prompt, '').strip()
|
| 273 |
+
if not answer:
|
| 274 |
+
return "π€ I couldn't find a confident answer to your question based on the documents."
|
| 275 |
+
return f"**Answer:** {answer}"
|
| 276 |
+
|
| 277 |
+
else:
|
| 278 |
+
return "β οΈ Unsupported model type for QA."
|
| 279 |
|
| 280 |
except Exception as e:
|
|
|
|
| 281 |
return f"β An error occurred while answering your question: {str(e)}"
|
| 282 |
|
| 283 |
def extract_direct_answer(self, query: str, context: str) -> str:
|