app.py file
Browse files
app.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
|
4 |
+
|
5 |
+
model_name = "distilbert-base-uncased"
|
6 |
+
tokenizer = DistilBertTokenizer.from_pretrained(model_name)
|
7 |
+
model = DistilBertForSequenceClassification.from_pretrained(model_name)
|
8 |
+
|
9 |
+
def predict_sentiment(text):
|
10 |
+
# Tokenize and predict
|
11 |
+
inputs = tokenizer(text, return_tensors="pt")
|
12 |
+
outputs = model(**inputs)
|
13 |
+
logits = outputs.logits
|
14 |
+
predicted_class = logits.argmax().item()
|
15 |
+
|
16 |
+
# Return the predicted class
|
17 |
+
return {"positive": logits[0][1].item(), "negative": logits[0][0].item()}
|
18 |
+
|
19 |
+
iface = gr.Interface(
|
20 |
+
fn=predict_sentiment,
|
21 |
+
inputs=gr.Textbox(),
|
22 |
+
outputs="label",
|