ApsidalSolid4 commited on
Commit
d00b248
·
verified ·
1 Parent(s): 3a9ac67

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -95
app.py CHANGED
@@ -493,7 +493,7 @@ class TextClassifier:
493
  }
494
 
495
  # Function to handle file upload, OCR processing, and text analysis
496
- def handle_file_upload_and_analyze(file_obj, mode: str, classifier) -> tuple:
497
  """
498
  Handle file upload, OCR processing, and text analysis
499
  """
@@ -772,13 +772,39 @@ classifier = TextClassifier()
772
 
773
  # Create Gradio interface with a file upload button matched to the radio buttons
774
  def create_interface():
775
- # Simplified CSS - no longer trying to style the file upload component
776
  css = """
777
  #analyze-btn {
778
  background-color: #FF8C00 !important;
779
  border-color: #FF8C00 !important;
780
  color: white !important;
781
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
782
  """
783
 
784
  with gr.Blocks(css=css, title="AI Text Detector") as demo:
@@ -799,7 +825,7 @@ def create_interface():
799
  gr.Markdown("Analysis Mode")
800
  gr.Markdown("Quick mode for faster analysis. Detailed mode for sentence-level analysis.")
801
 
802
- # Create layout with two separate rows
803
  with gr.Row():
804
  mode_selection = gr.Radio(
805
  choices=["quick", "detailed"],
@@ -807,56 +833,14 @@ def create_interface():
807
  label="",
808
  show_label=False
809
  )
810
-
811
- # Create a separate HTML component for custom file upload
812
- file_html = gr.HTML("""
813
- <div style="margin-top: 10px; margin-bottom: 15px;">
814
- <label for="custom-file-upload"
815
- style="display: inline-block;
816
- padding: 8px 12px;
817
- background-color: #f0f0f0;
818
- border: 1px solid #d9d9d9;
819
- border-radius: 4px;
820
- cursor: pointer;
821
- font-size: 14px;
822
- width: 150px;
823
- text-align: center;">
824
- Upload Document
825
- </label>
826
- <input type="file"
827
- id="custom-file-upload"
828
- style="display: none;"
829
- onchange="uploadFile(this)">
830
- </div>
831
- <script>
832
- function uploadFile(input) {
833
- if (input.files && input.files[0]) {
834
- // Create a visible element to show the file name
835
- var fileNameElem = document.createElement('span');
836
- fileNameElem.textContent = input.files[0].name;
837
- fileNameElem.style.marginLeft = '10px';
838
- input.parentNode.appendChild(fileNameElem);
839
-
840
- // Get the file data
841
- var reader = new FileReader();
842
- reader.onload = function(e) {
843
- // Here we'd normally send this to the backend
844
- // For now, we'll just log it
845
- console.log("File loaded");
846
- };
847
- reader.readAsDataURL(input.files[0]);
848
- }
849
- }
850
- </script>
851
- """)
852
-
853
- # Hidden file component for actual upload (to be triggered by custom HTML)
854
- file_upload = gr.File(
855
- file_types=["image", "pdf", "doc", "docx"],
856
- type="binary",
857
- label="",
858
- visible=False # Hide the actual component
859
- )
860
 
861
  # Analyze button
862
  analyze_btn = gr.Button("Analyze Text", elem_id="analyze-btn")
@@ -874,53 +858,15 @@ def create_interface():
874
  outputs=[output_html, output_sentences, output_result]
875
  )
876
 
877
- # Keep the file upload handler (though it won't be directly visible)
878
- file_upload.change(
879
- fn=handle_file_upload_and_analyze,
880
  inputs=[file_upload, mode_selection],
881
  outputs=[output_html, output_sentences, output_result]
882
  )
