File size: 1,744 Bytes
7fd68cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification

# ================= LOAD MODEL =================
MODEL_NAME = "distilbert-base-multilingual-cased"

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForSequenceClassification.from_pretrained(
    MODEL_NAME,
    num_labels=2
)
model.to(device)
model.eval()

# ================= AI FUNCTION =================
def classify_text(text):
    if not text or text.strip() == "":
        return "❌ Please enter some text"

    inputs = tokenizer(
        text,
        return_tensors="pt",
        truncation=True,
        padding=True
    ).to(device)

    with torch.no_grad():
        outputs = model(**inputs)

    probs = torch.softmax(outputs.logits, dim=1)[0]

    human = probs[0].item() * 100
    ai = probs[1].item() * 100

    return f"""
βœ… Human Probability: {human:.2f} %

πŸ€– AI Probability: {ai:.2f} %
"""

# ================= UI =================
with gr.Blocks(theme=gr.themes.Soft()) as app:
    gr.Markdown(
        "<h1 style='text-align:center;'>AI vs Human Text Detector</h1>"
    )
    gr.Markdown(
        "<p style='text-align:center;'>Professional NLP Web Application</p>"
    )

    with gr.Tab("πŸ“ Text Input"):
        text_input = gr.Textbox(
            lines=8,
            placeholder="Paste text here..."
        )
        analyze_btn = gr.Button("Analyze Text")
        result = gr.Textbox(label="Result")

    analyze_btn.click(classify_text, text_input, result)

    gr.Markdown(
        "<p style='text-align:center; font-size:12px;'>"
        "Created by Manar | AI Project 2025</p>"
    )

app.launch()