ApsidalSolid4 commited on
Commit
be06247
·
verified ·
1 Parent(s): 23ffcc3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -63
app.py CHANGED
@@ -770,7 +770,7 @@ def analyze_text(text: str, mode: str, classifier: TextClassifier) -> tuple:
770
  # Initialize the classifier globally
771
  classifier = TextClassifier()
772
 
773
- # Create Gradio interface with a small file upload button next to the radio buttons
774
  def create_interface():
775
  # Custom CSS for the interface
776
  css = """
@@ -780,34 +780,28 @@ def create_interface():
780
  color: white !important;
781
  }
782
 
783
- /* Custom styling for the radio group container */
784
- .radio-group-container {
785
- display: flex;
786
- align-items: center;
787
- }
788
-
789
- /* Paperclip button styling */
790
- .paperclip-btn {
791
  cursor: pointer;
792
- background: none;
793
- border: none;
794
- font-size: 18px;
795
- margin-left: 10px;
796
- padding: 0;
797
- width: 24px;
798
- height: 24px;
799
  display: inline-flex;
800
  align-items: center;
801
  justify-content: center;
802
  }
803
 
804
- .paperclip-btn:hover {
805
- opacity: 0.7;
806
  }
807
 
808
- /* Hide the actual file upload element but make it accessible */
809
- #file-upload {
810
- display: none;
811
  }
812
  """
813
 
@@ -825,13 +819,13 @@ def create_interface():
825
  label="Input Text"
826
  )
827
 
828
- # Analysis Mode label
829
  gr.Markdown("Analysis Mode")
830
  gr.Markdown("Quick mode for faster analysis. Detailed mode for sentence-level analysis.")
831
 
832
- # Create a row for radio buttons and paperclip
833
- with gr.Row(elem_classes=["radio-group-container"]):
834
- # Radio buttons for analysis mode selection
835
  mode_selection = gr.Radio(
836
  choices=["quick", "detailed"],
837
  value="quick",
@@ -839,15 +833,21 @@ def create_interface():
839
  show_label=False
840
  )
841
 
842
- # File upload component (will be triggered by paperclip)
843
  file_upload = gr.File(
844
  file_types=["image", "pdf", "doc", "docx"],
845
  type="binary",
846
- elem_id="file-upload",
847
- visible=True, # Set to visible but we'll hide it with CSS
 
 
 
 
 
 
848
  )
849
 
850
- # Analyze button with custom styling
851
  analyze_btn = gr.Button("Analyze Text", elem_id="analyze-btn")
852
 
853
  # Right column - Results
@@ -864,45 +864,20 @@ def create_interface():
864
  outputs=[output_html, output_sentences, output_result]
865
  )
866
 
867
- # 2. File upload change event
 
 
 
 
 
 
 
 
868
  file_upload.change(
869
  fn=handle_file_upload_and_analyze,
870
  inputs=[file_upload, mode_selection],
871
  outputs=[output_html, output_sentences, output_result]
872
  )
873
-
874
- # Add custom JavaScript for the paperclip functionality
875
- demo.load(js="""
876
- function setupPaperclip() {
877
- // Get the file upload element and its parent
878
- const fileUpload = document.getElementById('file-upload');
879
- const radioGroup = document.querySelector('.radio-group-container');
880
-
881
- // Create paperclip button
882
- const paperclip = document.createElement('button');
883
- paperclip.innerHTML = '📎';
884
- paperclip.className = 'paperclip-btn';
885
- paperclip.title = 'Upload a document';
886
-
887
- // Insert paperclip button after the radio group
888
- radioGroup.appendChild(paperclip);
889
-
890
- // When paperclip is clicked, trigger the file upload dialog
891
- paperclip.addEventListener('click', function() {
892
- fileUpload.querySelector('input[type="file"]').click();
893
- });
894
-
895
- // Make the file upload container itself invisible (but still functional)
896
- fileUpload.style.display = 'none';
897
- }
898
-
899
- // Run setup when page loads
900
- if (document.readyState === 'complete') {
901
- setupPaperclip();
902
- } else {
903
- window.addEventListener('load', setupPaperclip);
904
- }
905
- """)
906
 
907
  return demo
908
 
 
770
  # Initialize the classifier globally
771
  classifier = TextClassifier()
772
 
773
+ # Create Gradio interface with a clear "Upload Document" button
774
  def create_interface():
775
  # Custom CSS for the interface
776
  css = """
 
780
  color: white !important;
781
  }
782
 
783
+ /* Clean styling for the upload button */
784
+ .upload-document-btn {
785
+ margin-left: 15px;
786
+ height: 38px;
787
+ padding: 0 15px;
788
+ border-radius: 4px;
789
+ background-color: #f0f0f0;
790
+ border: 1px solid #d9d9d9;
791
  cursor: pointer;
792
+ font-size: 14px;
 
 
 
 
 
 
793
  display: inline-flex;
794
  align-items: center;
795
  justify-content: center;
796
  }
797
 
798
+ .upload-document-btn:hover {
799
+ background-color: #e0e0e0;
800
  }
801
 
802
+ /* Hide the standard file upload component */
803
+ #hidden-file-upload {
804
+ display: none !important;
805
  }
806
  """
807
 
 
819
  label="Input Text"
820
  )
821
 
822
+ # Analysis Mode section
823
  gr.Markdown("Analysis Mode")
824
  gr.Markdown("Quick mode for faster analysis. Detailed mode for sentence-level analysis.")
825
 
826
+ # Create a row for radio buttons and upload button
827
+ with gr.Row():
828
+ # Radio buttons for analysis mode
829
  mode_selection = gr.Radio(
830
  choices=["quick", "detailed"],
831
  value="quick",
 
833
  show_label=False
834
  )
835
 
836
+ # Hidden file upload component
837
  file_upload = gr.File(
838
  file_types=["image", "pdf", "doc", "docx"],
839
  type="binary",
840
+ elem_id="hidden-file-upload",
841
+ visible=False
842
+ )
843
+
844
+ # Visible upload button
845
+ upload_btn = gr.Button(
846
+ "Upload Document",
847
+ elem_classes=["upload-document-btn"]
848
  )
849
 
850
+ # Analyze button
851
  analyze_btn = gr.Button("Analyze Text", elem_id="analyze-btn")
852
 
853
  # Right column - Results
 
864
  outputs=[output_html, output_sentences, output_result]
865
  )
866
 
867
+ # 2. Upload button click - make file upload visible
868
+ upload_btn.click(
869
+ fn=lambda: gr.update(visible=True),
870
+ inputs=None,
871
+ outputs=file_upload,
872
+ _js="() => {document.querySelector('#hidden-file-upload input[type=file]').click(); return {}}"
873
+ )
874
+
875
+ # 3. File upload change event
876
  file_upload.change(
877
  fn=handle_file_upload_and_analyze,
878
  inputs=[file_upload, mode_selection],
879
  outputs=[output_html, output_sentences, output_result]
880
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
881
 
882
  return demo
883