File size: 2,668 Bytes
958e345
ceeca7d
57bb1ed
6c56aca
ceeca7d
49d2f3f
ceeca7d
 
49d2f3f
 
 
 
 
 
 
ceeca7d
 
 
 
49d2f3f
 
ceeca7d
49d2f3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
import torch
import torch.nn.functional as F
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import re
import pandas as pd
import gradio as gr

MODEL_NAME = "dejanseo/ai-cop"

# --- Load model ---
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
dtype = torch.bfloat16 if (device.type=="cuda" and torch.cuda.is_bf16_supported()) else torch.float32
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME, torch_dtype=dtype)
model.to(device).eval()

def sent_tokenize(text):
    return [s for s in re.split(r'(?<=[\.!?])\s+', text.strip()) if s]

# --- Inference function ---
def classify_text(text):
    if not text.strip():
        return "⚠️ Please enter some text.", None, None

    sentences = sent_tokenize(text)
    if not sentences:
        return "⚠️ No sentences detected.", None, None

    inputs = tokenizer(
        sentences,
        return_tensors="pt",
        padding=True,
        truncation=True,
        max_length=model.config.max_position_embeddings
    ).to(device)

    with torch.no_grad():
        logits = model(**inputs).logits
        probs = F.softmax(logits, dim=-1).cpu()
        preds = torch.argmax(probs, dim=-1).cpu()

    results = []
    highlighted_sentences = []
    for i, s in enumerate(sentences):
        p = preds[i].item()
        conf = probs[i, p].item()
        label = "AI" if p == 0 else "Human"

        results.append([s, label, f"{conf:.2f}"])
        if label == "AI":
            highlighted_sentences.append(f"<span style='color:red; font-weight:bold'>{s}</span>")
        else:
            highlighted_sentences.append(f"<span style='color:green; font-weight:bold'>{s}</span>")

    # Overall AI likelihood
    avg = torch.mean(probs, dim=0)
    model_ai = avg[0].item() * 100

    highlighted_text = " ".join(highlighted_sentences)
    df = pd.DataFrame(results, columns=["Sentence", "Classification", "Confidence"])
    return f"⚖️ AI Likelihood: {model_ai:.1f}%", highlighted_text, df

# --- Gradio Interface ---
with gr.Blocks() as demo:
    gr.Markdown("## 🧠 AI Article Detection by Writenix")

    with gr.Row():
        text_input = gr.Textbox(label="Enter text", lines=10, placeholder="Paste your text here…")

    classify_btn = gr.Button("Classify")

    ai_score = gr.Label(label="Overall AI Likelihood")
    highlighted = gr.HTML()
    table = gr.Dataframe(headers=["Sentence", "Classification", "Confidence"], wrap=True)

    classify_btn.click(classify_text, inputs=text_input, outputs=[ai_score, highlighted, table])

if __name__ == "__main__":
    demo.launch()