deepkansara-123 commited on
Commit
8593988
Β·
verified Β·
1 Parent(s): 6f8c89a

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -12
app.py CHANGED
@@ -51,11 +51,19 @@ def upload_pdf(files):
51
 
52
 
53
 
54
- # Load QG and QA once
55
- qgen = QGenerator()
56
- qa_model = pipeline("text2text-generation", model="google/flan-t5-base")
57
-
 
 
 
 
 
 
 
58
 
 
59
  def generate_qa(filename):
60
  try:
61
  if not filename:
@@ -64,7 +72,7 @@ def generate_qa(filename):
64
  # Load chunk_data from DB
65
  with sqlite3.connect("my_database.db") as conn:
66
  cursor = conn.cursor()
67
- cursor.execute("SELECT chunk_data FROM token_data WHERE filename = ?", (filename,))
68
  row = cursor.fetchone()
69
 
70
  if not row:
@@ -78,7 +86,7 @@ def generate_qa(filename):
78
  if not questions:
79
  continue
80
 
81
- for question in questions[:2]: # Max 2 Qs per chunk
82
  prompt = f"Context: {chunk}\n\nQuestion: {question}\n\nAnswer:"
83
  try:
84
  result = qa_model(prompt, max_length=256, do_sample=False)
@@ -97,7 +105,6 @@ def generate_qa(filename):
97
  return f"❌ Error: {str(e)}"
98
 
99
 
100
-
101
  # βœ… Ask question using token (semantic similarity)
102
  def ask_question(token, question):
103
  try:
@@ -151,6 +158,7 @@ def list_uploaded_pdfs():
151
  return f"❌ Error: {str(e)}"
152
 
153
 
 
154
  # βœ… Gradio UI
155
  with gr.Blocks(theme="default") as demo:
156
  gr.Markdown(
@@ -169,11 +177,16 @@ with gr.Blocks(theme="default") as demo:
169
  upload_out = gr.Textbox(label="Upload Result", interactive=False)
170
  file.change(fn=upload_pdf, inputs=file, outputs=upload_out)
171
 
172
- with gr.Tab("🧠 2. Generate Questions & Answers"):
173
- gr.Markdown("### πŸ€– Generate Q&A from your PDF")
174
- fname = gr.Textbox(label="Enter uploaded filename", placeholder="example.pdf")
175
- qa_result = gr.Textbox(label="Generated Q&A", lines=15, interactive=False)
176
- gr.Button("πŸš€ Generate Q&A").click(fn=generate_qa, inputs=fname, outputs=qa_result)
 
 
 
 
 
177
 
178
  with gr.Tab("❓ 3. Ask a Question"):
179
  gr.Markdown("### πŸ’¬ Ask a question based on uploaded PDF")
 
51
 
52
 
53
 
54
+ # βœ… Helper function to get uploaded filenames from DB
55
+ def get_filenames():
56
+ try:
57
+ with sqlite3.connect("my_database.db") as conn:
58
+ cursor = conn.cursor()
59
+ cursor.execute("SELECT DISTINCT filename FROM token_data")
60
+ rows = cursor.fetchall()
61
+ return [row[0] for row in rows] if rows else []
62
+ except Exception as e:
63
+ print("Error fetching filenames:", e)
64
+ return []
65
 
66
+ # βœ… Main function to generate Q&A pairs from selected filename
67
  def generate_qa(filename):
68
  try:
69
  if not filename:
 
72
  # Load chunk_data from DB
73
  with sqlite3.connect("my_database.db") as conn:
74
  cursor = conn.cursor()
75
+ cursor.execute("SELECT chunk_data FROM token_data WHERE LOWER(filename) = LOWER(?)", (filename,))
76
  row = cursor.fetchone()
77
 
78
  if not row:
 
86
  if not questions:
87
  continue
88
 
89
+ for question in questions[:2]: # Max 2 questions per chunk
90
  prompt = f"Context: {chunk}\n\nQuestion: {question}\n\nAnswer:"
91
  try:
92
  result = qa_model(prompt, max_length=256, do_sample=False)
 
105
  return f"❌ Error: {str(e)}"
106
 
107
 
 
108
  # βœ… Ask question using token (semantic similarity)
109
  def ask_question(token, question):
110
  try:
 
158
  return f"❌ Error: {str(e)}"
159
 
160
 
161
+
162
  # βœ… Gradio UI
163
  with gr.Blocks(theme="default") as demo:
164
  gr.Markdown(
 
177
  upload_out = gr.Textbox(label="Upload Result", interactive=False)
178
  file.change(fn=upload_pdf, inputs=file, outputs=upload_out)
179
 
180
+
181
+
182
+ with gr.Blocks(title="PDF Q&A Generator") as demo:
183
+ with gr.Tab("🧠 2. Generate Questions & Answers"):
184
+ gr.Markdown("### πŸ€– Generate Q&A from your uploaded PDF")
185
+
186
+ fname = gr.Dropdown(label="Choose uploaded PDF", choices=get_filenames)
187
+ qa_result = gr.Textbox(label="Generated Q&A", lines=15, interactive=False)
188
+ gr.Button("πŸš€ Generate Q&A").click(fn=generate_qa, inputs=fname, outputs=qa_result)
189
+
190
 
191
  with gr.Tab("❓ 3. Ask a Question"):
192
  gr.Markdown("### πŸ’¬ Ask a question based on uploaded PDF")