Spaces:
Sleeping
Sleeping
Commit ·
61eff92
1
Parent(s): 53133c0
Add hello world AI sentiment analysis app
Browse filesCo-Authored-By: Oz <oz-agent@warp.dev>
- README.md +24 -6
- app.py +43 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,15 +1,33 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.19.0
|
| 8 |
python_version: '3.13'
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
license: mit
|
| 12 |
-
short_description:
|
| 13 |
---
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Hello World AI Sentiment
|
| 3 |
+
emoji: 🤗
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.19.0
|
| 8 |
python_version: '3.13'
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
license: mit
|
| 12 |
+
short_description: Hello World AI app — sentiment analysis with DistilBERT
|
| 13 |
---
|
| 14 |
|
| 15 |
+
# Hello World — AI Sentiment Analysis
|
| 16 |
+
|
| 17 |
+
A minimal Hugging Face Space for students learning to build AI apps.
|
| 18 |
+
|
| 19 |
+
## What it does
|
| 20 |
+
Classifies any sentence as **Positive** or **Negative** using
|
| 21 |
+
[DistilBERT](https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english),
|
| 22 |
+
a lightweight model fine-tuned on the Stanford Sentiment Treebank.
|
| 23 |
+
|
| 24 |
+
## Stack
|
| 25 |
+
- **UI**: Gradio
|
| 26 |
+
- **Model**: `distilbert-base-uncased-finetuned-sst-2-english`
|
| 27 |
+
- **Runtime**: 🤗 Transformers + PyTorch (CPU)
|
| 28 |
+
|
| 29 |
+
## Run locally
|
| 30 |
+
```bash
|
| 31 |
+
pip install -r requirements.txt
|
| 32 |
+
python app.py
|
| 33 |
+
```
|
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load a tiny, fast model — runs on CPU in seconds
|
| 5 |
+
sentiment = pipeline(
|
| 6 |
+
"sentiment-analysis",
|
| 7 |
+
model="distilbert-base-uncased-finetuned-sst-2-english",
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def analyze(text: str) -> str:
|
| 12 |
+
if not text.strip():
|
| 13 |
+
return "Please enter some text."
|
| 14 |
+
result = sentiment(text)[0]
|
| 15 |
+
label = result["label"].capitalize()
|
| 16 |
+
score = round(result["score"] * 100, 1)
|
| 17 |
+
emoji = "😊" if label == "Positive" else "😞"
|
| 18 |
+
return f"{emoji} **{label}** ({score}% confidence)"
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
demo = gr.Interface(
|
| 22 |
+
fn=analyze,
|
| 23 |
+
inputs=gr.Textbox(
|
| 24 |
+
label="Your text",
|
| 25 |
+
placeholder="Type anything… e.g. 'I love building AI apps!'",
|
| 26 |
+
lines=3,
|
| 27 |
+
),
|
| 28 |
+
outputs=gr.Markdown(label="Sentiment"),
|
| 29 |
+
title="🤗 Hello World — AI Sentiment Analysis",
|
| 30 |
+
description=(
|
| 31 |
+
"Type a sentence and the model will tell you whether it sounds "
|
| 32 |
+
"**positive** or **negative**. Powered by DistilBERT on 🤗 Transformers."
|
| 33 |
+
),
|
| 34 |
+
examples=[
|
| 35 |
+
["I love learning about machine learning!"],
|
| 36 |
+
["This assignment is really frustrating."],
|
| 37 |
+
["The weather is okay today."],
|
| 38 |
+
],
|
| 39 |
+
cache_examples=False,
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
transformers>=4.40.0
|
| 3 |
+
torch>=2.0.0
|