pradeepsengarr commited on
Commit
48c1ca7
Β·
verified Β·
1 Parent(s): d64804c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -31
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
- # Find relevant chunks for context
254
- context = self.find_relevant_content(query, top_k=3)
 
255
  if not context:
256
  return "πŸ” Sorry, no relevant information was found for your question. Try rephrasing."
257
 
258
  try:
259
- # Prepare input for QA pipeline (some QA pipelines accept question and context separately)
260
- # For distilbert QA pipeline:
261
- result = self.qa_pipeline(question=query, context=context)
 
 
262
 
263
- answer = result.get('answer', '').strip()
264
- score = result.get('score', 0.0)
265
 
266
- # Confidence threshold to prevent hallucination
267
- if score < 0.20 or not answer or answer.lower() in ['no answer', '']:
268
- return "πŸ€” I couldn't find a confident answer to your question based on the documents."
269
 
270
- # Optional heuristic: check if answer is too generic or unrelated
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
- # Return answer with a snippet of context for transparency
275
- snippet = context[:300].strip()
276
- if len(context) > 300:
277
- snippet += "..."
 
 
 
 
278
 
279
- return f"**Answer:** {answer}\n\n*Context snippet:* {snippet}"
 
 
 
 
 
 
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: