Spaces:
Sleeping
Sleeping
Added Apllication File
Browse files- app.py +39 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load model
|
| 6 |
+
model_name = "textattack/bert-base-uncased-SST-2"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def analyze_sentiment(text):
|
| 12 |
+
inputs = tokenizer(text, return_tensors="pt",
|
| 13 |
+
truncation=True, padding=True)
|
| 14 |
+
|
| 15 |
+
with torch.no_grad():
|
| 16 |
+
outputs = model(**inputs)
|
| 17 |
+
probs = torch.softmax(outputs.logits, dim=1)
|
| 18 |
+
|
| 19 |
+
return {
|
| 20 |
+
"POSITIVE": float(probs[0][1]),
|
| 21 |
+
"NEGATIVE": float(probs[0][0])
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# Create interface
|
| 26 |
+
demo = gr.Interface(
|
| 27 |
+
fn=analyze_sentiment,
|
| 28 |
+
inputs=gr.Textbox(label="Input text", placeholder="Enter text here..."),
|
| 29 |
+
outputs=gr.Label(label="Sentiment Probabilities"),
|
| 30 |
+
examples=[
|
| 31 |
+
["I love this product!"],
|
| 32 |
+
["This was terrible experience"],
|
| 33 |
+
["It was okay, nothing special"]
|
| 34 |
+
],
|
| 35 |
+
title="BERT Sentiment Analysis",
|
| 36 |
+
description="Predicts sentiment using BERT model fine-tuned on SST-2 dataset"
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
gradio
|