Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- README.md +18 -5
- app.py +57 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,12 +1,25 @@
|
|
| 1 |
---
|
| 2 |
title: Sentiment Explorer
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 6.0.2
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
title: Sentiment Explorer
|
| 3 |
+
emoji: π
|
| 4 |
+
colorFrom: purple
|
| 5 |
+
colorTo: pink
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: "6.0.2"
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# π Sentiment Explorer
|
| 14 |
+
|
| 15 |
+
Analyze the emotional tone of any text using DistilBERT via the HuggingFace Inference API.
|
| 16 |
+
|
| 17 |
+
## Features
|
| 18 |
+
|
| 19 |
+
- Real-time sentiment analysis
|
| 20 |
+
- Confidence scores with visual feedback
|
| 21 |
+
- No model downloads - uses API
|
| 22 |
+
|
| 23 |
+
## Setup
|
| 24 |
+
|
| 25 |
+
Add your `HF_TOKEN` as a Secret in Space Settings.
|
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
+
|
| 5 |
+
# Get token from environment (set in HF Space secrets)
|
| 6 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 7 |
+
client = InferenceClient(token=HF_TOKEN) if HF_TOKEN else InferenceClient()
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def analyze(text: str) -> tuple[str, dict]:
|
| 11 |
+
"""Return emoji + label and confidence scores."""
|
| 12 |
+
if not text.strip():
|
| 13 |
+
return "π€ Enter some text!", {}
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
result = client.text_classification(
|
| 17 |
+
text,
|
| 18 |
+
model="distilbert-base-uncased-finetuned-sst-2-english",
|
| 19 |
+
)[0]
|
| 20 |
+
|
| 21 |
+
label = result.label
|
| 22 |
+
score = result.score
|
| 23 |
+
emoji = "π" if label == "POSITIVE" else "π"
|
| 24 |
+
return f"{emoji} {label} ({score:.1%})", {label: score, "OTHER": 1 - score}
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return f"β Error: {e}", {}
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
with gr.Blocks(title="Sentiment Explorer") as demo:
|
| 30 |
+
gr.Markdown("# π Sentiment Explorer\nType anything and see if it's positive or negative!")
|
| 31 |
+
|
| 32 |
+
inp = gr.Textbox(
|
| 33 |
+
label="Your text",
|
| 34 |
+
placeholder="I absolutely love learning about AI!",
|
| 35 |
+
lines=3,
|
| 36 |
+
autofocus=True,
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
with gr.Row(equal_height=True):
|
| 40 |
+
result_label = gr.Textbox(label="Verdict", interactive=False)
|
| 41 |
+
confidence = gr.Label(label="Confidence")
|
| 42 |
+
|
| 43 |
+
btn = gr.Button("Analyze", variant="primary")
|
| 44 |
+
btn.click(analyze, inputs=inp, outputs=[result_label, confidence])
|
| 45 |
+
inp.submit(analyze, inputs=inp, outputs=[result_label, confidence])
|
| 46 |
+
|
| 47 |
+
gr.Examples(
|
| 48 |
+
examples=[
|
| 49 |
+
["This tutorial is amazing and super helpful!"],
|
| 50 |
+
["I'm frustrated, nothing works today."],
|
| 51 |
+
["The weather is okay, I guess."],
|
| 52 |
+
],
|
| 53 |
+
inputs=inp,
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
demo.queue()
|
| 57 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=6.0.0
|
| 2 |
+
huggingface_hub>=0.23.0
|
| 3 |
+
|