File size: 799 Bytes
0efecf4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# βœ… 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()