Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import (
|
| 4 |
+
AutoTokenizer,
|
| 5 |
+
AutoModelForSequenceClassification
|
| 6 |
+
)
|
| 7 |
+
|
| 8 |
+
MODEL_NAME = "duclo90/results"
|
| 9 |
+
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 11 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 12 |
+
|
| 13 |
+
model.eval()
|
| 14 |
+
|
| 15 |
+
def classify_text(text):
|
| 16 |
+
inputs = tokenizer(
|
| 17 |
+
text,
|
| 18 |
+
return_tensors="pt",
|
| 19 |
+
truncation=True,
|
| 20 |
+
max_length=512
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
outputs = model(**inputs)
|
| 25 |
+
probs = torch.softmax(outputs.logits, dim=-1)
|
| 26 |
+
score, pred = torch.max(probs, dim=-1)
|
| 27 |
+
|
| 28 |
+
label = model.config.id2label[pred.item()]
|
| 29 |
+
confidence = round(score.item(), 3)
|
| 30 |
+
|
| 31 |
+
return f"Prediction: {label} (Confidence: {confidence})"
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
iface = gr.Interface(
|
| 35 |
+
fn=classify_text,
|
| 36 |
+
inputs=gr.Textbox(lines=6, placeholder="Enter text here..."),
|
| 37 |
+
outputs="text",
|
| 38 |
+
title="Human vs Machine Text Classifier",
|
| 39 |
+
description="Classifies text as human-written or machine-generated using a fine-tuned BERT model."
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
iface.launch()
|