Spaces:
Sleeping
Sleeping
Download app.py from keerthii05/Sentiment: direct link, hf CLI and curl.
- Browser
- Download file 1.21 kB
-
https://huggingface.co/spaces/keerthii05/Sentiment/resolve/main/app.py
- Command line
-
hf download hf://spaces/keerthii05/Sentiment/app.py
-
curl -L -o app.py https://huggingface.co/spaces/keerthii05/Sentiment/resolve/main/app.py
1.21 kB
| import gradio as gr | |
| from transformers import pipeline | |
| # Load a pre-trained sentiment analysis pipeline | |
| sentiment_analyzer = pipeline( | |
| "sentiment-analysis", | |
| model="cardiffnlp/twitter-roberta-base-sentiment-latest" | |
| ) | |
| def analyze_sentiment(text_to_analyze): | |
| """ | |
| Takes input text and returns the sentiment and confidence score. | |
| """ | |
| if text_to_analyze: | |
| result = sentiment_analyzer(text_to_analyze)[0] | |
| sentiment = result['label'] | |
| confidence = f"{result['score']:.4f}" # Format confidence to 4 decimal places | |
| return f"Sentiment: {sentiment}", f"Confidence: {confidence}" | |
| else: | |
| return "Please enter some text.", "" | |
| # Define the Gradio interface | |
| iface = gr.Interface( | |
| fn=analyze_sentiment, | |
| inputs=gr.Textbox(lines=5, placeholder="Enter text to analyze sentiment..."), | |
| outputs=[ | |
| gr.Textbox(label="Predicted Sentiment"), | |
| gr.Textbox(label="Confidence Score") | |
| ], | |
| title="Simple Sentiment Analyzer", | |
| description="Enter a piece of text and the app will predict its sentiment (Positive, Negative, or Neutral) along with a confidence score.", | |
| ) | |
| # Launch the Gradio app | |
| if __name__ == "__main__": | |
| iface.launch() | |