Update app.py
Browse files
app.py
CHANGED
|
@@ -814,7 +814,7 @@ def analyze_text(text: str, mode: str, classifier: TextClassifier) -> tuple:
|
|
| 814 |
# Modified Gradio interface setup function to include file upload
|
| 815 |
def setup_gradio_interface(classifier):
|
| 816 |
"""
|
| 817 |
-
Set up Gradio interface with
|
| 818 |
|
| 819 |
Args:
|
| 820 |
classifier: The TextClassifier instance
|
|
@@ -830,71 +830,78 @@ def setup_gradio_interface(classifier):
|
|
| 830 |
|
| 831 |
def handle_file_upload_wrapper(file_obj, mode):
|
| 832 |
if file_obj is None:
|
| 833 |
-
return (
|
| 834 |
-
"No file uploaded",
|
| 835 |
-
"Please upload a file to analyze",
|
| 836 |
-
"No file uploaded for analysis"
|
| 837 |
-
)
|
| 838 |
return handle_file_upload_and_analyze(file_obj, mode, classifier)
|
| 839 |
|
|
|
|
| 840 |
with gr.Blocks(title="AI Text Detector") as demo:
|
| 841 |
gr.Markdown("# AI Text Detector")
|
| 842 |
-
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.")
|
| 843 |
-
|
| 844 |
-
# Text input
|
| 845 |
-
text_input = gr.Textbox(
|
| 846 |
-
lines=8,
|
| 847 |
-
placeholder="Enter text to analyze...",
|
| 848 |
-
label="Input Text"
|
| 849 |
-
)
|
| 850 |
|
| 851 |
with gr.Row():
|
| 852 |
-
# Left
|
| 853 |
-
with gr.Column(
|
| 854 |
-
|
| 855 |
-
|
| 856 |
-
|
| 857 |
-
label="
|
| 858 |
-
info="Quick mode for faster analysis, Detailed mode for sentence-level analysis"
|
| 859 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 860 |
|
| 861 |
-
# Right
|
| 862 |
-
with gr.Column(
|
| 863 |
-
|
| 864 |
-
|
| 865 |
-
|
| 866 |
-
|
| 867 |
-
|
| 868 |
-
|
| 869 |
-
# Action buttons row
|
| 870 |
-
with gr.Row():
|
| 871 |
-
text_submit_button = gr.Button("Analyze Text", variant="primary")
|
| 872 |
-
upload_submit_button = gr.Button("Process Uploaded File", variant="primary")
|
| 873 |
-
|
| 874 |
-
# Output areas
|
| 875 |
-
output_html = gr.HTML(label="Highlighted Analysis")
|
| 876 |
-
output_sentences = gr.Textbox(label="Sentence-by-Sentence Analysis", lines=10)
|
| 877 |
-
output_result = gr.Textbox(label="Overall Result", lines=4)
|
| 878 |
-
|
| 879 |
-
# Connect buttons to functions
|
| 880 |
-
text_submit_button.click(
|
| 881 |
analyze_text_wrapper,
|
| 882 |
inputs=[text_input, mode_selection],
|
| 883 |
outputs=[output_html, output_sentences, output_result]
|
| 884 |
)
|
| 885 |
|
| 886 |
-
|
|
|
|
| 887 |
handle_file_upload_wrapper,
|
| 888 |
inputs=[file_upload, mode_selection],
|
| 889 |
outputs=[output_html, output_sentences, output_result]
|
| 890 |
)
|
| 891 |
|
| 892 |
-
# Add file upload
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 893 |
gr.Markdown("""
|
| 894 |
-
|
| 895 |
-
- Maximum file size: 1MB
|
| 896 |
-
- PDF files: Maximum 3 pages (OCR.space API limitation)
|
| 897 |
-
- Supported formats: Images (PNG, JPG, GIF), PDF, Word documents (DOCX, DOC)
|
| 898 |
""")
|
| 899 |
|
| 900 |
return demo
|
|
|
|
| 814 |
# Modified Gradio interface setup function to include file upload
|
| 815 |
def setup_gradio_interface(classifier):
|
| 816 |
"""
|
| 817 |
+
Set up Gradio interface with original layout plus a small file upload button
|
| 818 |
|
| 819 |
Args:
|
| 820 |
classifier: The TextClassifier instance
|
|
|
|
| 830 |
|
| 831 |
def handle_file_upload_wrapper(file_obj, mode):
|
| 832 |
if file_obj is None:
|
| 833 |
+
return analyze_text_wrapper("", mode) # Return empty analysis
|
|
|
|
|
|
|
|
|
|
|
|
|
| 834 |
return handle_file_upload_and_analyze(file_obj, mode, classifier)
|
| 835 |
|
| 836 |
+
# Create layout with side-by-side content
|
| 837 |
with gr.Blocks(title="AI Text Detector") as demo:
|
| 838 |
gr.Markdown("# AI Text Detector")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 839 |
|
| 840 |
with gr.Row():
|
| 841 |
+
# Left column - Input
|
| 842 |
+
with gr.Column():
|
| 843 |
+
text_input = gr.Textbox(
|
| 844 |
+
lines=8,
|
| 845 |
+
placeholder="Enter text to analyze...",
|
| 846 |
+
label="Input Text"
|
|
|
|
| 847 |
)
|
| 848 |
+
|
| 849 |
+
with gr.Row():
|
| 850 |
+
# Analysis mode selection
|
| 851 |
+
mode_selection = gr.Radio(
|
| 852 |
+
choices=["quick", "detailed"],
|
| 853 |
+
value="quick",
|
| 854 |
+
label="Analysis Mode",
|
| 855 |
+
info="Quick mode for faster analysis, Detailed mode for sentence-level analysis"
|
| 856 |
+
)
|
| 857 |
+
|
| 858 |
+
# Small file upload button (similar to paperclip in Claude)
|
| 859 |
+
file_upload = gr.File(
|
| 860 |
+
label="",
|
| 861 |
+
file_types=["image", "pdf", "doc", "docx"],
|
| 862 |
+
type="binary",
|
| 863 |
+
elem_id="file_upload_small"
|
| 864 |
+
)
|
| 865 |
+
|
| 866 |
+
# Original analyze button
|
| 867 |
+
analyze_button = gr.Button("Analyze Text")
|
| 868 |
|
| 869 |
+
# Right column - Results (same as original)
|
| 870 |
+
with gr.Column():
|
| 871 |
+
output_html = gr.HTML(label="Highlighted Analysis")
|
| 872 |
+
output_sentences = gr.Textbox(label="Sentence-by-Sentence Analysis", lines=10)
|
| 873 |
+
output_result = gr.Textbox(label="Overall Result", lines=4)
|
| 874 |
+
|
| 875 |
+
# Connect original analyze button
|
| 876 |
+
analyze_button.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 877 |
analyze_text_wrapper,
|
| 878 |
inputs=[text_input, mode_selection],
|
| 879 |
outputs=[output_html, output_sentences, output_result]
|
| 880 |
)
|
| 881 |
|
| 882 |
+
# Connect file upload changes to automatically process
|
| 883 |
+
file_upload.change(
|
| 884 |
handle_file_upload_wrapper,
|
| 885 |
inputs=[file_upload, mode_selection],
|
| 886 |
outputs=[output_html, output_sentences, output_result]
|
| 887 |
)
|
| 888 |
|
| 889 |
+
# Add CSS to make file upload button small and icon-like
|
| 890 |
+
gr.HTML("""
|
| 891 |
+
<style>
|
| 892 |
+
#file_upload_small {
|
| 893 |
+
max-width: 50px;
|
| 894 |
+
margin-top: 25px;
|
| 895 |
+
}
|
| 896 |
+
#file_upload_small .label-wrap {
|
| 897 |
+
display: none;
|
| 898 |
+
}
|
| 899 |
+
</style>
|
| 900 |
+
""")
|
| 901 |
+
|
| 902 |
+
# Small text about file upload (at bottom)
|
| 903 |
gr.Markdown("""
|
| 904 |
+
*You can also upload an image, PDF, or Word document (max 1MB) for OCR text extraction and analysis.*
|
|
|
|
|
|
|
|
|
|
| 905 |
""")
|
| 906 |
|
| 907 |
return demo
|