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()
|