Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -51,28 +51,20 @@ def upload_pdf(files):
|
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 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:
|
| 70 |
-
return "β οΈ
|
| 71 |
|
| 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
|
| 76 |
row = cursor.fetchone()
|
| 77 |
|
| 78 |
if not row:
|
|
@@ -86,11 +78,16 @@ def generate_qa(filename):
|
|
| 86 |
if not questions:
|
| 87 |
continue
|
| 88 |
|
| 89 |
-
for question in questions[:2]: # Max 2
|
| 90 |
prompt = f"Context: {chunk}\n\nQuestion: {question}\n\nAnswer:"
|
| 91 |
try:
|
| 92 |
result = qa_model(prompt, max_length=256, do_sample=False)
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
qa_pairs.append(f"Q: {question}\nA: {answer}")
|
| 95 |
except Exception as e:
|
| 96 |
print(f"QA model failed: {e}")
|
|
@@ -104,7 +101,6 @@ def generate_qa(filename):
|
|
| 104 |
except Exception as e:
|
| 105 |
return f"β Error: {str(e)}"
|
| 106 |
|
| 107 |
-
|
| 108 |
# β
Ask question using token (semantic similarity)
|
| 109 |
def ask_question(token, question):
|
| 110 |
try:
|
|
@@ -177,16 +173,12 @@ with gr.Blocks(theme="default") as demo:
|
|
| 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 |
-
|
| 183 |
-
|
| 184 |
-
|
| 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")
|
|
|
|
| 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:
|
| 62 |
+
return "β οΈ Please select a filename."
|
| 63 |
|
| 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 |
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)
|
| 85 |
+
if isinstance(result, list) and "generated_text" in result[0]:
|
| 86 |
+
answer = result[0]["generated_text"].strip()
|
| 87 |
+
elif isinstance(result, dict) and "answer" in result:
|
| 88 |
+
answer = result["answer"].strip()
|
| 89 |
+
else:
|
| 90 |
+
answer = "N/A"
|
| 91 |
qa_pairs.append(f"Q: {question}\nA: {answer}")
|
| 92 |
except Exception as e:
|
| 93 |
print(f"QA model failed: {e}")
|
|
|
|
| 101 |
except Exception as e:
|
| 102 |
return f"β Error: {str(e)}"
|
| 103 |
|
|
|
|
| 104 |
# β
Ask question using token (semantic similarity)
|
| 105 |
def ask_question(token, question):
|
| 106 |
try:
|
|
|
|
| 173 |
upload_out = gr.Textbox(label="Upload Result", interactive=False)
|
| 174 |
file.change(fn=upload_pdf, inputs=file, outputs=upload_out)
|
| 175 |
|
| 176 |
+
with gr.Blocks(title="PDF Q&A Generator") as demo:
|
| 177 |
+
with gr.Tab("π§ 2. Generate Questions & Answers"):
|
| 178 |
+
gr.Markdown("### π€ Generate Questions and Answers from Uploaded PDF")
|
| 179 |
+
fname = gr.Textbox(label="π Enter Uploaded Filename", placeholder="example.pdf")
|
| 180 |
+
output_box = gr.Textbox(label="π Generated Q&A", lines=15, interactive=False)
|
| 181 |
+
gr.Button("π Generate Q&A").click(fn=generate_qa, inputs=fname, outputs=output_box)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
with gr.Tab("β 3. Ask a Question"):
|
| 184 |
gr.Markdown("### π¬ Ask a question based on uploaded PDF")
|