File size: 1,050 Bytes
8cb40c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import gradio as gr
from transformers import (
    AutoTokenizer,
    AutoModelForSequenceClassification
)

MODEL_NAME = "duclo90/results"

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

model.eval()

def classify_text(text):
    inputs = tokenizer(
        text,
        return_tensors="pt",
        truncation=True,
        max_length=512
    )

    with torch.no_grad():
        outputs = model(**inputs)
        probs = torch.softmax(outputs.logits, dim=-1)
        score, pred = torch.max(probs, dim=-1)

    label = model.config.id2label[pred.item()]
    confidence = round(score.item(), 3)

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


iface = gr.Interface(
    fn=classify_text,
    inputs=gr.Textbox(lines=6, placeholder="Enter text here..."),
    outputs="text",
    title="Human vs Machine Text Classifier",
    description="Classifies text as human-written or machine-generated using a fine-tuned BERT model."
)

iface.launch()