import pandas as pd from transformers import AutoTokenizer, AutoModelForSequenceClassification import random import gradio as gr import torch # Check if GPU is available and set the device device = torch.device("cuda" if torch.cuda.is_available() else "cpu") print(f"Using device: {device}") # Path to your model file (adjust this if needed) model_name = "bhadresh-savani/bert-base-uncased-emotion" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForSequenceClassification.from_pretrained(model_name) def predict_emotion(text): # Tokenize the input text inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512) # Perform inference outputs = model(**inputs) predictions = torch.softmax(outputs.logits, dim=1) # Get the predicted emotion label predicted_label = torch.argmax(predictions, dim=1).item() # Map the label to emotion names label_to_emotion = { 0: 'anger', 1: 'joy', 2: 'optimism', 3: 'sadness', 4: 'fear', 5: 'surprise', } emotion = label_to_emotion[predicted_label] confidence = predictions[0][predicted_label].item() return emotion, confidence # Motivational quotes for each emotion motivational_quotes = { "anger": [ "It’s okay to feel angry when things don’t go as expected. Your emotions are valid and show you care deeply. Take a moment to breathe and channel that energy into finding a solution—you’ve got the strength to turn this around!" ], "joy": [ "Your joy is contagious, and it’s wonderful to see you so happy! Celebrate this moment fully, and let it inspire you to keep reaching for more moments like these." ], "optimism": [ "Your positive outlook is inspiring! Keep holding onto that hope and belief in yourself—it will guide you through any challenge. The best is yet to come!" ], "sadness": [ "It’s okay to feel sad—allow yourself to process and heal. Remember, tough times don’t last forever. Lean on those who care about you, and know that brighter days are ahead." ], "fear": [ "Feeling afraid is natural; it shows you’re stepping outside your comfort zone. Trust in your abilities and take things one step at a time. You’re braver than you think!" ], "surprise": [ "Life’s surprises can be startling or exciting, but they often bring new opportunities. Embrace the unexpected with an open mind—you might discover something amazing." ] } # Function to generate motivational quotes def get_motivational_quote(emotion): if emotion in motivational_quotes: return random.choice(motivational_quotes[emotion]) else: return "Stay strong and keep moving forward!" def process_entry(date, text): emotion, confidence = predict_emotion(text) quote = get_motivational_quote(emotion) return f"On {date}, your detected emotion is: {emotion} (Confidence: {confidence:.2f})", quote interface = gr.Interface( fn=process_entry, inputs=[ gr.Textbox(lines=1, placeholder="Enter date of entry (DD/MM/YYYY):", label="Enter date of entry (DD/MM/YYYY):"), gr.Textbox(lines=2, placeholder="Enter your thoughts and feelings:", label="Enter your thoughts and feelings:") ], outputs=[ gr.Textbox(lines=2, placeholder="Detected Emotion:", label="Detected Emotion:"), gr.Textbox(lines=2, placeholder="Motivational Quote:", label="Here is something to tell yourself:") ], title="Feel Free to Express Yourself!", description="A safe space for you to express your feelings and recognize them!" ) # Launch the Gradio app with public sharing enabled interface.launch()