Spaces:
Sleeping
Sleeping
| import torch | |
| import torch.nn as nn | |
| from torchvision import transforms | |
| from PIL import Image | |
| import gradio as gr | |
| from transformers import pipeline | |
| # Load emotion classes | |
| classes = ['Angry', 'Disgust', 'Fear', 'Sad', 'Surprise', 'Neutral', 'Happy'] | |
| # Set device | |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| # Define the actual architecture used for training | |
| class EmotionModel(nn.Module): | |
| def __init__(self): | |
| super(EmotionModel, self).__init__() | |
| self.model = nn.Sequential( | |
| nn.Conv2d(1, 32, 3, padding=1), # Match 32 out_channels | |
| nn.ReLU(), | |
| nn.MaxPool2d(2, 2), | |
| nn.Conv2d(32, 64, 3, padding=1), # Match 64 out_channels | |
| nn.ReLU(), | |
| nn.MaxPool2d(2, 2), | |
| nn.Flatten(), | |
| nn.Linear(64 * 12 * 12, 128), # 64 filters * 12x12 features = 9216 | |
| nn.ReLU(), | |
| nn.Linear(128, 7) | |
| ) | |
| def forward(self, x): | |
| return self.model(x) | |
| # Load the model | |
| model = EmotionModel().to(device) | |
| model.load_state_dict(torch.load("emotion_model.pth", map_location=device)) | |
| model.eval() | |
| # Transformation | |
| transform = transforms.Compose([ | |
| transforms.Grayscale(), | |
| transforms.Resize((48, 48)), | |
| transforms.ToTensor(), | |
| transforms.Normalize((0.5,), (0.5,)) | |
| ]) | |
| # NLP pipeline | |
| gen = pipeline("text-generation", model="distilgpt2") | |
| # Templates with emojis included now | |
| templates = { | |
| "Friendly": { | |
| "Happy": "You seem cheerful π! ", | |
| "Sad": "You look down π’. Here's something to cheer you up... ", | |
| "Angry": "Feeling heated π ? Let's shift the vibe... ", | |
| "Fear": "A little nervous π¨? No worries, try this... ", | |
| "Disgust": "Something bugging you π€’?", | |
| "Surprise": "Whoa! π² That was unexpected?", | |
| "Neutral": "Keeping it chill π?" | |
| }, | |
| "Professional": { | |
| "Happy": "You look ready to go π.", | |
| "Sad": "You seem thoughtful π’.", | |
| "Angry": "Tense vibes π .", | |
| "Fear": "Unsure? π¨", | |
| "Disgust": "Hey! Reset your focus π€’", | |
| "Surprise": "Unexpected moment π²?", | |
| "Neutral": "Yo! Are you redy for this π?" | |
| }, | |
| "Funny": { | |
| "Happy": "Youβre all smiles π!", | |
| "Sad": "You need a laugh π’.", | |
| "Angry": "Cool it down π ", | |
| "Fear": "Letβs laugh the fear away π¨: ", | |
| "Disgust": "Weird day π€’?", | |
| "Surprise": "Surprise! π²", | |
| "Neutral": "Letβs wake things up π with a joke. " | |
| } | |
| } | |
| # Prediction logic | |
| # Emotion to Emoji mapping | |
| emotion_emojis = { | |
| "Angry": "π ", | |
| "Disgust": "π€’", | |
| "Fear": "π¨", | |
| "Happy": "π", | |
| "Sad": "π’", | |
| "Surprise": "π²", | |
| "Neutral": "π" | |
| } | |
| def predict_emotion_and_icebreaker(image, tone): | |
| image = Image.fromarray(image).convert("RGB") | |
| image = transform(image).unsqueeze(0).to(device) | |
| with torch.no_grad(): | |
| output = model(image) | |
| pred = output.argmax(dim=1).item() | |
| emotion = classes[pred] | |
| # Emojis per emotion | |
| emotion_emojis = { | |
| "Happy": "π", "Sad": "π’", "Angry": "π ", | |
| "Fear": "π¨", "Disgust": "π€’", "Surprise": "π²", "Neutral": "π" | |
| } | |
| emoji = emotion_emojis[emotion] | |
| tone_prefix = templates[tone][emotion] | |
| # Prompt for generation | |
| prompt = f"{tone_prefix} {emoji} Try saying:" | |
| response = gen(prompt, max_length=40, num_return_sequences=1, do_sample=True, temperature=0.7, top_k=50)[0]['generated_text'] | |
| clean_response = response.replace(prompt, "").strip().split("\n")[0] | |
| return f"π§ Emotion Detected: {emotion} {emoji}\nπ¬ Icebreaker ({tone}):\n{clean_response}" | |
| # Generate icebreaker with better sampling | |
| generated = gen( | |
| prompt, | |
| max_length=60, | |
| num_return_sequences=1, | |
| do_sample=True, | |
| temperature=0.9, | |
| top_p=0.95 | |
| )[0]['generated_text'] | |
| # Remove the prompt part from the response | |
| response = generated[len(prompt):].strip() | |
| # Fallback if no content was generated | |
| if not response: | |
| response = "Here's a fun question to get started: What's your hidden talent?" | |
| return f"π§ Emotion Detected: {emotion}\nπ¬ Icebreaker ({tone}):\n{response}" | |
| # Interface | |
| webcam_input = gr.Image(type="numpy", label="Upload or Take a Photo") | |
| tone_dropdown = gr.Dropdown(choices=["Friendly", "Professional", "Funny"], value="Friendly", label="Tone") | |
| demo = gr.Interface( | |
| fn=predict_emotion_and_icebreaker, | |
| inputs=[webcam_input, tone_dropdown], | |
| outputs="text", | |
| title="Emotion + Icebreaker Generator", | |
| description="Upload or capture a face photo. AI will predict the emotion and generate a tone-specific conversation starter." | |
| ) | |
| demo.launch() | |