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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -72
app.py CHANGED
@@ -772,68 +772,13 @@ classifier = TextClassifier()
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
776
  css = """
777
  #analyze-btn {
778
  background-color: #FF8C00 !important;
779
  border-color: #FF8C00 !important;
780
  color: white !important;
781
  }
782
-
783
- /* Create a compact file upload button */
784
- [data-testid="file"] {
785
- width: 150px !important;
786
- margin-left: 15px !important;
787
- height: 38px !important; /* Match height to radio buttons */
788
- }
789
-
790
- /* Hide the drop area completely */
791
- [data-testid="file"] [data-testid="droparea"] {
792
- height: 38px !important;
793
- min-height: 38px !important;
794
- max-height: 38px !important;
795
- padding: 0 !important;
796
- margin: 0 !important;
797
- border-radius: 4px !important;
798
- background-color: #f0f0f0 !important;
799
- border: 1px solid #d9d9d9 !important;
800
- display: flex !important;
801
- align-items: center !important;
802
- justify-content: center !important;
803
- overflow: hidden !important;
804
- }
805
-
806
- /* Hide all elements inside the drop area */
807
- [data-testid="file"] [data-testid="droparea"] > * {
808
- display: none !important;
809
- }
810
-
811
- /* Add a custom button */
812
- [data-testid="file"] [data-testid="droparea"]::before {
813
- content: "Upload Document" !important;
814
- display: block !important;
815
- font-size: 14px !important;
816
- color: #333 !important;
817
- }
818
-
819
- /* Make sure the file input is still accessible but invisible */
820
- [data-testid="file"] input[type="file"] {
821
- position: absolute !important;
822
- width: 150px !important;
823
- height: 38px !important;
824
- top: 0 !important;
825
- left: 15px !important;
826
- opacity: 0 !important;
827
- cursor: pointer !important;
828
- z-index: 100 !important;
829
- }
830
-
831
- /* Hide the "or" text and other elements */
832
- [data-testid="file"] .or,
833
- [data-testid="file"] p,
834
- [data-testid="file"] svg {
835
- display: none !important;
836
- }
837
  """
838
 
839
  with gr.Blocks(css=css, title="AI Text Detector") as demo:
@@ -854,23 +799,64 @@ def create_interface():
854
  gr.Markdown("Analysis Mode")
855
  gr.Markdown("Quick mode for faster analysis. Detailed mode for sentence-level analysis.")
856
 
857
- # Simple row layout for radio buttons and file upload
858
  with gr.Row():
859
- with gr.Column(elem_classes=["radio-container"], scale=3):
860
- mode_selection = gr.Radio(
861
- choices=["quick", "detailed"],
862
- value="quick",
863
- label="",
864
- show_label=False
865
- )
866
-
867
- # Use an explicit column with a class for file upload
868
- with gr.Column(elem_classes=["file-container"], scale=1):
869
- file_upload = gr.File(
870
- file_types=["image", "pdf", "doc", "docx"],
871
- type="binary",
872
- label="" # Remove label to use our pseudo-element
873
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
874
 
875
  # Analyze button
876
  analyze_btn = gr.Button("Analyze Text", elem_id="analyze-btn")
@@ -888,11 +874,50 @@ def create_interface():
888
  outputs=[output_html, output_sentences, output_result]
889
  )
890
 
 
891
  file_upload.change(
892
  fn=handle_file_upload_and_analyze,
893
  inputs=[file_upload, mode_selection],
894
  outputs=[output_html, output_sentences, output_result]
895
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
896
 
897
  return demo
898
 
 
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
  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"],
806
+ value="quick",
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
  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