Ars135 commited on
Commit
6c7d72c
·
verified ·
1 Parent(s): 42a803a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -20
app.py CHANGED
@@ -5,35 +5,31 @@ import torch
5
  from transformers import AutoImageProcessor, AutoModelForImageClassification
6
 
7
  # --- Configuration ---
8
- MODEL_NAME = "nateraw/fer-2013"
9
- # CRITICAL FIX: Explicitly and unconditionally set DEVICE to 'cpu'
10
- DEVICE = "cpu"
11
 
12
  # --- Model and Processor Loading ---
13
  try:
14
  processor = AutoImageProcessor.from_pretrained(MODEL_NAME)
15
 
16
- # CRITICAL FIX: Use map_location='cpu' for memory-safe loading.
17
- # We also use the local_files_only=False default to re-attempt download if necessary.
18
  model = AutoModelForImageClassification.from_pretrained(
19
  MODEL_NAME,
20
  map_location=DEVICE
21
- )
22
- # Ensure all layers are formally moved to the CPU
23
- model.to(DEVICE)
24
- model.eval()
25
 
 
26
  LABELS = model.config.id2label
27
 
28
  print(f"Model loaded successfully on device: {DEVICE}")
29
 
30
  except Exception as e:
31
- # If loading fails, return a highly specific error message to the user.
32
  print(f"CRITICAL ERROR during model loading: {e}")
33
  processor = None
34
  model = None
35
- # This error message directs the user to the only remaining physical solution
36
- LABELS = {0: "System Error: Failed to load model. Likely OOM. Try a paid CPU/GPU tier."}
37
 
38
  # --- Inference Function ---
39
  def classify_emotion(image_np: np.ndarray) -> str:
@@ -42,24 +38,18 @@ def classify_emotion(image_np: np.ndarray) -> str:
42
  return LABELS[0]
43
 
44
  try:
45
- # 1. Convert numpy array to PIL Image
46
  image = Image.fromarray(image_np).convert("RGB")
47
-
48
- # 2. Preprocess
49
  inputs = processor(images=image, return_tensors="pt").to(DEVICE)
50
 
51
- # 3. Run inference
52
  with torch.no_grad():
53
  outputs = model(**inputs)
54
 
55
- # 4. Process predictions
56
  probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
57
  confidence, predicted_class_idx = torch.max(probabilities, 1)
58
 
59
  dominant_emotion = LABELS[predicted_class_idx.item()]
60
  confidence_score = confidence.item()
61
 
62
- # 5. Format the result
63
  result_str = (
64
  f"<h2 class='text-xl font-bold'>Predicted Emotion:</h2>"
65
  f"<p class='text-3xl mt-2'>**{dominant_emotion.upper()}**</p>"
@@ -78,9 +68,9 @@ iface = gr.Interface(
78
  label="Upload an image of a face"
79
  ),
80
  outputs=gr.Markdown(label="Predicted Emotion"),
81
- title="😊 PyTorch Facial Emotion Detection (Optimized)",
82
  description=(
83
- "Upload an image containing a face to classify the dominant emotion. Built for maximum stability on Hugging Face Spaces."
84
  ),
85
  allow_flagging="never",
86
  theme=gr.themes.Soft()
 
5
  from transformers import AutoImageProcessor, AutoModelForImageClassification
6
 
7
  # --- Configuration ---
8
+ # NEW, more stable ViT-based model for emotion detection
9
+ MODEL_NAME = "abhilash88/face-emotion-detection"
10
+ DEVICE = "cpu" # Explicitly set to CPU
11
 
12
  # --- Model and Processor Loading ---
13
  try:
14
  processor = AutoImageProcessor.from_pretrained(MODEL_NAME)
15
 
16
+ # Load model with map_location='cpu' for memory-safe loading.
 
17
  model = AutoModelForImageClassification.from_pretrained(
18
  MODEL_NAME,
19
  map_location=DEVICE
20
+ ).to(DEVICE)
 
 
 
21
 
22
+ model.eval()
23
  LABELS = model.config.id2label
24
 
25
  print(f"Model loaded successfully on device: {DEVICE}")
26
 
27
  except Exception as e:
 
28
  print(f"CRITICAL ERROR during model loading: {e}")
29
  processor = None
30
  model = None
31
+ # If this ViT model fails, the only remaining cause is a lack of RAM.
32
+ LABELS = {0: "HARDWARE FAILURE: Free tier lacks sufficient RAM (OOM). Upgrade required."}
33
 
34
  # --- Inference Function ---
35
  def classify_emotion(image_np: np.ndarray) -> str:
 
38
  return LABELS[0]
39
 
40
  try:
 
41
  image = Image.fromarray(image_np).convert("RGB")
 
 
42
  inputs = processor(images=image, return_tensors="pt").to(DEVICE)
43
 
 
44
  with torch.no_grad():
45
  outputs = model(**inputs)
46
 
 
47
  probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
48
  confidence, predicted_class_idx = torch.max(probabilities, 1)
49
 
50
  dominant_emotion = LABELS[predicted_class_idx.item()]
51
  confidence_score = confidence.item()
52
 
 
53
  result_str = (
54
  f"<h2 class='text-xl font-bold'>Predicted Emotion:</h2>"
55
  f"<p class='text-3xl mt-2'>**{dominant_emotion.upper()}**</p>"
 
68
  label="Upload an image of a face"
69
  ),
70
  outputs=gr.Markdown(label="Predicted Emotion"),
71
+ title="😊 PyTorch Facial Emotion Detection (ViT Model)",
72
  description=(
73
+ "Uses a stable ViT (Vision Transformer) model fine-tuned on the FER-2013 dataset."
74
  ),
75
  allow_flagging="never",
76
  theme=gr.themes.Soft()