Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,60 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
|
|
3 |
|
4 |
-
# Load
|
5 |
detector = pipeline("text-classification", model="roberta-base-openai-detector")
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
def detect_text(text):
|
|
|
|
|
8 |
result = detector(text)[0]
|
9 |
label = result['label']
|
10 |
score = round(result['score'] * 100, 2)
|
11 |
return f"Prediction: {label} ({score}%)"
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
|
|
23 |
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
+
import re
|
4 |
|
5 |
+
# Load the pre-trained AI text classification model
|
6 |
detector = pipeline("text-classification", model="roberta-base-openai-detector")
|
7 |
|
8 |
+
# Count words in the input text
|
9 |
+
def count_words(text):
|
10 |
+
return len(re.findall(r'\b\w+\b', text))
|
11 |
+
|
12 |
+
# Count characters (excluding spaces)
|
13 |
+
def count_characters(text):
|
14 |
+
return len(text.replace(" ", ""))
|
15 |
+
|
16 |
+
# Detect if the text is AI-generated or human-written
|
17 |
def detect_text(text):
|
18 |
+
if not text.strip():
|
19 |
+
return "No text entered."
|
20 |
result = detector(text)[0]
|
21 |
label = result['label']
|
22 |
score = round(result['score'] * 100, 2)
|
23 |
return f"Prediction: {label} ({score}%)"
|
24 |
|
25 |
+
# Perform full analysis
|
26 |
+
def full_analysis(text):
|
27 |
+
prediction = detect_text(text)
|
28 |
+
words = count_words(text)
|
29 |
+
chars = count_characters(text)
|
30 |
+
return f"{prediction}\n\nWord Count: {words}\nCharacter Count: {chars}"
|
31 |
+
|
32 |
+
description = """
|
33 |
+
Detect whether a given text is AI-generated or human-written.
|
34 |
+
Also view word and character count for basic analysis.
|
35 |
+
"""
|
36 |
+
|
37 |
+
examples = [
|
38 |
+
["The sun sets beautifully behind the hills every evening."],
|
39 |
+
["As an AI language model developed by OpenAI, I am capable of many tasks."],
|
40 |
+
["She opened the book and smiled as the story unfolded."]
|
41 |
+
]
|
42 |
+
|
43 |
+
with gr.Blocks(title="Text AI Detector") as interface:
|
44 |
+
gr.Markdown("# Text AI Detector")
|
45 |
+
gr.Markdown(description)
|
46 |
+
|
47 |
+
with gr.Tab("Detector"):
|
48 |
+
text_input = gr.Textbox(label="Input Text", lines=8, placeholder="Type or paste your text here...")
|
49 |
+
analyze_btn = gr.Button("Analyze")
|
50 |
+
output = gr.Textbox(label="Result", lines=6)
|
51 |
+
analyze_btn.click(fn=full_analysis, inputs=text_input, outputs=output)
|
52 |
+
|
53 |
+
with gr.Tab("Examples"):
|
54 |
+
gr.Examples(examples=examples, inputs=[text_input], outputs=[output], fn=full_analysis)
|
55 |
|
56 |
+
gr.Markdown("---")
|
57 |
+
gr.Markdown("Final Year Project | Built with Hugging Face + Gradio")
|
58 |
|
59 |
+
if __name__ == "__main__":
|
60 |
+
interface.launch()
|