Update app.py
Browse files
app.py
CHANGED
@@ -29,14 +29,14 @@ css = """
|
|
29 |
}
|
30 |
"""
|
31 |
|
32 |
-
def predict(
|
33 |
"""Process single image and return prediction"""
|
34 |
-
if
|
35 |
-
return "Please select or upload an image"
|
36 |
|
37 |
try:
|
38 |
-
image = Image.open(
|
39 |
-
tensor = preprocess(image).unsqueeze(0)
|
40 |
|
41 |
with torch.inference_mode():
|
42 |
outputs = model(tensor)
|
@@ -49,38 +49,51 @@ def predict(img):
|
|
49 |
|
50 |
with gr.Blocks(title="Animal Classifier", css=css) as demo:
|
51 |
gr.Markdown("## 🐾 Animal Classifier")
|
52 |
-
gr.Markdown("
|
|
|
|
|
|
|
53 |
|
54 |
with gr.Row():
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
57 |
|
58 |
-
# Example gallery
|
59 |
with gr.Row(variant="panel"):
|
60 |
examples_gallery = gr.Gallery(
|
61 |
value=example_images,
|
62 |
-
label="Example Images (Click to
|
63 |
columns=7,
|
64 |
height=120,
|
65 |
allow_preview=False,
|
66 |
elem_classes=["centered-examples"]
|
67 |
)
|
68 |
-
|
69 |
-
# Handle
|
70 |
def select_example(evt: gr.SelectData):
|
71 |
return example_images[evt.index]
|
72 |
|
73 |
examples_gallery.select(
|
74 |
fn=select_example,
|
75 |
-
outputs=
|
76 |
show_progress=False
|
77 |
)
|
78 |
-
|
79 |
-
# Handle
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
fn=predict,
|
82 |
-
inputs=
|
83 |
-
outputs=
|
84 |
)
|
85 |
|
86 |
if __name__ == "__main__":
|
|
|
29 |
}
|
30 |
"""
|
31 |
|
32 |
+
def predict(img_path):
|
33 |
"""Process single image and return prediction"""
|
34 |
+
if not img_path:
|
35 |
+
return "Please select or upload an image first"
|
36 |
|
37 |
try:
|
38 |
+
image = Image.open(img_path).convert('RGB')
|
39 |
+
tensor = preprocess(image).unsqueeze(0)
|
40 |
|
41 |
with torch.inference_mode():
|
42 |
outputs = model(tensor)
|
|
|
49 |
|
50 |
with gr.Blocks(title="Animal Classifier", css=css) as demo:
|
51 |
gr.Markdown("## 🐾 Animal Classifier")
|
52 |
+
gr.Markdown("Select an image below or upload your own, then click Classify")
|
53 |
+
|
54 |
+
# Store current image path
|
55 |
+
current_image = gr.State()
|
56 |
|
57 |
with gr.Row():
|
58 |
+
with gr.Column():
|
59 |
+
image_preview = gr.Image(label="Selected Image", type="filepath")
|
60 |
+
upload_btn = gr.UploadButton("Upload Custom Image", file_types=["image"])
|
61 |
+
classify_btn = gr.Button("Classify 🚀", variant="primary")
|
62 |
+
result = gr.Textbox(label="Prediction", lines=3)
|
63 |
|
64 |
+
# Example gallery at bottom
|
65 |
with gr.Row(variant="panel"):
|
66 |
examples_gallery = gr.Gallery(
|
67 |
value=example_images,
|
68 |
+
label="Example Images (Click to Select)",
|
69 |
columns=7,
|
70 |
height=120,
|
71 |
allow_preview=False,
|
72 |
elem_classes=["centered-examples"]
|
73 |
)
|
74 |
+
|
75 |
+
# Handle image selection from examples
|
76 |
def select_example(evt: gr.SelectData):
|
77 |
return example_images[evt.index]
|
78 |
|
79 |
examples_gallery.select(
|
80 |
fn=select_example,
|
81 |
+
outputs=[image_preview, current_image],
|
82 |
show_progress=False
|
83 |
)
|
84 |
+
|
85 |
+
# Handle custom uploads
|
86 |
+
upload_btn.upload(
|
87 |
+
fn=lambda file: (file.name, file.name),
|
88 |
+
inputs=upload_btn,
|
89 |
+
outputs=[image_preview, current_image]
|
90 |
+
)
|
91 |
+
|
92 |
+
# Handle classification
|
93 |
+
classify_btn.click(
|
94 |
fn=predict,
|
95 |
+
inputs=current_image,
|
96 |
+
outputs=result
|
97 |
)
|
98 |
|
99 |
if __name__ == "__main__":
|