deepkansara-123 commited on
Commit
bbcdadc
·
verified ·
1 Parent(s): ea04179

Upload 6 files

Browse files
Files changed (1) hide show
  1. app.py +26 -3
app.py CHANGED
@@ -51,11 +51,16 @@ def upload_pdf(files):
51
 
52
 
53
 
 
 
 
54
  def generate_qa(token):
55
  try:
56
  if not token:
57
  return "⚠️ Please provide a token."
58
 
 
 
59
  # Load chunk_data using token
60
  with sqlite3.connect("my_database.db") as conn:
61
  cursor = conn.cursor()
@@ -63,41 +68,59 @@ def generate_qa(token):
63
  row = cursor.fetchone()
64
 
65
  if not row:
 
66
  return "❌ No data found for this token."
67
 
68
  chunks = json.loads(row[0])
 
 
 
 
69
  qa_pairs = []
70
 
71
- for chunk in chunks:
 
72
  questions = qgen.generate(chunk)
 
 
73
  if not questions:
 
74
  continue
75
 
76
  for question in questions[:2]: # Max 2 Qs per chunk
77
  prompt = f"Context: {chunk}\n\nQuestion: {question}\n\nAnswer:"
 
 
78
  try:
79
  result = qa_model(prompt, max_length=256, do_sample=False)
 
 
80
  if isinstance(result, list) and "generated_text" in result[0]:
81
  answer = result[0]["generated_text"].strip()
82
  elif isinstance(result, dict) and "answer" in result:
83
  answer = result["answer"].strip()
84
  else:
85
  answer = "N/A"
 
 
86
  qa_pairs.append(f"Q: {question}\nA: {answer}")
 
87
  except Exception as e:
88
- print(f"QA model failed: {e}")
89
  continue
90
 
91
  if not qa_pairs:
 
92
  return "⚠️ No Q&A pairs generated."
93
 
 
94
  return "\n\n".join(qa_pairs)
95
 
96
  except Exception as e:
 
97
  return f"❌ Error: {str(e)}"
98
 
99
 
100
-
101
  # ✅ Ask question using token (semantic similarity)
102
  def ask_question(token, question):
103
  try:
 
51
 
52
 
53
 
54
+ # Load QG and QA once
55
+ qgen = QGenerator()
56
+ qa_model = pipeline("text2text-generation", model="google/flan-t5-base")
57
  def generate_qa(token):
58
  try:
59
  if not token:
60
  return "⚠️ Please provide a token."
61
 
62
+ print("📥 Received Token:", token)
63
+
64
  # Load chunk_data using token
65
  with sqlite3.connect("my_database.db") as conn:
66
  cursor = conn.cursor()
 
68
  row = cursor.fetchone()
69
 
70
  if not row:
71
+ print("❌ No data found for token in DB.")
72
  return "❌ No data found for this token."
73
 
74
  chunks = json.loads(row[0])
75
+ if not chunks:
76
+ print("⚠️ Chunk data is empty.")
77
+ return "⚠️ No content available in database for this PDF."
78
+
79
  qa_pairs = []
80
 
81
+ for i, chunk in enumerate(chunks):
82
+ print(f"\n🔹 Processing chunk {i+1}/{len(chunks)}")
83
  questions = qgen.generate(chunk)
84
+ print(f"🧠 Questions generated: {questions}")
85
+
86
  if not questions:
87
+ print("⚠️ No questions generated for this chunk.")
88
  continue
89
 
90
  for question in questions[:2]: # Max 2 Qs per chunk
91
  prompt = f"Context: {chunk}\n\nQuestion: {question}\n\nAnswer:"
92
+ print(f"➡️ Prompt:\n{prompt}")
93
+
94
  try:
95
  result = qa_model(prompt, max_length=256, do_sample=False)
96
+ print(f"⬅️ Raw model output: {result}")
97
+
98
  if isinstance(result, list) and "generated_text" in result[0]:
99
  answer = result[0]["generated_text"].strip()
100
  elif isinstance(result, dict) and "answer" in result:
101
  answer = result["answer"].strip()
102
  else:
103
  answer = "N/A"
104
+
105
+ print(f"✅ Final Answer: {answer}")
106
  qa_pairs.append(f"Q: {question}\nA: {answer}")
107
+
108
  except Exception as e:
109
+ print(f"QA model failed: {e}")
110
  continue
111
 
112
  if not qa_pairs:
113
+ print("⚠️ No Q&A pairs generated.")
114
  return "⚠️ No Q&A pairs generated."
115
 
116
+ print("✅ Final Q&A generated successfully.")
117
  return "\n\n".join(qa_pairs)
118
 
119
  except Exception as e:
120
+ print(f"🔥 Exception in generate_qa(): {e}")
121
  return f"❌ Error: {str(e)}"
122
 
123
 
 
124
  # ✅ Ask question using token (semantic similarity)
125
  def ask_question(token, question):
126
  try: