| 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() |
|
|