Mahdi Naser Moghadasi commited on
Commit
d2eb486
Β·
1 Parent(s): 35f8f30

πŸ“ Simplify file management: Add multi-file upload support, remove dropdown selector, clean HTML display

Browse files
Files changed (1) hide show
  1. app.py +60 -49
app.py CHANGED
@@ -2740,15 +2740,36 @@ def update_combined_context():
2740
  mathmind_pdf_context = "\n".join(context_parts)
2741
 
2742
  def generate_context_display():
2743
- """Generate display text for current PDF context"""
2744
  if not uploaded_pdfs:
2745
- return "No PDF context loaded"
 
 
2746
 
2747
- display_parts = []
2748
  for i, pdf in enumerate(uploaded_pdfs, 1):
2749
- display_parts.append(f"**{i}. {pdf['name']}** (Added: {pdf['timestamp']})\n{pdf['summary'][:150]}...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2750
 
2751
- return f"πŸ“š **Loaded Documents ({len(uploaded_pdfs)}):**\n\n" + "\n\n".join(display_parts)
2752
 
2753
  def remove_pdf_by_name(file_name: str):
2754
  """Remove a specific PDF from context"""
@@ -4588,9 +4609,10 @@ with gr.Blocks(
4588
  """)
4589
 
4590
  mathmind_pdf_upload = gr.File(
4591
- label="πŸ“„ Add Math PDF",
4592
  file_types=[".pdf"],
4593
- type="binary"
 
4594
  )
4595
 
4596
  mathmind_pdf_status = gr.Textbox(
@@ -4599,22 +4621,11 @@ with gr.Blocks(
4599
  lines=3
4600
  )
4601
 
4602
- mathmind_pdf_context = gr.Textbox(
4603
  label="πŸ“– Loaded Documents",
4604
- interactive=False,
4605
- lines=10,
4606
- placeholder="No PDF context loaded"
4607
  )
4608
 
4609
- with gr.Row():
4610
- mathmind_file_selector = gr.Dropdown(
4611
- label="Select file to remove",
4612
- choices=[],
4613
- interactive=True,
4614
- scale=2
4615
- )
4616
- mathmind_remove_selected = gr.Button("πŸ—‘οΈ Remove Selected", variant="secondary", scale=1)
4617
-
4618
  mathmind_remove_all_pdf = gr.Button("πŸ—‘οΈ Remove All Documents", variant="secondary")
4619
 
4620
  # Quick Actions
@@ -4649,47 +4660,47 @@ with gr.Blocks(
4649
  )
4650
 
4651
  # PDF management functions
4652
- def update_file_dropdown():
4653
- """Update the file selector dropdown with current files"""
4654
- return gr.Dropdown(choices=[pdf["name"] for pdf in uploaded_pdfs])
4655
-
4656
- def handle_pdf_upload(pdf_file):
4657
- """Handle PDF upload and update dropdown"""
4658
- status, context = update_pdf_context(pdf_file)
4659
- new_dropdown = gr.Dropdown(choices=[pdf["name"] for pdf in uploaded_pdfs])
4660
- return status, context, new_dropdown
4661
-
4662
- def handle_remove_selected(selected_file):
4663
- """Remove selected file from context"""
4664
- if not selected_file:
4665
- return "⚠️ No file selected", generate_context_display(), gr.Dropdown(choices=[pdf["name"] for pdf in uploaded_pdfs])
 
 
 
4666
 
4667
- status, context = remove_pdf_by_name(selected_file)
4668
- new_dropdown = gr.Dropdown(choices=[pdf["name"] for pdf in uploaded_pdfs])
4669
- return status, context, new_dropdown
 
 
 
 
4670
 
4671
  def handle_remove_all():
4672
  """Remove all files from context"""
4673
- status, context = remove_pdf_context()
4674
- new_dropdown = gr.Dropdown(choices=[])
4675
- return status, context, new_dropdown
4676
 
4677
  # Event handlers
4678
  mathmind_pdf_upload.change(
4679
- fn=handle_pdf_upload,
4680
  inputs=[mathmind_pdf_upload],
4681
- outputs=[mathmind_pdf_status, mathmind_pdf_context, mathmind_file_selector]
4682
- )
4683
-
4684
- mathmind_remove_selected.click(
4685
- fn=handle_remove_selected,
4686
- inputs=[mathmind_file_selector],
4687
- outputs=[mathmind_pdf_status, mathmind_pdf_context, mathmind_file_selector]
4688
  )
4689
 
4690
  mathmind_remove_all_pdf.click(
4691
  fn=handle_remove_all,
4692
- outputs=[mathmind_pdf_status, mathmind_pdf_context, mathmind_file_selector]
4693
  )
4694
 
4695
  # Example Questions Handler
 
2740
  mathmind_pdf_context = "\n".join(context_parts)
2741
 
2742
  def generate_context_display():
2743
+ """Generate HTML display for current PDF context"""
2744
  if not uploaded_pdfs:
2745
+ return "<p style='color: #666; font-style: italic;'>No PDF context loaded</p>"
2746
+
2747
+ html_parts = [f"<h4 style='color: #667eea; margin-bottom: 15px;'>πŸ“š Loaded Documents ({len(uploaded_pdfs)}):</h4>"]
2748
 
 
2749
  for i, pdf in enumerate(uploaded_pdfs, 1):
2750
+ # Create a clean card for each document
2751
+ card_html = f"""
2752
+ <div style="border: 1px solid #e2e8f0; border-radius: 8px; padding: 15px; margin-bottom: 10px; background: #f8f9fa;">
2753
+ <h5 style="color: #2d3748; margin: 0 0 8px 0; font-weight: bold;">πŸ“„ {i}. {pdf['name']}</h5>
2754
+ <p style="color: #4a5568; margin: 0 0 8px 0; font-size: 0.9rem;">⏰ Added: {pdf['timestamp']}</p>
2755
+ <div style="color: #666; font-size: 0.85rem; line-height: 1.4; background: #fff; padding: 10px; border-radius: 4px;">
2756
+ {pdf['summary'][:300]}{'...' if len(pdf['summary']) > 300 else ''}
2757
+ </div>
2758
+ </div>
2759
+ """
2760
+ html_parts.append(card_html)
2761
+
2762
+ # Add instruction for removing files
2763
+ if len(uploaded_pdfs) > 0:
2764
+ html_parts.append("""
2765
+ <div style="margin-top: 15px; padding: 10px; background: #e6f3ff; border-radius: 6px; border-left: 3px solid #3182ce;">
2766
+ <p style="margin: 0; color: #2c5aa0; font-size: 0.9rem;">
2767
+ πŸ’‘ <strong>Tip:</strong> To remove individual documents, you can upload new ones to replace them, or use "Remove All Documents" below to clear everything.
2768
+ </p>
2769
+ </div>
2770
+ """)
2771
 
2772
+ return "".join(html_parts)
2773
 
2774
  def remove_pdf_by_name(file_name: str):
2775
  """Remove a specific PDF from context"""
 
4609
  """)
4610
 
4611
  mathmind_pdf_upload = gr.File(
4612
+ label="πŸ“„ Add Math PDFs (Multiple Selection Supported)",
4613
  file_types=[".pdf"],
4614
+ type="binary",
4615
+ file_count="multiple"
4616
  )
4617
 
4618
  mathmind_pdf_status = gr.Textbox(
 
4621
  lines=3
4622
  )
4623
 
4624
+ mathmind_pdf_context = gr.HTML(
4625
  label="πŸ“– Loaded Documents",
4626
+ value="<p style='color: #666; font-style: italic;'>No PDF context loaded</p>"
 
 
4627
  )
4628
 
 
 
 
 
 
 
 
 
 
4629
  mathmind_remove_all_pdf = gr.Button("πŸ—‘οΈ Remove All Documents", variant="secondary")
4630
 
4631
  # Quick Actions
 
4660
  )
