Spaces:
Sleeping
Sleeping
Upload 7 files
Browse files
app.py
CHANGED
|
@@ -24,7 +24,8 @@ model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-base")
|
|
| 24 |
qa_model = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
|
| 25 |
|
| 26 |
# β
Upload and process PDF
|
| 27 |
-
|
|
|
|
| 28 |
def upload_pdf(files):
|
| 29 |
try:
|
| 30 |
messages = []
|
|
@@ -101,21 +102,20 @@ def ask_question(token, question):
|
|
| 101 |
except Exception as e:
|
| 102 |
return f"β Error: {str(e)}"
|
| 103 |
|
| 104 |
-
# β
View uploaded PDFs (Gradio Tab)
|
| 105 |
def list_uploaded_pdfs():
|
| 106 |
try:
|
| 107 |
with sqlite3.connect("my_database.db") as conn:
|
| 108 |
cursor = conn.cursor()
|
| 109 |
-
cursor.execute("SELECT filename
|
| 110 |
rows = cursor.fetchall()
|
| 111 |
|
| 112 |
if not rows:
|
| 113 |
return "βΉοΈ No PDFs uploaded yet."
|
| 114 |
|
| 115 |
-
result = ""
|
| 116 |
-
for
|
| 117 |
-
|
| 118 |
-
|
| 119 |
return result
|
| 120 |
except Exception as e:
|
| 121 |
return f"β Error: {str(e)}"
|
|
@@ -131,12 +131,25 @@ with gr.Blocks(theme="default") as demo:
|
|
| 131 |
"""
|
| 132 |
)
|
| 133 |
|
| 134 |
-
with gr.
|
| 135 |
-
gr.
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
with gr.Tab("π§ 2. Generate Questions & Answers"):
|
| 142 |
gr.Markdown("### π€ Generate Q&A from your PDF")
|
|
@@ -152,10 +165,17 @@ with gr.Blocks(theme="default") as demo:
|
|
| 152 |
gr.Button("π― Get Answer").click(fn=ask_question, inputs=[token_box, question_box], outputs=answer_result)
|
| 153 |
|
| 154 |
with gr.Tab("π 4. View Uploaded PDFs"):
|
| 155 |
-
gr.Markdown("### π Uploaded PDF List
|
|
|
|
| 156 |
list_btn = gr.Button("π Refresh List")
|
|
|
|
| 157 |
pdf_list_output = gr.Markdown()
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
if __name__ == "__main__":
|
| 161 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 24 |
qa_model = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
|
| 25 |
|
| 26 |
# β
Upload and process PDF
|
| 27 |
+
|
| 28 |
+
# β
Multiple file upload logic
|
| 29 |
def upload_pdf(files):
|
| 30 |
try:
|
| 31 |
messages = []
|
|
|
|
| 102 |
except Exception as e:
|
| 103 |
return f"β Error: {str(e)}"
|
| 104 |
|
|
|
|
| 105 |
def list_uploaded_pdfs():
|
| 106 |
try:
|
| 107 |
with sqlite3.connect("my_database.db") as conn:
|
| 108 |
cursor = conn.cursor()
|
| 109 |
+
cursor.execute("SELECT filename FROM token_data")
|
| 110 |
rows = cursor.fetchall()
|
| 111 |
|
| 112 |
if not rows:
|
| 113 |
return "βΉοΈ No PDFs uploaded yet."
|
| 114 |
|
| 115 |
+
result = "π **Uploaded PDF Files:**\n\n"
|
| 116 |
+
for i, (filename,) in enumerate(rows, start=1):
|
| 117 |
+
result += f"{i}. {filename}\n"
|
| 118 |
+
|
| 119 |
return result
|
| 120 |
except Exception as e:
|
| 121 |
return f"β Error: {str(e)}"
|
|
|
|
| 131 |
"""
|
| 132 |
)
|
| 133 |
|
| 134 |
+
with gr.Blocks(title="PDF Uploader") as demo:
|
| 135 |
+
with gr.Tab("π€ Upload PDFs"):
|
| 136 |
+
gr.Markdown("### Select and upload one or more PDF files")
|
| 137 |
+
|
| 138 |
+
file_input = gr.File(
|
| 139 |
+
label="Upload PDFs",
|
| 140 |
+
file_types=[".pdf"],
|
| 141 |
+
file_count="multiple" # β
enables multi-select in UI
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
upload_output = gr.Textbox(
|
| 145 |
+
label="Upload Results",
|
| 146 |
+
lines=10,
|
| 147 |
+
interactive=False
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
file_input.change(fn=upload_pdf, inputs=file_input, outputs=upload_output)
|
| 151 |
+
|
| 152 |
+
|
| 153 |
|
| 154 |
with gr.Tab("π§ 2. Generate Questions & Answers"):
|
| 155 |
gr.Markdown("### π€ Generate Q&A from your PDF")
|
|
|
|
| 165 |
gr.Button("π― Get Answer").click(fn=ask_question, inputs=[token_box, question_box], outputs=answer_result)
|
| 166 |
|
| 167 |
with gr.Tab("π 4. View Uploaded PDFs"):
|
| 168 |
+
gr.Markdown("### π Uploaded PDF List")
|
| 169 |
+
|
| 170 |
list_btn = gr.Button("π Refresh List")
|
| 171 |
+
|
| 172 |
pdf_list_output = gr.Markdown()
|
| 173 |
+
|
| 174 |
+
list_btn.click(
|
| 175 |
+
fn=list_uploaded_pdfs,
|
| 176 |
+
inputs=[],
|
| 177 |
+
outputs=pdf_list_output
|
| 178 |
+
)
|
| 179 |
|
| 180 |
if __name__ == "__main__":
|
| 181 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|