Spaces:
Sleeping
Sleeping
Initial deploy
Browse files- README.md +10 -5
- app.py +89 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,12 +1,17 @@
|
|
| 1 |
---
|
| 2 |
title: Sentiment Analysis
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
title: Sentiment Analysis
|
| 3 |
+
emoji: "\U0001F4CA"
|
| 4 |
+
colorFrom: green
|
| 5 |
+
colorTo: yellow
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: "5.29.0"
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# Sentiment Analysis
|
| 14 |
+
|
| 15 |
+
Analyze the sentiment of text input (positive / negative / neutral).
|
| 16 |
+
|
| 17 |
+
**Course**: 100 Deep Learning ch4 — RNN & NLP
|
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Sentiment Analysis — Positive / Negative / Neutral
|
| 3 |
+
Course: 100 Deep Learning ch4
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from transformers import pipeline
|
| 8 |
+
|
| 9 |
+
# Multi-language sentiment model
|
| 10 |
+
classifier = pipeline(
|
| 11 |
+
"sentiment-analysis",
|
| 12 |
+
model="lxyuan/distilbert-base-multilingual-cased-sentiments-student",
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
LABEL_MAP = {
|
| 16 |
+
"positive": "Positive",
|
| 17 |
+
"negative": "Negative",
|
| 18 |
+
"neutral": "Neutral",
|
| 19 |
+
}
|
| 20 |
+
EMOJI_MAP = {
|
| 21 |
+
"positive": "😊",
|
| 22 |
+
"negative": "😞",
|
| 23 |
+
"neutral": "😐",
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def analyze(text: str):
|
| 28 |
+
if not text.strip():
|
| 29 |
+
return {}, ""
|
| 30 |
+
|
| 31 |
+
results = classifier(text, top_k=3)
|
| 32 |
+
label_dict = {
|
| 33 |
+
f"{EMOJI_MAP.get(r['label'], '')} {LABEL_MAP.get(r['label'], r['label'])}": round(r["score"], 4)
|
| 34 |
+
for r in results
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
top = results[0]
|
| 38 |
+
emoji = EMOJI_MAP.get(top["label"], "")
|
| 39 |
+
summary = (
|
| 40 |
+
f"**Prediction: {emoji} {LABEL_MAP.get(top['label'], top['label'])}** "
|
| 41 |
+
f"({top['score']:.1%} confidence)\n\n"
|
| 42 |
+
f"| Label | Score |\n|---|---|\n"
|
| 43 |
+
)
|
| 44 |
+
for r in results:
|
| 45 |
+
bar_len = int(r["score"] * 30)
|
| 46 |
+
bar = "█" * bar_len + "░" * (30 - bar_len)
|
| 47 |
+
summary += f"| {LABEL_MAP.get(r['label'], r['label'])} | {bar} {r['score']:.1%} |\n"
|
| 48 |
+
|
| 49 |
+
return label_dict, summary
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
with gr.Blocks(title="Sentiment Analysis") as demo:
|
| 53 |
+
gr.Markdown(
|
| 54 |
+
"# Sentiment Analysis\n"
|
| 55 |
+
"Enter text in any language to analyze its sentiment.\n"
|
| 56 |
+
"Uses a multilingual DistilBERT model.\n"
|
| 57 |
+
"*Course: 100 Deep Learning ch4 — RNN & Sequence Models*"
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
with gr.Row():
|
| 61 |
+
with gr.Column():
|
| 62 |
+
text_input = gr.Textbox(
|
| 63 |
+
label="Input Text",
|
| 64 |
+
placeholder="Type or paste text here...",
|
| 65 |
+
lines=4,
|
| 66 |
+
)
|
| 67 |
+
btn = gr.Button("Analyze Sentiment", variant="primary")
|
| 68 |
+
with gr.Column():
|
| 69 |
+
label_out = gr.Label(num_top_classes=3, label="Sentiment Scores")
|
| 70 |
+
|
| 71 |
+
detail_md = gr.Markdown()
|
| 72 |
+
|
| 73 |
+
btn.click(analyze, [text_input], [label_out, detail_md])
|
| 74 |
+
text_input.submit(analyze, [text_input], [label_out, detail_md])
|
| 75 |
+
|
| 76 |
+
gr.Examples(
|
| 77 |
+
examples=[
|
| 78 |
+
"I absolutely love this product! It exceeded all my expectations.",
|
| 79 |
+
"The movie was terrible. Worst 2 hours of my life.",
|
| 80 |
+
"The weather today is partly cloudy with temperatures around 72°F.",
|
| 81 |
+
"这个餐厅的食物非常好吃,服务也很棒!",
|
| 82 |
+
"I'm not sure how I feel about the new update. It has some good features but also some bugs.",
|
| 83 |
+
"Das Essen war hervorragend und die Bedienung sehr freundlich.",
|
| 84 |
+
],
|
| 85 |
+
inputs=[text_input],
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
if __name__ == "__main__":
|
| 89 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=5.0.0
|
| 2 |
+
transformers>=4.30.0
|
| 3 |
+
torch>=2.0.0
|