Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
|
| 6 |
+
# Lightweight model already fine-tuned for sentiment (91%+ accuracy)
|
| 7 |
+
# Much smaller than bert-base-uncased β fits easily in free CPU Spaces
|
| 8 |
+
MODEL_NAME = "distilbert/distilbert-base-uncased-finetuned-sst-2-english"
|
| 9 |
+
|
| 10 |
+
# Load model efficiently
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 12 |
+
|
| 13 |
+
model = AutoModelForSequenceClassification.from_pretrained(
|
| 14 |
+
MODEL_NAME,
|
| 15 |
+
torch_dtype=torch.float32, # Safe for CPU
|
| 16 |
+
low_cpu_mem_usage=True
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
model.eval()
|
| 20 |
+
|
| 21 |
+
# Safe device handling
|
| 22 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 23 |
+
model = model.to(device)
|
| 24 |
+
|
| 25 |
+
def predict_sentiment(text: str):
|
| 26 |
+
if not text or not text.strip():
|
| 27 |
+
return "Please enter some text", 0.0, "β οΈ"
|
| 28 |
+
|
| 29 |
+
# Tokenize
|
| 30 |
+
inputs = tokenizer(
|
| 31 |
+
text,
|
| 32 |
+
return_tensors="pt",
|
| 33 |
+
truncation=True,
|
| 34 |
+
padding=True,
|
| 35 |
+
max_length=512
|
| 36 |
+
).to(device)
|
| 37 |
+
|
| 38 |
+
# Inference with no gradient
|
| 39 |
+
with torch.no_grad():
|
| 40 |
+
outputs = model(**inputs)
|
| 41 |
+
probs = F.softmax(outputs.logits, dim=-1)
|
| 42 |
+
|
| 43 |
+
# Get prediction and confidence
|
| 44 |
+
pred = torch.argmax(probs, dim=-1).item()
|
| 45 |
+
confidence = probs[0][pred].item() * 100
|
| 46 |
+
|
| 47 |
+
if pred == 1: # 1 = positive in this model
|
| 48 |
+
sentiment = "Positive π"
|
| 49 |
+
emoji = "π’"
|
| 50 |
+
else:
|
| 51 |
+
sentiment = "Negative π"
|
| 52 |
+
emoji = "π΄"
|
| 53 |
+
|
| 54 |
+
return sentiment, round(confidence, 2), emoji
|
| 55 |
+
|
| 56 |
+
# ====================== Gradio UI ======================
|
| 57 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="BERT Sentiment Analysis") as demo:
|
| 58 |
+
gr.Markdown("# π¬ Transformer Sentiment Analysis")
|
| 59 |
+
gr.Markdown("**DistilBERT** (lightweight BERT) for fast movie review / text sentiment prediction")
|
| 60 |
+
|
| 61 |
+
with gr.Row():
|
| 62 |
+
text_input = gr.Textbox(
|
| 63 |
+
label="Enter your text (movie review, comment, tweet, etc.)",
|
| 64 |
+
placeholder="Type or paste here...",
|
| 65 |
+
lines=5,
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
analyze_btn = gr.Button("π Analyze Sentiment", variant="primary", size="large")
|
| 69 |
+
|
| 70 |
+
with gr.Row():
|
| 71 |
+
sentiment_output = gr.Textbox(label="Sentiment Result", interactive=False)
|
| 72 |
+
confidence_output = gr.Number(label="Confidence (%)", interactive=False)
|
| 73 |
+
indicator_output = gr.Textbox(label="Indicator", interactive=False)
|
| 74 |
+
|
| 75 |
+
# Nice examples
|
| 76 |
+
gr.Examples(
|
| 77 |
+
examples=[
|
| 78 |
+
["This movie was absolutely fantastic! The acting and story were brilliant."],
|
| 79 |
+
["I really disliked the plot. It was boring and predictable."],
|
| 80 |
+
["The visuals were great but the dialogue felt terrible."],
|
| 81 |
+
["One of the best films I have watched this year. Highly recommended!"],
|
| 82 |
+
["Complete waste of time. Do not watch this movie."],
|
| 83 |
+
],
|
| 84 |
+
inputs=text_input,
|
| 85 |
+
outputs=[sentiment_output, confidence_output, indicator_output],
|
| 86 |
+
fn=predict_sentiment,
|
| 87 |
+
cache_examples=False,
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
# Button click
|
| 91 |
+
analyze_btn.click(
|
| 92 |
+
fn=predict_sentiment,
|
| 93 |
+
inputs=text_input,
|
| 94 |
+
outputs=[sentiment_output, confidence_output, indicator_output],
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# Launch (Important: share=False on HF Spaces)
|
| 98 |
+
if __name__ == "__main__":
|
| 99 |
+
demo.launch(
|
| 100 |
+
server_name="0.0.0.0",
|
| 101 |
+
server_port=7860,
|
| 102 |
+
share=False,
|
| 103 |
+
debug=False
|
| 104 |
+
)
|