Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -51,11 +51,19 @@ def upload_pdf(files):
|
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
| 56 |
-
|
| 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
|
| 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 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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")
|