import os from fastai.vision.all import * import gradio as gr import pickle import tempfile from transformers import AutoTokenizer, AutoModelWithLMHead from speechbrain.inference.interfaces import foreign_class # Facial expression classifier # Emotion learn_emotion = load_learner('emotions_vgg.pkl') learn_emotion_labels = learn_emotion.dls.vocab # Predict def predict(img): img = PILImage.create(img) pred_emotion, pred_emotion_idx, probs_emotion = learn_emotion.predict(img) predicted_emotion = learn_emotion_labels[pred_emotion_idx] return predicted_emotion # Gradio title = "Facial Emotion Detector" description = gr.Markdown( """Ever wondered what a person might be feeling looking at their picture? Well, now you can! Try this fun app. Just upload a facial image in JPG or PNG format. You can now see what they might have felt when the picture was taken. **Tip**: Be sure to only include face to get best results. Check some sample images below for inspiration!""").value article = gr.Markdown( """**DISCLAIMER:** This model does not reveal the actual emotional state of a person. Use and interpret results at your own risk!. **PREMISE:** The idea is to determine an overall emotion of a person based on the pictures. We are restricting pictures to only include close-up facial images. **DATA:** FER2013 dataset consists of 48x48 pixel grayscale images of faces.Images are assigned one of the 7 emotions: Angry, Disgust, Fear, Happy, Sad, Surprise, and Neutral. """).value enable_queue=True examples = ["happy1.jpg","happy2.jpeg","netural.jpg","sad.jpeg","surprise.jpeg"] image_mode=gr.Interface(fn = predict, inputs = gr.Image( image_mode='L'), outputs = [gr.Label(label='Emotion')], #gr.Label(), title = title, examples = examples, description = description, article=article, allow_flagging='never') # Txet Model # Load tokenizer and model from pickles with open("emotion_tokenizer.pkl", "rb") as f: tokenizer = pickle.load(f) with open("emotion_model.pkl", "rb") as f: model = pickle.load(f) def classify_emotion(text): # Tokenize input text and generate output input_ids = tokenizer.encode("emotion: " + text, return_tensors="pt") output = model.generate(input_ids) output_text = tokenizer.decode(output[0], skip_special_tokens=True) # Classify the emotion into positive, negative, or neutral if output_text in ["joy", "love"]: return "Positive" elif output_text == "surprise": return "Neutral" else: return "Negative" return output_text text_model = gr.Interface(fn=classify_emotion, inputs="textbox", outputs="textbox") # Initialize the classifier classifier = foreign_class(source="speechbrain/emotion-recognition-wav2vec2-IEMOCAP", pymodule_file="custom_interface.py", classname="CustomEncoderWav2vec2Classifier") def save_uploaded_file(uploaded_file): temp_dir = tempfile.TemporaryDirectory() file_path = os.path.join(temp_dir.name, uploaded_file.name) with open(file_path, "wb") as f: f.write(uploaded_file.getbuffer()) return file_path def emotion(file_path): if file_path: # Classify the file out_prob, score, index, text_lab = classifier.classify_file(file_path) if isinstance(text_lab, list): text_lab = text_lab[0] # Map the original labels to the desired categories emotion_mapping = { 'neu': 'Neutral', 'ang': 'Angry', 'hap': 'Happy', 'sad': 'Sadness' } # Get the corresponding category from the mapping emotion_category = emotion_mapping.get(text_lab, 'Unknown') emotion_category = emotion_mapping.get(text_lab, 'Unknown') # Return the emotion category return emotion_category else: return "Please provide the path to an audio file." audio_model = gr.Interface(fn=emotion, inputs="textbox", outputs="textbox") #Home Page HP_title = "Multimodal Sentiment Analysis: Feel the Emotion in Every Pixel, Word, and Sound!" HP_description = gr.Markdown( """ ## Hey There! 🌟 ### 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! ## Why It's So Darn Cool 😎 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! ## Explore Emotions Effortlessly with Tabs! 📑 Navigate through emotions seamlessly with our nifty tabs: - **Text Emotion Recognition**: Unravel the emotional rollercoaster hidden in every word! - **Image Emotion Recognition**: Peek into the feelings behind every snapshot! - **Audio Emotion Recognition**: Tune in to the vibes of emotions with every sound clip! ## Meet Our Awesome Models 🌟 ### 1. Text Emotion Recognition 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! ### 2. Image Emotion Recognition 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! ### 3. Audio Emotion Recognition 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! ## Ready to Dive In? 🚀 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! ## Meet the Fabulous Team Behind the Magic! 🧠 Let's give a round of applause to the brilliant minds who made it all happen: - **Pavan**: The wordsmith behind the Text Emotion Recognition model, spinning magic with language and algorithms! - **Abhiram**: The visionary behind the Image Emotion Recognition model, bringing pixels to life with emotion decoding powers! - **Karthik**: The audio maestro shaping the Audio Emotion Recognition model, capturing the symphony of emotions in every sound wave! - **Ganesh**: The glue holding it all together, orchestrating the dance of emotions and teamwork! """).value def greet(name): pass home_page=gr.Interface(fn = greet, 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!" ), outputs = None, description = HP_description, theme='gradio/monochrome', title=HP_title, allow_flagging='never') 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') main_model.launch()