Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
model_name = "MennatullahHany/Tunned_Bert"
|
| 6 |
+
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
def classify(text):
|
| 11 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True)
|
| 12 |
+
with torch.no_grad():
|
| 13 |
+
outputs = model(**inputs)
|
| 14 |
+
probs = torch.softmax(outputs.logits, dim=1)
|
| 15 |
+
confidence, predicted = torch.max(probs, dim=1)
|
| 16 |
+
|
| 17 |
+
label = model.config.id2label[predicted.item()]
|
| 18 |
+
confidence = float(confidence.item())
|
| 19 |
+
return {label: confidence}
|
| 20 |
+
|
| 21 |
+
iface = gr.Interface(
|
| 22 |
+
fn=classify,
|
| 23 |
+
inputs=gr.Textbox(lines=5, placeholder="Enter text here..."),
|
| 24 |
+
outputs=gr.Label(),
|
| 25 |
+
title="BERT Safety Classifier",
|
| 26 |
+
description="Enter text to classify as SAFE or DANGER."
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
iface.launch()
|