File size: 993 Bytes
86a73fe
4b2a1b4
86a73fe
4b2a1b4
86a73fe
4b2a1b4
86a73fe
 
 
 
4b2a1b4
74a3aa1
86a73fe
 
 
 
 
 
 
 
 
 
 
74a3aa1
86a73fe
 
 
 
 
 
 
 
4b2a1b4
 
 
74a3aa1
4b2a1b4
 
 
 
86a73fe
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
import torch
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification

MODEL_NAME = "duclo90/Semeval"

tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
model.eval()

def classify_text(text):
    if not text.strip():
        return "Please enter some text."

    inputs = tokenizer(
        text,
        return_tensors="pt",
        truncation=True,
        padding=True,
        max_length=512,
    )

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

    probs = torch.softmax(logits, dim=-1)
    pred_id = torch.argmax(probs, dim=1).item()

    label = model.config.id2label[pred_id]
    confidence = round(probs[0][pred_id].item(), 3)

    return f"Prediction: {label} (Confidence: {confidence})"

iface = gr.Interface(
    fn=classify_text,
    inputs=gr.Textbox(lines=6),
    outputs="text",
    title="Human vs Machine Text Classifier",
)

iface.launch()