cstr commited on
Commit
a077d87
·
verified ·
1 Parent(s): 658e7c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -56
app.py CHANGED
@@ -244,7 +244,7 @@ def send_to_model_impl(prompt, model_selection, hf_model_choice, hf_custom_model
244
  with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as f:
245
  f.write(summary)
246
 
247
- return summary, [f.name]
248
  except Exception as e:
249
  error_msg = f"Error processing request: {str(e)}"
250
  logging.error(error_msg)
@@ -456,7 +456,7 @@ with gr.Blocks(css="""
456
  max_lines=50,
457
  show_copy_button=True
458
  )
459
- download_full_text = gr.Button("📥 Download Full Text")
460
 
461
  # Tab 2: Snippet Selection
462
  with gr.Tab("2️⃣ Snippet Selection"):
@@ -486,9 +486,8 @@ with gr.Blocks(css="""
486
  )
487
 
488
  with gr.Row():
489
- copy_prompt_button = gr.Button("📋 Copy Prompt")
490
- download_prompt = gr.Button("📥 Download Prompt")
491
- download_snippet = gr.Button("📥 Download Selected Snippet")
492
 
493
  # Tab 3: Model Processing
494
  with gr.Tab("3️⃣ Model Processing"):
@@ -552,8 +551,7 @@ with gr.Blocks(css="""
552
  )
553
 
554
  with gr.Row():
555
- copy_summary_button = gr.Button("📋 Copy Summary")
556
- download_summary = gr.Button("📥 Download Summary")
557
 
558
  # Hidden components for file handling
559
  download_files = gr.Files(label="📥 Downloads", visible=False)
@@ -675,7 +673,8 @@ with gr.Blocks(css="""
675
  formatted_text,
676
  snippets_list,
677
  gr.update(choices=update_snippet_choices(snippets_list), value="Snippet 1 of " + str(len(snippets_list))),
678
- [download_file]
 
679
  )
680
 
681
  except Exception as e:
@@ -690,29 +689,25 @@ with gr.Blocks(css="""
690
  None
691
  )
692
 
693
- def handle_snippet_selection(choice, snippets_list):
694
- """Handle snippet selection and update prompt"""
695
  if not snippets_list:
696
- return (
697
- "No snippets available.", # progress_status
698
- "", # generated_prompt
699
- None # download_files
700
- )
701
-
702
  try:
703
  idx = get_snippet_index(choice)
704
  selected_snippet = snippets_list[idx]
705
-
706
- # Create downloadable snippet
707
  with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as f:
708
  f.write(selected_snippet)
709
-
 
710
  return (
711
  f"Selected snippet {idx + 1}",
712
  selected_snippet,
713
- [f.name]
714
  )
715
-
716
  except Exception as e:
717
  error_msg = f"Error selecting snippet: {str(e)}"
718
  logging.error(error_msg)
@@ -737,8 +732,10 @@ with gr.Blocks(css="""
737
  # Save prompt for download
738
  with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as f:
739
  f.write(prompt)
 
740
 
741
- return "Prompt generated!", prompt, [f.name]
 
742
  except Exception as e:
743
  logging.error(f"Error generating prompt: {e}")
744
  return f"Error: {str(e)}", "", None
@@ -754,35 +751,20 @@ with gr.Blocks(css="""
754
  process_button.click(
755
  handle_pdf_process,
756
  inputs=[pdf_input, format_type, context_size],
757
- outputs=[ # List of outputs, not dict
758
- progress_status,
759
- processed_text,
760
- pdf_content,
761
- snippets,
762
- snippet_selector,
763
- download_files
764
- ]
765
  )
766
 
767
  generate_prompt_btn.click(
768
  handle_prompt_generation,
769
  inputs=[generated_prompt, custom_prompt, snippet_selector, snippets],
770
- outputs=[
771
- progress_status,
772
- generated_prompt,
773
- download_files
774
- ]
775
  )
776
 
777
  # Snippet handling
778
  snippet_selector.change(
779
  handle_snippet_selection,
780
  inputs=[snippet_selector, snippets],
781
- outputs=[
782
- progress_status,
783
- generated_prompt,
784
- download_files
785
- ]
786
  )
787
 
788
  # Model selection
@@ -809,18 +791,6 @@ with gr.Blocks(css="""
809
  outputs=[context_size]
810
  )
811
 
812
- # Download handlers
813
- copy_prompt_button.click(
814
- fn=None,
815
- _js=copy_text_js("generated_prompt"), # Use the helper function
816
- outputs=progress_status
817
- )
818
- copy_summary_button.click(
819
- fn=None,
820
- _js=copy_text_js("summary_output"), # Use the helper function
821
- outputs=progress_status
822
- )
823
-
824
  def download_file(content: str, prefix: str) -> List[str]:
825
  if not content:
826
  return []
@@ -854,10 +824,7 @@ with gr.Blocks(css="""
854
  groq_api_key,
855
  openai_api_key
856
  ],
857
- outputs=[
858
- summary_output,
859
- download_files
860
- ]
861
  )
862
 
863
  groq_refresh_btn.click(
 
244
  with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as f:
245
  f.write(summary)
246
 
247
+ return summary, download_file # Return the file for download_summary
248
  except Exception as e:
249
  error_msg = f"Error processing request: {str(e)}"
250
  logging.error(error_msg)
 
456
  max_lines=50,
457
  show_copy_button=True
458
  )
459
+ download_full_text = gr.File(label="📥 Download Full Text")
460
 
461
  # Tab 2: Snippet Selection
462
  with gr.Tab("2️⃣ Snippet Selection"):
 
486
  )
487
 
488
  with gr.Row():
489
+ download_prompt = gr.File(label="📥 Download Prompt")
490
+ download_snippet = gr.File(label="📥 Download Selected Snippet")
 
491
 
492
  # Tab 3: Model Processing
493
  with gr.Tab("3️⃣ Model Processing"):
 
551
  )
552
 
553
  with gr.Row():
554
+ download_summary = gr.File(label="📥 Download Summary")
 
555
 
556
  # Hidden components for file handling
557
  download_files = gr.Files(label="📥 Downloads", visible=False)
 
673
  formatted_text,
674
  snippets_list,
675
  gr.update(choices=update_snippet_choices(snippets_list), value="Snippet 1 of " + str(len(snippets_list))),
676
+ download_file # Return the file for download_full_text
677
+ #[download_file]
678
  )
679
 
680
  except Exception as e:
 
689
  None
690
  )
691
 
692
+ def handle_snippet_selection(choice, snippets_list): # Add download_snippet output
693
+ """Handle snippet selection, update prompt, and provide snippet download."""
694
  if not snippets_list:
695
+ return "No snippets available.", "", None # Return None for download
696
+
 
 
 
 
697
  try:
698
  idx = get_snippet_index(choice)
699
  selected_snippet = snippets_list[idx]
700
+
 
701
  with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as f:
702
  f.write(selected_snippet)
703
+ snippet_download_file = f.name # Store the file path
704
+
705
  return (
706
  f"Selected snippet {idx + 1}",
707
  selected_snippet,
708
+ snippet_download_file # Return file for download
709
  )
710
+
711
  except Exception as e:
712
  error_msg = f"Error selecting snippet: {str(e)}"
713
  logging.error(error_msg)
 
732
  # Save prompt for download
733
  with tempfile.NamedTemporaryFile(delete=False, mode='w', suffix='.txt') as f:
734
  f.write(prompt)
735
+ download_file = f.name
736
 
737
+ return "Prompt generated!", prompt, download_file # Return the file for download_prompt
738
+
739
  except Exception as e:
740
  logging.error(f"Error generating prompt: {e}")
741
  return f"Error: {str(e)}", "", None
 
751
  process_button.click(
752
  handle_pdf_process,
753
  inputs=[pdf_input, format_type, context_size],
754
+ outputs=[progress_status, processed_text, pdf_content, snippets, snippet_selector, download_full_text]
 
 
 
 
 
 
 
755
  )
756
 
757
  generate_prompt_btn.click(
758
  handle_prompt_generation,
759
  inputs=[generated_prompt, custom_prompt, snippet_selector, snippets],
760
+ outputs=[progress_status, generated_prompt, download_prompt]
 
 
 
 
761
  )
762
 
763
  # Snippet handling
764
  snippet_selector.change(
765
  handle_snippet_selection,
766
  inputs=[snippet_selector, snippets],
767
+ outputs=[progress_status, generated_prompt, download_snippet] # Connect download_snippet
 
 
 
 
768
  )
769
 
770
  # Model selection
 
791
  outputs=[context_size]
792
  )
793
 
 
 
 
 
 
 
 
 
 
 
 
 
794
  def download_file(content: str, prefix: str) -> List[str]:
795
  if not content:
796
  return []
 
824
  groq_api_key,
825
  openai_api_key
826
  ],
827
+ outputs=[summary_output, download_summary]
 
 
 
828
  )
829
 
830
  groq_refresh_btn.click(