Shreyansh49's picture
Create app.py
56027ff verified
import gradio as gr
from transformers import pipeline
# Load the sentiment analysis model
sentiment_analysis = pipeline(
"sentiment-analysis",
framework="pt",
model="SamLowe/roberta-base-go_emotions"
)
def analyze_sentiment(text):
results = sentiment_analysis(text)
# Sort the results by score in descending order
sorted_results = sorted(results, key=lambda x: x['score'], reverse=True)
# Select the top 3 labels and their scores
top_3_labels_scores = {result['label']: result['score'] for result in sorted_results[:3]}
return top_3_labels_scores
def get_sentiment_emoji(sentiment):
emoji_mapping = {
"disappointment": "๐Ÿ˜ž",
"sadness": "๐Ÿ˜ข",
"annoyance": "๐Ÿ˜ ",
"neutral": "๐Ÿ˜",
"disapproval": "๐Ÿ‘Ž",
"realization": "๐Ÿ˜ฎ",
"nervousness": "๐Ÿ˜ฌ",
"approval": "๐Ÿ‘",
"joy": "๐Ÿ˜„",
"anger": "๐Ÿ˜ก",
"embarrassment": "๐Ÿ˜ณ",
"caring": "๐Ÿค—",
"remorse": "๐Ÿ˜”",
"disgust": "๐Ÿคข",
"grief": "๐Ÿ˜ฅ",
"confusion": "๐Ÿ˜•",
"relief": "๐Ÿ˜Œ",
"desire": "๐Ÿ˜",
"admiration": "๐Ÿ˜Œ",
"optimism": "๐Ÿ˜Š",
"fear": "๐Ÿ˜จ",
"love": "โค๏ธ",
"excitement": "๐ŸŽ‰",
"curiosity": "๐Ÿค”",
"amusement": "๐Ÿ˜„",
"surprise": "๐Ÿ˜ฒ",
"gratitude": "๐Ÿ™",
"pride": "๐Ÿฆ"
}
return emoji_mapping.get(sentiment, "")
def display_sentiment_results(sentiment_results, option):
sentiment_text = ""
for sentiment, score in sentiment_results.items():
emoji = get_sentiment_emoji(sentiment)
score_percentage = score * 100
if option == "Sentiment Only":
sentiment_text += f"{sentiment} {emoji}\n"
elif option == "Sentiment + Score":
sentiment_text += f"{sentiment} {emoji}: {score_percentage:.2f}%\n"
return sentiment_text
def inference(text_input, sentiment_option):
sentiment_results = analyze_sentiment(text_input)
sentiment_output = display_sentiment_results(sentiment_results, sentiment_option)
return sentiment_output
title = "๐ŸŽค Gradio UI"
description = "we have deployed our model on Gradio"
block = gr.Blocks()
with block:
gr.Markdown("# ๐Ÿ•ต๏ธ")
gr.Markdown("Between the Lines, Emotions Speak ๐Ÿคซ๐Ÿ“– - Decode the Silent Echoes with Mood Reader ๐Ÿ•ต๏ธโ€โ™‚๏ธ๐Ÿ’ฌ Every Sentence with Mood Reader ๐Ÿ•ต๏ธโ€โ™‚๏ธ๐Ÿ’ฌ")
with gr.Column():
text_input = gr.Textbox(label="Input Text", lines=4)
sentiment_option = gr.Radio(choices=["Sentiment Only", "Sentiment + Score"], label="Select an option")
analyze_btn = gr.Button("Analyze")
sentiment_output = gr.Textbox(label="Sentiment Analysis Results")
analyze_btn.click(
inference,
inputs=[text_input, sentiment_option],
outputs=[sentiment_output]
)
block.launch()