Update app.py
Browse files
app.py
CHANGED
|
@@ -2,19 +2,14 @@ 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
|
| 18 |
return "Please enter some text."
|
| 19 |
|
| 20 |
inputs = tokenizer(
|
|
@@ -26,8 +21,7 @@ def classify_text(text):
|
|
| 26 |
)
|
| 27 |
|
| 28 |
with torch.no_grad():
|
| 29 |
-
|
| 30 |
-
logits = outputs.logits
|
| 31 |
|
| 32 |
probs = torch.softmax(logits, dim=-1)
|
| 33 |
pred_id = torch.argmax(probs, dim=1).item()
|
|
@@ -37,17 +31,11 @@ def classify_text(text):
|
|
| 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()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
|
|
|
|
| 5 |
MODEL_NAME = "duclo90/Semeval"
|
| 6 |
|
|
|
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 8 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
|
|
|
|
|
|
| 9 |
model.eval()
|
| 10 |
|
|
|
|
| 11 |
def classify_text(text):
|
| 12 |
+
if not text.strip():
|
| 13 |
return "Please enter some text."
|
| 14 |
|
| 15 |
inputs = tokenizer(
|
|
|
|
| 21 |
)
|
| 22 |
|
| 23 |
with torch.no_grad():
|
| 24 |
+
logits = model(**inputs).logits
|
|
|
|
| 25 |
|
| 26 |
probs = torch.softmax(logits, dim=-1)
|
| 27 |
pred_id = torch.argmax(probs, dim=1).item()
|
|
|
|
| 31 |
|
| 32 |
return f"Prediction: {label} (Confidence: {confidence})"
|
| 33 |
|
|
|
|
| 34 |
iface = gr.Interface(
|
| 35 |
fn=classify_text,
|
| 36 |
+
inputs=gr.Textbox(lines=6),
|
|
|
|
|
|
|
|
|
|
| 37 |
outputs="text",
|
| 38 |
title="Human vs Machine Text Classifier",
|
|
|
|
| 39 |
)
|
| 40 |
|
|
|
|
| 41 |
iface.launch()
|