| import gradio as gr |
| from transformers import pipeline |
|
|
| |
| sentiment_analysis = pipeline( |
| "sentiment-analysis", |
| framework="pt", |
| model="SamLowe/roberta-base-go_emotions" |
| ) |
|
|
| def analyze_sentiment(text): |
| results = sentiment_analysis(text) |
| |
| sorted_results = sorted(results, key=lambda x: x['score'], reverse=True) |
| |
| 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() |
|
|