Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import os
|
| 3 |
+
from transformers import pipeline
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
# Load the sentiment-analysis pipeline once (cached in memory).
|
| 7 |
+
# Model: distilBERT fine-tuned on SST-2. Swap this string with another HF model if you want.
|
| 8 |
+
MODEL_NAME = "distilbert-base-uncased-finetuned-sst-2-english"
|
| 9 |
|
| 10 |
+
# Instantiate the pipeline (this downloads weights the first time).
|
| 11 |
+
sentiment_pipe = pipeline("sentiment-analysis", model=MODEL_NAME, tokenizer=MODEL_NAME)
|
| 12 |
+
|
| 13 |
+
def analyze_sentiment(text: str):
|
| 14 |
+
"""
|
| 15 |
+
Analyze sentiment for `text` and return:
|
| 16 |
+
- a dict of label probabilities (for gr.Label component)
|
| 17 |
+
- a human-readable label + score string
|
| 18 |
+
"""
|
| 19 |
+
if not text or not text.strip():
|
| 20 |
+
return {"POSITIVE": 0.0, "NEGATIVE": 0.0}, "No input provided."
|
| 21 |
+
|
| 22 |
+
raw = sentiment_pipe(text[:1000])[0] # truncate long text to 1000 chars to keep latency reasonable
|
| 23 |
+
label = raw["label"] # "POSITIVE" or "NEGATIVE"
|
| 24 |
+
score = float(raw["score"])
|
| 25 |
+
|
| 26 |
+
# Provide both label probabilities so the Label component can show a nice bar
|
| 27 |
+
if label.upper() == "POSITIVE":
|
| 28 |
+
probs = {"POSITIVE": score, "NEGATIVE": 1.0 - score}
|
| 29 |
+
else:
|
| 30 |
+
probs = {"POSITIVE": 1.0 - score, "NEGATIVE": score}
|
| 31 |
+
|
| 32 |
+
pretty = f"{label} (confidence: {score:.2f})"
|
| 33 |
+
return probs, pretty
|
| 34 |
+
|
| 35 |
+
# Build Gradio UI
|
| 36 |
+
title = "Simple Sentiment Classifier (Transformers → Gradio)"
|
| 37 |
+
description = "Type some text and the model will predict sentiment (positive/negative). Uses a Hugging Face transformers sentiment model in the backend."
|
| 38 |
+
|
| 39 |
+
with gr.Blocks() as demo:
|
| 40 |
+
gr.Markdown(f"# {title}")
|
| 41 |
+
gr.Markdown(description)
|
| 42 |
+
|
| 43 |
+
with gr.Row():
|
| 44 |
+
txt = gr.Textbox(lines=6, placeholder="Enter text to analyze...", label="Input text")
|
| 45 |
+
# Left: probabilities shown as bars. Right: human readable label
|
| 46 |
+
out_probs = gr.Label(label="Predicted probabilities")
|
| 47 |
+
out_pretty = gr.Textbox(label="Predicted label", interactive=False)
|
| 48 |
+
|
| 49 |
+
submit = gr.Button("Analyze")
|
| 50 |
+
|
| 51 |
+
# Wire inputs -> function
|
| 52 |
+
submit.click(fn=analyze_sentiment, inputs=txt, outputs=[out_probs, out_pretty])
|
| 53 |
+
|
| 54 |
+
# If you deploy on Hugging Face Spaces, they run the app automatically; otherwise run locally.
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
# Use environment variable PORT for cloud hosts (Spaces sets it automatically)
|
| 57 |
+
port = int(os.environ.get("PORT", 7860))
|
| 58 |
+
demo.launch(server_name="0.0.0.0", server_port=port)
|