883
-
884
- # JavaScript to connect custom button to the hidden file input
885
- demo.load(fn=None, inputs=None, outputs=None, _js="""
886
- function() {
887
- // Connect our custom file input to the hidden gradio component
888
- document.getElementById('custom-file-upload').addEventListener('change', function(e) {
889
- // Find the hidden file input in the Gradio UI
890
- const hiddenFileInputs = document.querySelectorAll('input[type="file"]');
891
- for (let input of hiddenFileInputs) {
892
- if (input.id !== 'custom-file-upload' && input.style.display !== 'none') {
893
- // Clone the FileList from our custom input to the hidden one
894
- const dataTransfer = new DataTransfer();
895
- for (let file of this.files) {
896
- dataTransfer.items.add(file);
897
- }
898
- input.files = dataTransfer.files;
899
-
900
- // Trigger change event
901
- const event = new Event('change', { bubbles: true });
902
- input.dispatchEvent(event);
903
-
904
- // Remove any previously displayed filename
905
- const prevFilename = document.querySelector('span.custom-filename');
906
- if (prevFilename) prevFilename.remove();
907
-
908
- // Display filename
909
- const fileNameElem = document.createElement('span');
910
- fileNameElem.textContent = this.files[0].name;
911
- fileNameElem.className = 'custom-filename';
912
- fileNameElem.style.marginLeft = '10px';
913
- fileNameElem.style.fontSize = '14px';
914
- this.parentNode.appendChild(fileNameElem);
915
- break;
916
- }
917
- }
918
- });
919
- }
920
- """)
921
 
922
  return demo
923
-
924
  # Setup the app with CORS middleware
925
  def setup_app():
926
  demo = create_interface()
 
493
  }
494
 
495
  # Function to handle file upload, OCR processing, and text analysis
496
+ def handle_file_upload_and_analyze(file_obj, mode: str, classifier: TextClassifier) -> tuple:
497
  """
498
  Handle file upload, OCR processing, and text analysis
499
  """
 
772
 
773
  # Create Gradio interface with a file upload button matched to the radio buttons
774
  def create_interface():
775
+ # Custom CSS for the interface - simplified to focus on essential styling
776
  css = """
777
  #analyze-btn {
778
  background-color: #FF8C00 !important;
779
  border-color: #FF8C00 !important;
780
  color: white !important;
781
  }
782
+
783
+ /* Style file upload button container */
784
+ .upload-btn-container {
785
+ display: inline-block;
786
+ margin-left: 15px;
787
+ vertical-align: middle;
788
+ }
789
+
790
+ /* Target Gradio's file upload component */
791
+ .upload-btn-container > div {
792
+ width: 150px !important;
793
+ box-sizing: border-box !important;
794
+ }
795
+
796
+ /* Fix the height to match radio buttons */
797
+ .upload-btn-container button {
798
+ height: 40px !important;
799
+ border-radius: 4px !important;
800
+ background-color: #f0f0f0 !important;
801
+ color: #333 !important;
802
+ border: 1px solid #d9d9d9 !important;
803
+ font-size: 14px !important;
804
+ line-height: normal !important;
805
+ padding: 8px 12px !important;
806
+ margin: 0 !important;
807
+ }
808
  """
809
 
810
  with gr.Blocks(css=css, title="AI Text Detector") as demo:
 
825
  gr.Markdown("Analysis Mode")
826
  gr.Markdown("Quick mode for faster analysis. Detailed mode for sentence-level analysis.")
827
 
828
+ # Simple row layout for radio buttons
829
  with gr.Row():
830
  mode_selection = gr.Radio(
831
  choices=["quick", "detailed"],
 
833
  label="",
834
  show_label=False
835
  )
836
+
837
+ # File upload in a custom-styled container
838
+ with gr.Column(elem_classes=["upload-btn-container"], scale=0):
839
+ file_upload = gr.UploadButton(
840
+ label="Upload Document",
841
+ file_types=["image", "pdf", "doc", "docx"],
842
+ size="sm"
843
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
844
 
845
  # Analyze button
846
  analyze_btn = gr.Button("Analyze Text", elem_id="analyze-btn")
 
858
  outputs=[output_html, output_sentences, output_result]
859
  )
860
 
861
+ # Connect file upload
862
+ file_upload.upload(
863
+ fn=lambda file, mode: handle_file_upload_and_analyze(file, mode, classifier),
864
  inputs=[file_upload, mode_selection],
865
  outputs=[output_html, output_sentences, output_result]
866
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
867
 
868
  return demo
869
+
870
  # Setup the app with CORS middleware
871
  def setup_app():
872
  demo = create_interface()