pavan2606 commited on
Commit
6f02c54
โ€ข
1 Parent(s): 8c47fd0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +250 -0
app.py CHANGED
@@ -0,0 +1,250 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from fastai.vision.all import *
3
+ import gradio as gr
4
+ import pickle
5
+ import tempfile
6
+ from transformers import AutoTokenizer, AutoModelWithLMHead
7
+ from speechbrain.inference.interfaces import foreign_class
8
+
9
+
10
+
11
+ # Facial expression classifier
12
+
13
+ # Emotion
14
+ learn_emotion = load_learner('emotions_vgg.pkl')
15
+ learn_emotion_labels = learn_emotion.dls.vocab
16
+
17
+
18
+ # Predict
19
+ def predict(img):
20
+ img = PILImage.create(img)
21
+ pred_emotion, pred_emotion_idx, probs_emotion = learn_emotion.predict(img)
22
+ predicted_emotion = learn_emotion_labels[pred_emotion_idx]
23
+ return predicted_emotion
24
+
25
+
26
+ # Gradio
27
+ title = "Facial Emotion Detector"
28
+
29
+ description = gr.Markdown(
30
+ """Ever wondered what a person might be feeling looking at their picture?
31
+ Well, now you can! Try this fun app. Just upload a facial image in JPG or
32
+ PNG format. You can now see what they might have felt when the picture
33
+ was taken.
34
+
35
+ **Tip**: Be sure to only include face to get best results. Check some sample images
36
+ below for inspiration!""").value
37
+
38
+ article = gr.Markdown(
39
+ """**DISCLAIMER:** This model does not reveal the actual emotional state of a person. Use and
40
+ interpret results at your own risk!.
41
+
42
+ **PREMISE:** The idea is to determine an overall emotion of a person
43
+ based on the pictures. We are restricting pictures to only include close-up facial
44
+ images.
45
+
46
+ **DATA:** FER2013 dataset consists of 48x48 pixel grayscale images of faces.Images
47
+ are assigned one of the 7 emotions: Angry, Disgust, Fear, Happy, Sad, Surprise, and Neutral.
48
+
49
+ """).value
50
+
51
+ enable_queue=True
52
+
53
+ examples = ["happy1.jpg","happy2.jpeg","netural.jpg","sad.jpeg","surprise.jpeg"]
54
+
55
+ image_mode=gr.Interface(fn = predict,
56
+ inputs = gr.Image( image_mode='L',label='Image'),
57
+ outputs = [gr.Label(label='Emotion')], #gr.Label(),
58
+ title = title,
59
+ examples = examples,
60
+ description = description,
61
+ article=article,
62
+ allow_flagging='never')
63
+
64
+
65
+
66
+
67
+ # Txet Model
68
+
69
+ # Load tokenizer and model from pickles
70
+ with open("emotion_tokenizer.pkl", "rb") as f:
71
+ tokenizer = pickle.load(f)
72
+
73
+ with open("emotion_model.pkl", "rb") as f:
74
+ model = pickle.load(f)
75
+
76
+
77
+
78
+ def classify_emotion(text):
79
+ # Tokenize input text and generate output
80
+ input_ids = tokenizer.encode("emotion: " + text, return_tensors="pt")
81
+ output = model.generate(input_ids)
82
+ output_text = tokenizer.decode(output[0], skip_special_tokens=True)
83
+
84
+ # Classify the emotion into positive, negative, or neutral
85
+ if output_text in ["joy", "love"]:
86
+ return "Positive"
87
+ elif output_text == "surprise":
88
+ return "Neutral"
89
+ else:
90
+ return "Negative"
91
+ return output_text
92
+
93
+
94
+
95
+
96
+ # Gradio
97
+ text_title = "Text Emotion Detector"
98
+
99
+ text_description = gr.Markdown(
100
+ """# Text Emotion Detector: The Mood Meter ๐Ÿ˜„๐Ÿคจ๐Ÿ˜ 
101
+
102
+ ## Introduction
103
+ Welcome to our Text Emotion Detector, also known as The Mood Meter! ๐ŸŽญ This nifty tool helps you decipher the emotional rollercoaster hidden within any piece of text. Whether it's an enthusiastic rave, a stone-cold report, or a fiery rant, we'll break it down into three simple categories: positive, neutral, or negative vibes. Let's have some fun with words! ๐Ÿš€
104
+
105
+ ## How It Works
106
+ Our Mood Meter uses top-notch text analysis wizardry to understand the feels behind your words. We've trained it on mountains of text data and equipped it with cutting-edge algorithms to accurately predict whether your text is partying in positivity, chilling in neutrality, or burning with negativity. ๐Ÿ”ฅ
107
+
108
+
109
+ ## Usage: Let's Play the Sentiment Game! ๐ŸŽฎ๐ŸŽ‰
110
+
111
+ 1. Type your text into the "Mood-o-Matic" text box. Bonus points for creativity! Remember, the weirder, the better! ๐Ÿคช
112
+ 2. Summon the Mood Genie by clicking the "Submit" button. Watch out for the magical sparkles! โœจโœจ
113
+ 3. Hold onto your hats (or tiaras) as the Mood-o-Matic works its mojo and reveals the sentiment verdict. Will it be sunshine and rainbows, or thunderclouds and lightning bolts? โšกโ˜€๏ธ
114
+ 4. React accordingly: If it's positive, do a happy dance worthy of a TikTok trend. If it's neutral, give a nonchalant nod like a cool cat. And if it's negative, let out a dramatic gasp that could rival a soap opera cliffhanger. Let's embrace the theatrics of sentiment analysis! ๐ŸŽญ
115
+
116
+ Now, who said sentiment analysis couldn't be the highlight of your day? Let's turn those words into a mood-boosting adventure! ๐Ÿš€""").value
117
+
118
+ text_article = gr.Markdown(
119
+ """
120
+ ## Premise
121
+ We believe that understanding text sentiment shouldn't be dull. It's about diving into the sea of human expression and surfacing with a smile (or a frown, depending on the text). Our Mood Meter is here to make sentiment analysis a delightful journey. ๐ŸŒˆ
122
+
123
+ ## Data
124
+ Our Mood Meter has gobbled up datasets filled with everything from Shakespearean sonnets to social media rants. We've trained it to handle diverse dialects, and writing styles. Because emotions don't stick to a script, and neither do we! ๐ŸŒ
125
+
126
+ ## Disclaimer
127
+ While The Mood Meter aims to tickle your funny bone while analyzing sentiment, remember that text analysis is an art, not an exact science. Take our results with a sprinkle of salt (or confetti) and always trust your gut (or your funny bone). ๐Ÿ˜‰โœจ
128
+ """).value
129
+
130
+ enable_queue=True
131
+
132
+
133
+ text_examples=["I aced my exam and received praise from my teacher for my hard work.",
134
+ "I just got a promotion at work, and I'm feeling on top of the world!",
135
+ "The sudden change in weather surprised everyone, but it didn't cause any inconvenience.",
136
+ "I accidentally spilled coffee on my laptop, causing it to malfunction.",
137
+ "I burnt my dinner while trying out a new recipe, and now I have nothing to eat."
138
+
139
+ ]
140
+
141
+
142
+ text_model=gr.Interface(fn = classify_emotion,
143
+ inputs = gr.Textbox( label='Text'),
144
+ outputs = [gr.Textbox(label='Emotion')],
145
+ title = text_title,
146
+ examples = text_examples,
147
+ description = text_description,
148
+ article=text_article,
149
+ allow_flagging='never')
150
+
151
+
152
+
153
+
154
+
155
+
156
+
157
+
158
+
159
+
160
+
161
+
162
+ # Initialize the classifier
163
+ classifier = foreign_class(source="speechbrain/emotion-recognition-wav2vec2-IEMOCAP", pymodule_file="custom_interface.py", classname="CustomEncoderWav2vec2Classifier")
164
+
165
+ def save_uploaded_file(uploaded_file):
166
+ temp_dir = tempfile.TemporaryDirectory()
167
+ file_path = os.path.join(temp_dir.name, uploaded_file.name)
168
+ with open(file_path, "wb") as f:
169
+ f.write(uploaded_file.getbuffer())
170
+ return file_path
171
+
172
+
173
+ def emotion(file_path):
174
+
175
+ if file_path:
176
+ # Classify the file
177
+ out_prob, score, index, text_lab = classifier.classify_file(file_path)
178
+ if isinstance(text_lab, list):
179
+ text_lab = text_lab[0]
180
+ # Map the original labels to the desired categories
181
+ emotion_mapping = {
182
+ 'neu': 'Neutral',
183
+ 'ang': 'Angry',
184
+ 'hap': 'Happy',
185
+ 'sad': 'Sadness'
186
+ }
187
+ # Get the corresponding category from the mapping
188
+ emotion_category = emotion_mapping.get(text_lab, 'Unknown')
189
+
190
+ emotion_category = emotion_mapping.get(text_lab, 'Unknown')
191
+ # Return the emotion category
192
+ return emotion_category
193
+ else:
194
+ return "Please provide the path to an audio file."
195
+
196
+
197
+
198
+
199
+
200
+ audio_model = gr.Interface(fn=emotion, inputs="textbox", outputs="textbox")
201
+
202
+
203
+
204
+
205
+ #Home Page
206
+ HP_title = "Multimodal Sentiment Analysis: Feel the Emotion in Every Pixel, Word, and Sound!"
207
+ HP_description = gr.Markdown(
208
+ """
209
+ ## Hey There! ๐ŸŒŸ
210
+ ### Welcome to our spectacular project, Multimodal Sentiment Analysis! ๐Ÿš€ Here, we're all about unraveling the emotions tucked away in text, audio, and images. Think of us as your personal emotion whisperers across various platforms!
211
+ ## Why It's So Darn Cool ๐Ÿ˜Ž
212
+ Imagine this: understanding emotions unlocks the door to understanding people better. With our project, we're diving headfirst into a pool of sentiments! From heartwarming messages to catchy tunes, and from breathtaking landscapes to hilarious memes, we're decoding it all!
213
+ ## Explore Emotions Effortlessly with Tabs! ๐Ÿ“‘
214
+ Navigate through emotions seamlessly with our nifty tabs:
215
+ - **Text Emotion Recognition**: Unravel the emotional rollercoaster hidden in every word!
216
+ - **Image Emotion Recognition**: Peek into the feelings behind every snapshot!
217
+ - **Audio Emotion Recognition**: Tune in to the vibes of emotions with every sound clip!
218
+ ## Meet Our Awesome Models ๐ŸŒŸ
219
+ ### 1. Text Emotion Recognition
220
+ This model is your go-to buddy for understanding the emotional vibe in written text! Whether it's a love letter or a tweet storm, our Text Emotion Recognition model has got your back, decrypting emotions like a champ!
221
+ ### 2. Image Emotion Recognition
222
+ Ever wondered what feelings those grins, frowns, and winks in photos convey? Our Image Emotion Recognition model spills the beans! It's like having a personal mood interpreter for every pic you snap!
223
+ ### 3. Audio Emotion Recognition
224
+ Listen up! Our Audio Emotion Recognition model tunes in to the subtle nuances of voice, capturing emotions in every syllable! From giggles to sobs and everything in between, it's your trusty sidekick for decoding the melodies of emotions!
225
+ ## Ready to Dive In? ๐Ÿš€
226
+ Getting started with Multimodal Sentiment Analysis is as easy as pie! Grab our user-friendly APIs and libraries, plug in the models for text, image, and audio emotion recognition, and voilร ! You'll be swimming in the sea of emotions like a pro in no time!
227
+ ## Meet the Fabulous Team Behind the Magic! ๐Ÿง 
228
+ Let's give a round of applause to the brilliant minds who made it all happen:
229
+ - **Pavan**: The wordsmith behind the Text Emotion Recognition model, spinning magic with language and algorithms!
230
+ - **Abhiram**: The visionary behind the Image Emotion Recognition model, bringing pixels to life with emotion decoding powers!
231
+ - **Karthik**: The audio maestro shaping the Audio Emotion Recognition model, capturing the symphony of emotions in every sound wave!
232
+ - **Ganesh**: The glue holding it all together, orchestrating the dance of emotions and teamwork!
233
+ """).value
234
+
235
+ def greet(name):
236
+ pass
237
+ home_page=gr.Interface(fn = greet,
238
+ inputs = gr.Textbox(label="Hey there! Ready to spice up your title game? Drop your name, and let's turn it into a giggling sensation!"
239
+ ),
240
+ outputs = None,
241
+ description = HP_description,
242
+ theme='gradio/monochrome',
243
+ title=HP_title,
244
+ allow_flagging='never')
245
+
246
+
247
+
248
+ main_model = gr.TabbedInterface([home_page,text_model, image_mode,audio_model], ["Home Page","Text Emotion Recognition", "Image Emotion Recognition" , "Audio Emotion Recognition"],theme='gradio/monochrome')
249
+
250
+ main_model.launch()