Update app.py
Browse files
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 |
-
|
|
|
816 |
"""
|
817 |
-
Set up Gradio interface with text input and file upload
|
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
|
836 |
-
gr.Markdown("Analyze text to detect if it was written by a human or AI. You can paste text directly or upload
|
837 |
-
|
838 |
-
|
839 |
-
|
840 |
-
|
841 |
-
|
842 |
-
|
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.
|
865 |
-
#
|
866 |
-
|
867 |
-
|
868 |
-
|
869 |
-
|
870 |
-
|
|
|
|
|
871 |
|
872 |
-
|
873 |
-
|
874 |
-
|
875 |
-
|
876 |
-
|
877 |
-
|
878 |
-
|
879 |
-
|
880 |
-
|
881 |
-
|
882 |
-
|
883 |
-
|
884 |
-
|
885 |
-
|
886 |
-
|
887 |
-
|
888 |
-
|
889 |
-
|
890 |
-
|
891 |
-
|
892 |
-
|
893 |
-
|
894 |
-
|
895 |
-
|
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 |
|