Spaces:
Sleeping
Sleeping
Upload 9 files
Browse files- .gitattributes +7 -0
- animal_image_app.py +114 -0
- cat.png +3 -0
- frog.png +3 -0
- hippo.png +3 -0
- jaguar.png +3 -0
- requirements.txt +4 -0
- sloth.png +3 -0
- toucan.png +3 -0
- turtle.png +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,10 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
cat.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
frog.png filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
hippo.png filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
jaguar.png filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
sloth.png filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
toucan.png filter=lfs diff=lfs merge=lfs -text
|
| 42 |
+
turtle.png filter=lfs diff=lfs merge=lfs -text
|
animal_image_app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
# Load image classification model
|
| 6 |
+
# Using a pre-trained model that can classify various animals and objects
|
| 7 |
+
classifier = pipeline("image-classification", model="google/vit-base-patch16-224")
|
| 8 |
+
|
| 9 |
+
def classify_image(image):
|
| 10 |
+
"""
|
| 11 |
+
Classify an uploaded animal image and return top predictions with progress bars
|
| 12 |
+
"""
|
| 13 |
+
if image is None:
|
| 14 |
+
return "<div style='font-family: Arial, sans-serif; padding: 20px; text-align: center; color: #666;'>Please upload an image.</div>"
|
| 15 |
+
|
| 16 |
+
# Classify the image
|
| 17 |
+
results = classifier(image)
|
| 18 |
+
|
| 19 |
+
# Format results with HTML progress bars - show top 5 predictions
|
| 20 |
+
html_results = "<div style='font-family: Arial, sans-serif;'>"
|
| 21 |
+
html_results += "<h3 style='margin-top: 0;'>Top Predictions:</h3>"
|
| 22 |
+
|
| 23 |
+
for i, result in enumerate(results[:5], 1):
|
| 24 |
+
label = result['label']
|
| 25 |
+
score = result['score'] * 100
|
| 26 |
+
score_int = int(score)
|
| 27 |
+
|
| 28 |
+
# Create progress bar with color gradient (green for high, yellow for medium, red for low)
|
| 29 |
+
if score_int >= 70:
|
| 30 |
+
bar_color = "#4CAF50" # Green
|
| 31 |
+
elif score_int >= 40:
|
| 32 |
+
bar_color = "#FF9800" # Orange
|
| 33 |
+
else:
|
| 34 |
+
bar_color = "#F44336" # Red
|
| 35 |
+
|
| 36 |
+
html_results += f"""
|
| 37 |
+
<div style='margin-bottom: 15px;'>
|
| 38 |
+
<div style='display: flex; justify-content: space-between; margin-bottom: 5px;'>
|
| 39 |
+
<span style='font-weight: bold;'>{i}. {label}</span>
|
| 40 |
+
<span style='font-weight: bold; color: #333;'>{score:.2f}%</span>
|
| 41 |
+
</div>
|
| 42 |
+
<div style='background-color: #e0e0e0; border-radius: 10px; height: 25px; overflow: hidden;'>
|
| 43 |
+
<div style='background-color: {bar_color}; height: 100%; width: {score:.2f}%; transition: width 0.3s ease; border-radius: 10px;'></div>
|
| 44 |
+
</div>
|
| 45 |
+
</div>
|
| 46 |
+
"""
|
| 47 |
+
|
| 48 |
+
html_results += "</div>"
|
| 49 |
+
return html_results
|
| 50 |
+
|
| 51 |
+
# Create the Gradio interface
|
| 52 |
+
with gr.Blocks(title="Animal Image Classifier") as demo:
|
| 53 |
+
gr.Markdown("# Animal Image Classifier")
|
| 54 |
+
gr.Markdown("Upload an animal photo to classify it using AI!")
|
| 55 |
+
|
| 56 |
+
with gr.Row():
|
| 57 |
+
with gr.Column():
|
| 58 |
+
# Image input
|
| 59 |
+
input_image = gr.Image(
|
| 60 |
+
type="pil",
|
| 61 |
+
label="Upload Animal Photo"
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# Classify button
|
| 65 |
+
classify_btn = gr.Button("Classify Image", variant="primary", size="lg")
|
| 66 |
+
clear_btn = gr.Button("Clear", variant="secondary")
|
| 67 |
+
|
| 68 |
+
with gr.Column():
|
| 69 |
+
# Output for classification results with HTML progress bars
|
| 70 |
+
output_html = gr.HTML(
|
| 71 |
+
label="Classification Results"
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
# Example images at the bottom
|
| 75 |
+
gr.Markdown("### Example Images")
|
| 76 |
+
gr.Markdown("Try these example images:")
|
| 77 |
+
|
| 78 |
+
example_images = [
|
| 79 |
+
"cat.png",
|
| 80 |
+
"frog.png",
|
| 81 |
+
"hippo.png",
|
| 82 |
+
"jaguar.png",
|
| 83 |
+
"sloth.png",
|
| 84 |
+
"toucan.png",
|
| 85 |
+
"turtle.png"
|
| 86 |
+
]
|
| 87 |
+
|
| 88 |
+
# Create example gallery - images are in the same directory as this script
|
| 89 |
+
import os
|
| 90 |
+
script_dir = os.path.dirname(os.path.abspath(__file__))
|
| 91 |
+
example_paths = [[os.path.join(script_dir, img)] for img in example_images]
|
| 92 |
+
|
| 93 |
+
gr.Examples(
|
| 94 |
+
examples=example_paths,
|
| 95 |
+
inputs=input_image,
|
| 96 |
+
label="Click on an example image to load it"
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
# Define button actions
|
| 100 |
+
classify_btn.click(
|
| 101 |
+
fn=classify_image,
|
| 102 |
+
inputs=input_image,
|
| 103 |
+
outputs=output_html
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
clear_btn.click(
|
| 107 |
+
fn=lambda: (None, "<div></div>"),
|
| 108 |
+
inputs=None,
|
| 109 |
+
outputs=[input_image, output_html]
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
# Launch the app
|
| 113 |
+
if __name__ == "__main__":
|
| 114 |
+
demo.launch()
|
cat.png
ADDED
|
Git LFS Details
|
frog.png
ADDED
|
Git LFS Details
|
hippo.png
ADDED
|
Git LFS Details
|
jaguar.png
ADDED
|
Git LFS Details
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
sentencepiece
|
sloth.png
ADDED
|
Git LFS Details
|
toucan.png
ADDED
|
Git LFS Details
|
turtle.png
ADDED
|
Git LFS Details
|