Spaces:
Running
Running
| import gradio as gr | |
| from transformers import pipeline | |
| # Load a tiny, fast model — runs on CPU in seconds | |
| sentiment = pipeline( | |
| "sentiment-analysis", | |
| model="distilbert-base-uncased-finetuned-sst-2-english", | |
| ) | |
| def analyze(text: str) -> str: | |
| if not text.strip(): | |
| return "Please enter some text." | |
| result = sentiment(text)[0] | |
| label = result["label"].capitalize() | |
| score = round(result["score"] * 100, 1) | |
| emoji = "😊" if label == "Positive" else "😞" | |
| return f"{emoji} **{label}** ({score}% confidence)" | |
| demo = gr.Interface( | |
| fn=analyze, | |
| inputs=gr.Textbox( | |
| label="Your text", | |
| placeholder="Type anything… e.g. 'I love building AI apps!'", | |
| lines=3, | |
| ), | |
| outputs=gr.Markdown(label="Sentiment"), | |
| title="🤗 Hello World — AI Sentiment Analysis", | |
| description=( | |
| "Type a sentence and the model will tell you whether it sounds " | |
| "**positive** or **negative**. Powered by DistilBERT on 🤗 Transformers." | |
| ), | |
| examples=[ | |
| ["I love learning about machine learning!"], | |
| ["This assignment is really frustrating."], | |
| ["The weather is okay today."], | |
| ], | |
| cache_examples=False, | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |