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