File size: 2,800 Bytes
56027ff |
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
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()
|