deepkansara-123 commited on
Commit
e6b7fe9
Β·
verified Β·
1 Parent(s): 9578afc

Upload 7 files

Browse files
Files changed (1) hide show
  1. app.py +35 -15
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
- # βœ… Updated version – supports multiple PDF files
 
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, token_id, content FROM token_data")
110
  rows = cursor.fetchall()
111
 
112
  if not rows:
113
  return "ℹ️ No PDFs uploaded yet."
114
 
115
- result = ""
116
- for filename, token, content in rows:
117
- preview = content[:200].replace("\n", " ") + "..." if len(content) > 200 else content
118
- result += f"πŸ“„ **Filename:** {filename}\nπŸ”‘ Token: `{token}`\nπŸ“ Preview: {preview}\n\n---\n"
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.Tab("πŸ“€ 1. Upload PDF"):
135
- gr.Markdown("### πŸ—‚ Upload a PDF File")
136
- file = gr.File(label="Upload one or more PDFs", file_types=[".pdf"], file_count="multiple")
137
-
138
- upload_out = gr.Textbox(label="Upload Result", interactive=False)
139
- file.change(fn=upload_pdf, inputs=file, outputs=upload_out)
 
 
 
 
 
 
 
 
 
 
 
 
 
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 (with Preview Snippets)")
 
156
  list_btn = gr.Button("πŸ”„ Refresh List")
 
157
  pdf_list_output = gr.Markdown()
158
- list_btn.click(fn=list_uploaded_pdfs, outputs=pdf_list_output)
 
 
 
 
 
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)