Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
|
4 |
+
model_name = "elo4/TinyBERT-sentiment-model"
|
5 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
6 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
7 |
+
|
8 |
+
def predict_sentiment(text):
|
9 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128)
|
10 |
+
outputs = model(**inputs)
|
11 |
+
sentiment = outputs.logits.argmax().item()
|
12 |
+
return f"Predicted Sentiment Class: {sentiment}"
|
13 |
+
|
14 |
+
interface = gr.Interface(
|
15 |
+
fn=predict_sentiment,
|
16 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter a review here..."),
|
17 |
+
outputs="text",
|
18 |
+
title="TinyBERT Sentiment Analysis",
|
19 |
+
description="Enter a text review and get the sentiment class predicted by TinyBERT."
|
20 |
+
)
|
21 |
+
|
22 |
+
interface.launch()
|