gregmerritt commited on
Commit
ea0e40d
·
verified ·
1 Parent(s): b4856b0

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -70
app.py DELETED
@@ -1,70 +0,0 @@
1
-
2
- import torch
3
- import torch.nn as nn
4
- import torchvision.transforms as transforms
5
- from PIL import Image
6
- import gradio as gr
7
-
8
- # Define your model class (must match the architecture used for training)
9
- class EmotionModel(nn.Module):
10
- def __init__(self):
11
- super(EmotionModel, self).__init__()
12
- self.model = nn.Sequential(
13
- nn.Conv2d(1, 32, 3, padding=1), # First conv layer: 32 channels
14
- nn.ReLU(),
15
- nn.MaxPool2d(2, 2),
16
-
17
- nn.Conv2d(32, 64, 3, padding=1), # Second conv layer: 64 channels
18
- nn.ReLU(),
19
- nn.MaxPool2d(2, 2),
20
-
21
- nn.Flatten(),
22
- nn.Linear(64 * 12 * 12, 128), # Matches flatten shape 64*12*12=9216
23
- nn.ReLU(),
24
- nn.Linear(128, 7) # 7 emotion classes
25
- )
26
-
27
- def forward(self, x):
28
- return self.model(x)
29
-
30
- # Load model
31
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
32
- model = EmotionModel()
33
- model.load_state_dict(torch.load("emotion_model.pth", map_location=device))
34
- model.eval()
35
-
36
- # Class labels (order must match training)
37
- EMOTIONS = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
38
-
39
- # Image transform
40
- transform = transforms.Compose([
41
- transforms.Grayscale(),
42
- transforms.Resize((48, 48)),
43
- transforms.ToTensor(),
44
- ])
45
-
46
- # Prediction function
47
- def predict_emotion(img):
48
- if img is None:
49
- return "⚠️ No image received. Please upload or take a photo."
50
-
51
- img = transform(img).unsqueeze(0).to(device)
52
- with torch.no_grad():
53
- output = model(img)
54
- _, predicted = torch.max(output, 1)
55
- return f"Predicted Emotion: {EMOTIONS[predicted.item()]}"
56
-
57
- # Gradio UI
58
- with gr.Blocks() as demo:
59
- gr.Markdown("## 😊 Emotion Detection App")
60
-
61
- with gr.Row():
62
- webcam = gr.Image(type="pil", label="Upload or Take Photo")
63
- output = gr.Textbox(label="Prediction")
64
-
65
- with gr.Row():
66
- predict_btn = gr.Button("Predict Emotion")
67
-
68
- predict_btn.click(fn=predict_emotion, inputs=webcam, outputs=output)
69
-
70
- demo.launch()