ApsidalSolid4 commited on
Commit
c3bd588
·
verified ·
1 Parent(s): 23f4252

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -62
app.py CHANGED
@@ -812,9 +812,10 @@ def analyze_text(text: str, mode: str, classifier: TextClassifier) -> tuple:
812
 
813
 
814
  # Modified Gradio interface setup function to include file upload
815
- def setup_gradio_interface(classifier):
 
816
  """
817
- Set up Gradio interface with text input and file upload options
818
 
819
  Args:
820
  classifier: The TextClassifier instance
@@ -829,71 +830,73 @@ def setup_gradio_interface(classifier):
829
  return analyze_text(text, mode, classifier)
830
 
831
  def handle_file_upload_wrapper(file_obj, mode):
 
 
 
 
 
 
832
  return handle_file_upload_and_analyze(file_obj, mode, classifier)
833
 
834
  with gr.Blocks(title="AI Text Detector") as demo:
835
- gr.Markdown("# AI Text Detector with Document Upload")
836
- gr.Markdown("Analyze text to detect if it was written by a human or AI. You can paste text directly or upload images, PDFs, or Word documents.")
837
-
838
- with gr.Tab("Text Input"):
839
- text_input = gr.Textbox(
840
- lines=8,
841
- placeholder="Enter text to analyze...",
842
- label="Input Text"
843
- )
844
-
845
- mode_selection = gr.Radio(
846
- choices=["quick", "detailed"],
847
- value="quick",
848
- label="Analysis Mode",
849
- info="Quick mode for faster analysis, Detailed mode for sentence-level analysis"
850
- )
851
-
852
- text_submit_button = gr.Button("Analyze Text")
853
-
854
- output_html = gr.HTML(label="Highlighted Analysis")
855
- output_sentences = gr.Textbox(label="Sentence-by-Sentence Analysis", lines=10)
856
- output_result = gr.Textbox(label="Overall Result", lines=4)
857
-
858
- text_submit_button.click(
859
- analyze_text_wrapper,
860
- inputs=[text_input, mode_selection],
861
- outputs=[output_html, output_sentences, output_result]
862
- )
863
 
864
- with gr.Tab("File Upload"):
865
- # FIXED: Changed type="file" to type="binary"
866
- file_upload = gr.File(
867
- label="Upload Document",
868
- file_types=["image", "pdf", "doc", "docx"],
869
- type="binary"
870
- )
 
 
871
 
872
- file_mode_selection = gr.Radio(
873
- choices=["quick", "detailed"],
874
- value="quick",
875
- label="Analysis Mode",
876
- info="Quick mode for faster analysis, Detailed mode for sentence-level analysis"
877
- )
878
-
879
- upload_submit_button = gr.Button("Process and Analyze")
880
-
881
- file_output_html = gr.HTML(label="Highlighted Analysis")
882
- file_output_sentences = gr.Textbox(label="Sentence-by-Sentence Analysis", lines=10)
883
- file_output_result = gr.Textbox(label="Overall Result", lines=4)
884
-
885
- upload_submit_button.click(
886
- handle_file_upload_wrapper,
887
- inputs=[file_upload, file_mode_selection],
888
- outputs=[file_output_html, file_output_sentences, file_output_result]
889
- )
890
-
891
- gr.Markdown("""
892
- ### File Upload Limitations
893
- - Maximum file size: 1MB
894
- - PDF files: Maximum 3 pages (OCR.space API limitation)
895
- - Supported formats: Images (PNG, JPG, GIF), PDF, Word documents (DOCX, DOC)
896
- """)
 
 
 
 
 
 
 
 
 
 
 
 
 
897
 
898
  return demo
899
 
 
812
 
813
 
814
  # Modified Gradio interface setup function to include file upload
815
+ Here's the modified code for the Gradio interface that adds a file upload button next to the analysis mode buttons, while maintaining the original interface:
816
+ pythonCopydef setup_gradio_interface(classifier):
817
  """
818
+ Set up Gradio interface with text input and file upload button next to analysis mode
819
 
820
  Args:
821
  classifier: The TextClassifier instance
 
830
  return analyze_text(text, mode, classifier)
831
 
832
  def handle_file_upload_wrapper(file_obj, mode):
833
+ if file_obj is None:
834
+ return (
835
+ "No file uploaded",
836
+ "Please upload a file to analyze",
837
+ "No file uploaded for analysis"
838
+ )
839
  return handle_file_upload_and_analyze(file_obj, mode, classifier)
840
 
841
  with gr.Blocks(title="AI Text Detector") as demo:
842
+ gr.Markdown("# AI Text Detector")
843
+ gr.Markdown("Analyze text to detect if it was written by a human or AI. You can paste text directly or upload an image, PDF, or Word document.")
844
+
845
+ # Text input
846
+ text_input = gr.Textbox(
847
+ lines=8,
848
+ placeholder="Enter text to analyze...",
849
+ label="Input Text"
850
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
851
 
852
+ with gr.Row():
853
+ # Left side: Analysis mode radio buttons
854
+ with gr.Column(scale=1):
855
+ mode_selection = gr.Radio(
856
+ choices=["quick", "detailed"],
857
+ value="quick",
858
+ label="Analysis Mode",
859
+ info="Quick mode for faster analysis, Detailed mode for sentence-level analysis"
860
+ )
861
 
862
+ # Right side: File upload button
863
+ with gr.Column(scale=1):
864
+ file_upload = gr.File(
865
+ label="Or upload a document",
866
+ file_types=["image", "pdf", "doc", "docx"],
867
+ type="binary"
868
+ )
869
+
870
+ # Action buttons row
871
+ with gr.Row():
872
+ text_submit_button = gr.Button("Analyze Text", variant="primary")
873
+ upload_submit_button = gr.Button("Process Uploaded File", variant="primary")
874
+
875
+ # Output areas
876
+ output_html = gr.HTML(label="Highlighted Analysis")
877
+ output_sentences = gr.Textbox(label="Sentence-by-Sentence Analysis", lines=10)
878
+ output_result = gr.Textbox(label="Overall Result", lines=4)
879
+
880
+ # Connect buttons to functions
881
+ text_submit_button.click(
882
+ analyze_text_wrapper,
883
+ inputs=[text_input, mode_selection],
884
+ outputs=[output_html, output_sentences, output_result]
885
+ )
886
+
887
+ upload_submit_button.click(
888
+ handle_file_upload_wrapper,
889
+ inputs=[file_upload, mode_selection],
890
+ outputs=[output_html, output_sentences, output_result]
891
+ )
892
+
893
+ # Add file upload limitations info
894
+ gr.Markdown("""
895
+ ### File Upload Limitations
896
+ - Maximum file size: 1MB
897
+ - PDF files: Maximum 3 pages (OCR.space API limitation)
898
+ - Supported formats: Images (PNG, JPG, GIF), PDF, Word documents (DOCX, DOC)
899
+ """)
900
 
901
  return demo
902