vsimmer commited on
Commit
dbde549
·
verified ·
1 Parent(s): 214c1fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -42
app.py CHANGED
@@ -6,7 +6,7 @@ from huggingface_hub import snapshot_download
6
  import os
7
  import sys
8
 
9
- print("🦁 Setting up zoolang model...")
10
 
11
  # Download your model files
12
  model_path = snapshot_download(repo_id="vsimmer/zoolang")
@@ -19,13 +19,17 @@ sys.path.insert(0, model_path)
19
  from model import get_model
20
  from handler import EndpointHandler
21
 
 
 
 
22
  # Initialize your handler
23
  print("Initializing model handler...")
24
  handler = EndpointHandler(path=model_path)
25
  print("✅ Model loaded successfully!")
 
26
 
27
  def classify_image(image):
28
- """Classify an image using your zoolang model"""
29
  if image is None:
30
  return {"No image": 1.0}
31
 
@@ -33,43 +37,4 @@ def classify_image(image):
33
  # Convert PIL image to bytes for your handler
34
  import io
35
  img_byte_arr = io.BytesIO()
36
- image.save(img_byte_arr, format='PNG')
37
- img_byte_arr.seek(0)
38
-
39
- # Use your handler
40
- result = handler({"inputs": img_byte_arr})
41
-
42
- # Your handler returns {"label": prediction} where prediction is a class index
43
- # Convert to probabilities (since we don't have softmax from your model)
44
- # For now, return the predicted class with high confidence
45
- if isinstance(result, dict) and "label" in result:
46
- class_index = result["label"]
47
- # Create mock probabilities - your model's class index as 100% confidence
48
- # Since your model has 5 classes (0-4)
49
- class_names = [f"Class_{i}" for i in range(5)] # Replace with your actual class names
50
-
51
- # Return the predicted class as the top result
52
- return {
53
- class_names[class_index]: 0.95, # High confidence for predicted class
54
- "Other": 0.05
55
- }
56
- else:
57
- return {"Unknown result format": 0.5}
58
-
59
- except Exception as e:
60
- print(f"Classification error: {e}")
61
- return {"Classification failed": 1.0}
62
-
63
- # Create Gradio interface
64
- iface = gr.Interface(
65
- fn=classify_image,
66
- inputs=gr.Image(type="pil", label="Upload Image"),
67
- outputs=gr.Label(num_top_classes=5, label="Zoolang Prediction"),
68
- title="🦁 Zoolang Image Classifier",
69
- description="Using your custom MobileNet V2 model with 5 classes",
70
- allow_flagging="never",
71
- examples=None
72
- )
73
-
74
- if __name__ == "__main__":
75
- iface.launch(server_name="0.0.0.0", server_port=7860)
 
6
  import os
7
  import sys
8
 
9
+ print("🦁 Setting up zoolang model for dog emotion classification...")
10
 
11
  # Download your model files
12
  model_path = snapshot_download(repo_id="vsimmer/zoolang")
 
19
  from model import get_model
20
  from handler import EndpointHandler
21
 
22
+ # Your class names
23
+ CLASS_NAMES = ['aggressive', 'anxious', 'frightened', 'happy', 'inquisitive']
24
+
25
  # Initialize your handler
26
  print("Initializing model handler...")
27
  handler = EndpointHandler(path=model_path)
28
  print("✅ Model loaded successfully!")
29
+ print(f"✅ Classes: {CLASS_NAMES}")
30
 
31
  def classify_image(image):
32
+ """Classify dog emotion using your zoolang model"""
33
  if image is None:
34
  return {"No image": 1.0}
35
 
 
37
  # Convert PIL image to bytes for your handler
38
  import io
39
  img_byte_arr = io.BytesIO()
40
+ image.save(img_byte_arr, format='PNG