Spaces:
Sleeping
Sleeping
Download app.py from SakshamSna/Sentiment-Analyzers: direct link, hf CLI and curl.
- Browser
- Download file 802 Bytes
-
https://huggingface.co/spaces/SakshamSna/Sentiment-Analyzers/resolve/main/app.py
- Command line
-
hf download hf://spaces/SakshamSna/Sentiment-Analyzers/app.py
-
curl -L -o app.py https://huggingface.co/spaces/SakshamSna/Sentiment-Analyzers/resolve/main/app.py
802 Bytes
| from transformers import pipeline | |
| import gradio as gr | |
| classifier = pipeline("sentiment-analysis") | |
| def analyze_sentiment(text): | |
| if not text: | |
| return "No input", 0 | |
| try: | |
| result = classifier(text)[0] # should return a dict | |
| label = result.get('label', 'Unknown') | |
| score = round(result.get('score', 0), 3) | |
| return label, score | |
| except Exception as e: | |
| return f"Error: {str(e)}", 0 | |
| iface = gr.Interface( | |
| fn=analyze_sentiment, | |
| inputs=gr.Textbox(lines=2, placeholder="Enter text here..."), | |
| outputs=[ | |
| gr.Textbox(label="Sentiment"), | |
| gr.Number(label="Confidence Score") | |
| ], | |
| title="Sentiment Analyzer", | |
| description="Enter a sentence to get its sentiment (POSITIVE/NEGATIVE) and confidence score." | |
| ) | |
| iface.launch() | |