Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,53 @@
|
|
| 1 |
-
|
| 2 |
-
# !pip install transformers gradio
|
| 3 |
-
|
| 4 |
-
from transformers import pipeline
|
| 5 |
import gradio as gr
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
tokenizer=model_name
|
| 14 |
-
)
|
| 15 |
|
| 16 |
-
#
|
|
|
|
|
|
|
|
|
|
| 17 |
def classify_text(text):
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
#
|
| 24 |
iface = gr.Interface(
|
| 25 |
fn=classify_text,
|
| 26 |
-
inputs=gr.Textbox(
|
|
|
|
|
|
|
|
|
|
| 27 |
outputs="text",
|
| 28 |
title="Human vs Machine Text Classifier",
|
| 29 |
-
description="
|
| 30 |
)
|
| 31 |
|
| 32 |
-
# Launch
|
| 33 |
-
iface.launch(
|
|
|
|
| 1 |
+
import torch
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
|
| 5 |
+
# Hugging Face model repo
|
| 6 |
+
MODEL_NAME = "duclo90/Semeval"
|
| 7 |
|
| 8 |
+
# Load tokenizer and model explicitly
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 10 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Put model in eval mode
|
| 13 |
+
model.eval()
|
| 14 |
+
|
| 15 |
+
# Inference function
|
| 16 |
def classify_text(text):
|
| 17 |
+
if not text or text.strip() == "":
|
| 18 |
+
return "Please enter some text."
|
| 19 |
+
|
| 20 |
+
inputs = tokenizer(
|
| 21 |
+
text,
|
| 22 |
+
return_tensors="pt",
|
| 23 |
+
truncation=True,
|
| 24 |
+
padding=True,
|
| 25 |
+
max_length=512,
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
with torch.no_grad():
|
| 29 |
+
outputs = model(**inputs)
|
| 30 |
+
logits = outputs.logits
|
| 31 |
+
|
| 32 |
+
probs = torch.softmax(logits, dim=-1)
|
| 33 |
+
pred_id = torch.argmax(probs, dim=1).item()
|
| 34 |
+
|
| 35 |
+
label = model.config.id2label[pred_id]
|
| 36 |
+
confidence = round(probs[0][pred_id].item(), 3)
|
| 37 |
+
|
| 38 |
+
return f"Prediction: {label} (Confidence: {confidence})"
|
| 39 |
|
| 40 |
+
# Gradio UI
|
| 41 |
iface = gr.Interface(
|
| 42 |
fn=classify_text,
|
| 43 |
+
inputs=gr.Textbox(
|
| 44 |
+
lines=6,
|
| 45 |
+
placeholder="Enter text here..."
|
| 46 |
+
),
|
| 47 |
outputs="text",
|
| 48 |
title="Human vs Machine Text Classifier",
|
| 49 |
+
description="Detect whether a text is written by a human or generated by a machine."
|
| 50 |
)
|
| 51 |
|
| 52 |
+
# Launch app
|
| 53 |
+
iface.launch()
|