DyuHo commited on
Commit
c6f8fb6
·
verified ·
1 Parent(s): 9c5663f

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -45
app.py CHANGED
@@ -43,6 +43,10 @@ class DogCatClassifier:
43
 
44
  def predict(self, image):
45
  try:
 
 
 
 
46
  # Preprocess image
47
  if isinstance(image, str):
48
  image = Image.open(image).convert('RGB')
@@ -61,11 +65,12 @@ class DogCatClassifier:
61
  dog_prob = probabilities[0][1].item()
62
 
63
  return {
64
- "Cat": cat_prob,
65
- "Dog": dog_prob
66
  }
67
  except Exception as e:
68
- return {"Error": 0.5, "Please try again": 0.5}
 
69
 
70
  # Initialize classifier
71
  classifier = DogCatClassifier()
@@ -76,48 +81,20 @@ def classify_image(image):
76
  """
77
  return classifier.predict(image)
78
 
79
- # Create Gradio interface with simpler components
80
- with gr.Blocks(title="🐱🐶 Cat vs Dog Classifier", theme=gr.themes.Soft()) as demo:
81
- gr.Markdown("# 🐱🐶 Cat vs Dog Classifier")
82
- gr.Markdown("Upload an image of a cat or dog, and the AI will classify it!")
83
- gr.Markdown("*This model uses EfficientNet-B1 architecture trained on the Cats vs Dogs dataset.*")
84
-
85
- with gr.Row():
86
- with gr.Column():
87
- image_input = gr.Image(type="pil", label="Upload an image")
88
- classify_btn = gr.Button("🔍 Classify Image", variant="primary")
89
-
90
- with gr.Column():
91
- result_output = gr.Label(num_top_classes=2, label="Prediction Results")
92
-
93
- # Examples section
94
- gr.Markdown("### Try these example images:")
95
- example_images = [
96
- ["test_images/cat1.jpg"],
97
- ["test_images/cat2.jpg"],
98
- ["test_images/dog1.jpg"],
99
- ["test_images/dog2.jpg"]
100
- ]
101
- gr.Examples(
102
- examples=example_images,
103
- inputs=image_input,
104
- outputs=result_output,
105
- fn=classify_image,
106
- cache_examples=True
107
- )
108
-
109
- # Event handlers
110
- classify_btn.click(
111
- fn=classify_image,
112
- inputs=image_input,
113
- outputs=result_output
114
- )
115
 
116
- image_input.change(
117
- fn=classify_image,
118
- inputs=image_input,
119
- outputs=result_output
120
- )
121
 
122
  if __name__ == "__main__":
123
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
43
 
44
  def predict(self, image):
45
  try:
46
+ # Handle None input
47
+ if image is None:
48
+ return {"Please upload an image": 1.0}
49
+
50
  # Preprocess image
51
  if isinstance(image, str):
52
  image = Image.open(image).convert('RGB')
 
65
  dog_prob = probabilities[0][1].item()
66
 
67
  return {
68
+ "Cat": float(cat_prob),
69
+ "Dog": float(dog_prob)
70
  }
71
  except Exception as e:
72
+ print(f"Error during prediction: {e}")
73
+ return {"Error - please try again": 1.0}
74
 
75
  # Initialize classifier
76
  classifier = DogCatClassifier()
 
81
  """
82
  return classifier.predict(image)
83
 
84
+ # Create simple Gradio interface
85
+ demo = gr.Interface(
86
+ fn=classify_image,
87
+ inputs=gr.Image(type="pil", label="Upload an image of a cat or dog"),
88
+ outputs=gr.Label(num_top_classes=2, label="Prediction"),
89
+ title="🐱🐶 Cat vs Dog Classifier",
90
+ description="""
91
+ Upload an image of a cat or dog, and the AI will classify it!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
+ This model uses EfficientNet-B1 architecture trained on the classic Cats vs Dogs dataset.
94
+ Simply upload an image or drag and drop, then the prediction will appear automatically.
95
+ """,
96
+ theme="soft"
97
+ )
98
 
99
  if __name__ == "__main__":
100
+ demo.launch()