| # β Step 1: Import Required Libraries | |
| from transformers import pipeline | |
| import gradio as gr | |
| # β Step 2: Load Pretrained Sentiment Model | |
| sentiment_pipeline = pipeline("sentiment-analysis") | |
| # β Step 3: Create Sentiment Function | |
| def get_sentiment(text): | |
| result = sentiment_pipeline(text)[0] | |
| label = result['label'] # e.g., 'POSITIVE' or 'NEGATIVE' | |
| score = round(result['score'], 4) # Round score to 4 decimal places | |
| return label, str(score) | |
| # β Step 4: Build Gradio UI | |
| iface = gr.Interface( | |
| fn=get_sentiment, | |
| inputs=gr.Textbox(lines=3, placeholder="Enter the review here..."), | |
| outputs=[ | |
| gr.Textbox(label="Sentiment"), | |
| gr.Textbox(label="Score") | |
| ], | |
| title="Sentiment analysis prototype" | |
| ) | |
| # β Step 5: Launch App | |
| iface.launch() | |