4661
 
4662
  # PDF management functions
4663
+ def handle_multiple_pdf_upload(pdf_files):
4664
+ """Handle multiple PDF uploads"""
4665
+ if not pdf_files:
4666
+ return "No files uploaded", generate_context_display()
4667
+
4668
+ # Handle both single file and multiple files
4669
+ files_to_process = pdf_files if isinstance(pdf_files, list) else [pdf_files]
4670
+
4671
+ status_messages = []
4672
+ processed_count = 0
4673
+
4674
+ for pdf_file in files_to_process:
4675
+ if pdf_file is not None:
4676
+ status, _ = update_pdf_context(pdf_file)
4677
+ if "successfully processed" in status:
4678
+ processed_count += 1
4679
+ status_messages.append(status.split('\n')[0]) # Get first line only
4680
 
4681
+ # Create combined status message
4682
+ if processed_count > 0:
4683
+ combined_status = f"βœ… {processed_count} PDF(s) successfully processed!\n" + "\n".join(status_messages[-3:]) # Show last 3 messages
4684
+ else:
4685
+ combined_status = "❌ No files were successfully processed:\n" + "\n".join(status_messages[-3:])
4686
+
4687
+ return combined_status, generate_context_display()
4688
 
4689
  def handle_remove_all():
4690
  """Remove all files from context"""
4691
+ status, _ = remove_pdf_context()
4692
+ return status, generate_context_display()
 
4693
 
4694
  # Event handlers
4695
  mathmind_pdf_upload.change(
4696
+ fn=handle_multiple_pdf_upload,
4697
  inputs=[mathmind_pdf_upload],
4698
+ outputs=[mathmind_pdf_status, mathmind_pdf_context]
 
 
 
 
 
 
4699
  )
4700
 
4701
  mathmind_remove_all_pdf.click(
4702
  fn=handle_remove_all,
4703
+ outputs=[mathmind_pdf_status, mathmind_pdf_context]
4704
  )
4705
 
4706
  # Example Questions Handler