AbdulazizAlhelabi's picture
Update app.py
0efecf4 verified
raw
history blame contribute delete
799 Bytes
# βœ… 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()