Shreyansh49 commited on
Commit
56027ff
ยท
verified ยท
1 Parent(s): 9ecc7b8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -0
app.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # Load the sentiment analysis model
5
+ sentiment_analysis = pipeline(
6
+ "sentiment-analysis",
7
+ framework="pt",
8
+ model="SamLowe/roberta-base-go_emotions"
9
+ )
10
+
11
+ def analyze_sentiment(text):
12
+ results = sentiment_analysis(text)
13
+ # Sort the results by score in descending order
14
+ sorted_results = sorted(results, key=lambda x: x['score'], reverse=True)
15
+ # Select the top 3 labels and their scores
16
+ top_3_labels_scores = {result['label']: result['score'] for result in sorted_results[:3]}
17
+ return top_3_labels_scores
18
+
19
+ def get_sentiment_emoji(sentiment):
20
+ emoji_mapping = {
21
+ "disappointment": "๐Ÿ˜ž",
22
+ "sadness": "๐Ÿ˜ข",
23
+ "annoyance": "๐Ÿ˜ ",
24
+ "neutral": "๐Ÿ˜",
25
+ "disapproval": "๐Ÿ‘Ž",
26
+ "realization": "๐Ÿ˜ฎ",
27
+ "nervousness": "๐Ÿ˜ฌ",
28
+ "approval": "๐Ÿ‘",
29
+ "joy": "๐Ÿ˜„",
30
+ "anger": "๐Ÿ˜ก",
31
+ "embarrassment": "๐Ÿ˜ณ",
32
+ "caring": "๐Ÿค—",
33
+ "remorse": "๐Ÿ˜”",
34
+ "disgust": "๐Ÿคข",
35
+ "grief": "๐Ÿ˜ฅ",
36
+ "confusion": "๐Ÿ˜•",
37
+ "relief": "๐Ÿ˜Œ",
38
+ "desire": "๐Ÿ˜",
39
+ "admiration": "๐Ÿ˜Œ",
40
+ "optimism": "๐Ÿ˜Š",
41
+ "fear": "๐Ÿ˜จ",
42
+ "love": "โค๏ธ",
43
+ "excitement": "๐ŸŽ‰",
44
+ "curiosity": "๐Ÿค”",
45
+ "amusement": "๐Ÿ˜„",
46
+ "surprise": "๐Ÿ˜ฒ",
47
+ "gratitude": "๐Ÿ™",
48
+ "pride": "๐Ÿฆ"
49
+ }
50
+ return emoji_mapping.get(sentiment, "")
51
+
52
+ def display_sentiment_results(sentiment_results, option):
53
+ sentiment_text = ""
54
+ for sentiment, score in sentiment_results.items():
55
+ emoji = get_sentiment_emoji(sentiment)
56
+ score_percentage = score * 100
57
+ if option == "Sentiment Only":
58
+ sentiment_text += f"{sentiment} {emoji}\n"
59
+ elif option == "Sentiment + Score":
60
+ sentiment_text += f"{sentiment} {emoji}: {score_percentage:.2f}%\n"
61
+ return sentiment_text
62
+
63
+ def inference(text_input, sentiment_option):
64
+ sentiment_results = analyze_sentiment(text_input)
65
+ sentiment_output = display_sentiment_results(sentiment_results, sentiment_option)
66
+
67
+ return sentiment_output
68
+
69
+ title = "๐ŸŽค Gradio UI"
70
+ description = "we have deployed our model on Gradio"
71
+
72
+ block = gr.Blocks()
73
+
74
+ with block:
75
+ gr.Markdown("# ๐Ÿ•ต๏ธ")
76
+ gr.Markdown("Between the Lines, Emotions Speak ๐Ÿคซ๐Ÿ“– - Decode the Silent Echoes with Mood Reader ๐Ÿ•ต๏ธโ€โ™‚๏ธ๐Ÿ’ฌ Every Sentence with Mood Reader ๐Ÿ•ต๏ธโ€โ™‚๏ธ๐Ÿ’ฌ")
77
+ with gr.Column():
78
+ text_input = gr.Textbox(label="Input Text", lines=4)
79
+ sentiment_option = gr.Radio(choices=["Sentiment Only", "Sentiment + Score"], label="Select an option")
80
+ analyze_btn = gr.Button("Analyze")
81
+ sentiment_output = gr.Textbox(label="Sentiment Analysis Results")
82
+
83
+ analyze_btn.click(
84
+ inference,
85
+ inputs=[text_input, sentiment_option],
86
+ outputs=[sentiment_output]
87
+ )
88
+
89
+ block.launch()