File size: 2,001 Bytes
6eda4a9
dfb0362
9b769e0
6eda4a9
9b769e0
dfb0362
 
9b769e0
 
 
 
 
 
 
 
 
dfb0362
9b769e0
 
dfb0362
 
 
 
 
9b769e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfb0362
9b769e0
 
6eda4a9
9b769e0
d4ab8ea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import gradio as gr
from transformers import pipeline
import re

# Load the pre-trained AI text classification model
detector = pipeline("text-classification", model="roberta-base-openai-detector")

# Count words in the input text
def count_words(text):
    return len(re.findall(r'\b\w+\b', text))

# Count characters (excluding spaces)
def count_characters(text):
    return len(text.replace(" ", ""))

# Detect if the text is AI-generated or human-written
def detect_text(text):
    if not text.strip():
        return "No text entered."
    result = detector(text)[0]
    label = result['label']
    score = round(result['score'] * 100, 2)
    return f"Prediction: {label} ({score}%)"

# Perform full analysis
def full_analysis(text):
    prediction = detect_text(text)
    words = count_words(text)
    chars = count_characters(text)
    return f"{prediction}\n\nWord Count: {words}\nCharacter Count: {chars}"

description = """
Detect whether a given text is AI-generated or human-written.
Also view word and character count for basic analysis.
"""

examples = [
    ["The sun sets beautifully behind the hills every evening."],
    ["As an AI language model developed by OpenAI, I am capable of many tasks."],
    ["She opened the book and smiled as the story unfolded."]
]

with gr.Blocks(title="Text AI Detector") as interface:
    gr.Markdown("# Text AI Detector")
    gr.Markdown(description)

    with gr.Tab("Detector"):
        text_input = gr.Textbox(label="Input Text", lines=8, placeholder="Type or paste your text here...")
        analyze_btn = gr.Button("Analyze")
        output = gr.Textbox(label="Result", lines=6)
        analyze_btn.click(fn=full_analysis, inputs=text_input, outputs=output)

    with gr.Tab("Examples"):
        gr.Examples(examples=examples, inputs=[text_input], outputs=[output], fn=full_analysis)

    gr.Markdown("---")
    gr.Markdown("Final Year Project | Built with Hugging Face + Gradio")

if __name__ == "__main__":
    interface.launch(share=True)