| import gradio as gr |
| from transformers import pipeline |
|
|
| |
| sentiment_pipeline = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment") |
|
|
| |
| def analyze_sentiment(text): |
| result = sentiment_pipeline(text) |
| label = result[0]['label'] |
| score = int(label.split()[0]) |
| |
| |
| |
| sentiment_score = (score - 3) / 2 |
| |
| return sentiment_score |
|
|
| |
| examples = [ |
| ["I love using Hugging Face Transformers!"], |
| ["The product is terrible and I will never buy it again."], |
| ["This is the best experience I've ever had!"], |
| ["The movie was okay, not great but not bad either."], |
| ["I am extremely disappointed with the service."] |
| ] |
|
|
| |
| interface = gr.Interface( |
| fn=analyze_sentiment, |
| inputs=gr.Textbox(label="Enter text for sentiment analysis", placeholder="Type your text here..."), |
| outputs=gr.Number(label="Sentiment Score (-1 to 1)"), |
| examples=examples, |
| title="Sentiment Analysis with Hugging Face Transformers", |
| description=""" |
| Enter a sentence to analyze its sentiment. The model will output a sentiment score between -1 and 1: |
| - **-1**: Extremely Negative |
| - **0**: Neutral |
| - **+1**: Extremely Positive |
| This uses the `nlptown/bert-base-multilingual-uncased-sentiment` model. |
| """ |
| ) |
|
|
| |
| if __name__ == "__main__": |
| interface.launch() |
|
|