Spaces:
Configuration error
Configuration error
Upload 4 files
Browse files- README.md +46 -14
- app.py +36 -0
- requirements.txt +4 -0
- sentiment.py +48 -0
README.md
CHANGED
|
@@ -1,14 +1,46 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Text Sentiment Analyzer (Local)
|
| 2 |
+
|
| 3 |
+
A **simple local** Gradio app that classifies text sentiment using a Hugging Face Transformers pipeline
|
| 4 |
+
(`distilbert-base-uncased-finetuned-sst-2-english`). No remote APIs.
|
| 5 |
+
|
| 6 |
+
## Quickstart
|
| 7 |
+
|
| 8 |
+
```bash
|
| 9 |
+
python -m venv .venv && source .venv/bin/activate # Windows: .venv\Scripts\activate
|
| 10 |
+
pip install -r requirements.txt
|
| 11 |
+
python app.py
|
| 12 |
+
```
|
| 13 |
+
|
| 14 |
+
Open the printed local URL and paste any text. Use the **Neutral margin** slider to make the
|
| 15 |
+
classifier return **NEUTRAL** when POS vs NEG is too close to call.
|
| 16 |
+
|
| 17 |
+
## Why this fits Deliverable 2
|
| 18 |
+
|
| 19 |
+
- **Runs locally** on your hardware (no `InferenceClient`, no external API).
|
| 20 |
+
- Clear contrast vs. your API-based product from Deliverable 1.
|
| 21 |
+
- Includes **tests** and a **GitHub Action** that runs `pytest` on push/PR.
|
| 22 |
+
|
| 23 |
+
## Project layout
|
| 24 |
+
|
| 25 |
+
```
|
| 26 |
+
sentiment_local/
|
| 27 |
+
├─ app.py
|
| 28 |
+
├─ sentiment.py
|
| 29 |
+
├─ requirements.txt
|
| 30 |
+
├─ README.md
|
| 31 |
+
├─ tests/
|
| 32 |
+
│ └─ test_sentiment.py
|
| 33 |
+
└─ .github/workflows/
|
| 34 |
+
└─ ci.yml
|
| 35 |
+
```
|
| 36 |
+
|
| 37 |
+
## Notes
|
| 38 |
+
|
| 39 |
+
- On the first run, Transformers will **download the model weights** to your machine
|
| 40 |
+
(cached under `~/.cache/huggingface/`). Subsequent runs are fully local.
|
| 41 |
+
- If you need strictly offline installs, pre-download the model:
|
| 42 |
+
```bash
|
| 43 |
+
python -c "from transformers import AutoTokenizer, AutoModelForSequenceClassification; \
|
| 44 |
+
AutoTokenizer.from_pretrained('distilbert-base-uncased-finetuned-sst-2-english'); \
|
| 45 |
+
AutoModelForSequenceClassification.from_pretrained('distilbert-base-uncased-finetuned-sst-2-english')"
|
| 46 |
+
```
|
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from sentiment import analyze_batch, load_pipeline
|
| 4 |
+
|
| 5 |
+
INTRO = """
|
| 6 |
+
# Text Sentiment Analyzer (Local)
|
| 7 |
+
Runs **fully local** using a Hugging Face Transformers pipeline—no external APIs.
|
| 8 |
+
Default model: `distilbert-base-uncased-finetuned-sst-2-english`.
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
pipe = None
|
| 12 |
+
|
| 13 |
+
def predict(text, neutral_margin):
|
| 14 |
+
global pipe
|
| 15 |
+
if not text or not text.strip():
|
| 16 |
+
return [{"label": "NEUTRAL", "score": 1.0}], "Please enter some text."
|
| 17 |
+
if pipe is None:
|
| 18 |
+
pipe = load_pipeline()
|
| 19 |
+
results = analyze_batch([text], pipe=pipe, neutral_margin=neutral_margin)
|
| 20 |
+
pretty = [f"**{r['label']}** (conf: {r['score']:.3f})" for r in results]
|
| 21 |
+
return results, "\n".join(pretty)
|
| 22 |
+
|
| 23 |
+
with gr.Blocks() as demo:
|
| 24 |
+
gr.Markdown(INTRO)
|
| 25 |
+
with gr.Row():
|
| 26 |
+
with gr.Column():
|
| 27 |
+
inp = gr.Textbox(label="Input text", placeholder="Paste a review, tweet, etc.", lines=6)
|
| 28 |
+
neutral_margin = gr.Slider(0.0, 0.5, value=0.15, step=0.01, label="Neutral margin (wider margin => more NEUTRAL)")
|
| 29 |
+
btn = gr.Button("Analyze", variant="primary")
|
| 30 |
+
with gr.Column():
|
| 31 |
+
out_json = gr.JSON(label="Raw output")
|
| 32 |
+
out_md = gr.Markdown(label="Friendly view")
|
| 33 |
+
btn.click(predict, inputs=[inp, neutral_margin], outputs=[out_json, out_md])
|
| 34 |
+
|
| 35 |
+
if __name__ == "__main__":
|
| 36 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.44.0
|
| 2 |
+
transformers>=4.44.2
|
| 3 |
+
torch>=2.2.0
|
| 4 |
+
pytest>=8.2.0
|
sentiment.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
from typing import List, Dict, Optional
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
LABEL_POS = "POSITIVE"
|
| 6 |
+
LABEL_NEG = "NEGATIVE"
|
| 7 |
+
LABEL_NEU = "NEUTRAL"
|
| 8 |
+
|
| 9 |
+
def load_pipeline(model_name: str = "distilbert-base-uncased-finetuned-sst-2-english"):
|
| 10 |
+
"""
|
| 11 |
+
Loads a local sentiment-analysis pipeline.
|
| 12 |
+
NOTE: On first run, Transformers may download model weights to your machine.
|
| 13 |
+
"""
|
| 14 |
+
return pipeline("sentiment-analysis", model=model_name)
|
| 15 |
+
|
| 16 |
+
def soften_to_neutral(pos_score: float, neg_score: float, margin: float) -> Optional[str]:
|
| 17 |
+
"""
|
| 18 |
+
If the top class wins by less than `margin`, return NEUTRAL.
|
| 19 |
+
margin in [0, 0.5]. Larger => more neutral outputs.
|
| 20 |
+
"""
|
| 21 |
+
gap = abs(pos_score - neg_score)
|
| 22 |
+
return LABEL_NEU if gap < margin else None
|
| 23 |
+
|
| 24 |
+
def postprocess(raw_item: Dict, margin: float) -> Dict:
|
| 25 |
+
label = raw_item["label"].upper()
|
| 26 |
+
score = float(raw_item["score"])
|
| 27 |
+
pos = score if label == LABEL_POS else 1.0 - score # two-class trick
|
| 28 |
+
neg = 1.0 - pos
|
| 29 |
+
neutral = soften_to_neutral(pos, neg, margin)
|
| 30 |
+
if neutral:
|
| 31 |
+
return {"label": LABEL_NEU, "score": float(1.0 - abs(pos - neg))}
|
| 32 |
+
# Return winning class + score
|
| 33 |
+
if pos >= neg:
|
| 34 |
+
return {"label": LABEL_POS, "score": pos}
|
| 35 |
+
else:
|
| 36 |
+
return {"label": LABEL_NEG, "score": neg}
|
| 37 |
+
|
| 38 |
+
def analyze_batch(texts: List[str], pipe=None, neutral_margin: float = 0.15) -> List[Dict]:
|
| 39 |
+
"""
|
| 40 |
+
Runs local inference on a list of strings and applies neutral margin postprocessing.
|
| 41 |
+
"""
|
| 42 |
+
if pipe is None:
|
| 43 |
+
pipe = load_pipeline()
|
| 44 |
+
raw = pipe(texts, truncation=True)
|
| 45 |
+
# HF pipeline returns single dict for single input; normalize to list
|
| 46 |
+
if isinstance(raw, dict):
|
| 47 |
+
raw = [raw]
|
| 48 |
+
return [postprocess(item, neutral_margin) for item in raw]
|