Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
# Load an animal classification model
|
| 6 |
+
animal_pipeline = pipeline(task="image-classification", model="microsoft/resnet-50")
|
| 7 |
+
|
| 8 |
+
st.title("Animal Species Classifier 🐾")
|
| 9 |
+
|
| 10 |
+
# Upload the animal image
|
| 11 |
+
file_name = st.file_uploader("Upload an animal image")
|
| 12 |
+
|
| 13 |
+
if file_name is not None:
|
| 14 |
+
col1, col2 = st.columns(2)
|
| 15 |
+
|
| 16 |
+
# Display the uploaded image
|
| 17 |
+
image = Image.open(file_name)
|
| 18 |
+
col1.image(image, use_container_width=True, caption="Uploaded Image")
|
| 19 |
+
|
| 20 |
+
# Make predictions
|
| 21 |
+
predictions = animal_pipeline(image)
|
| 22 |
+
|
| 23 |
+
# Display predictions and habitat information
|
| 24 |
+
col2.header("Animal Predictions 🐾")
|
| 25 |
+
habitat_info = {
|
| 26 |
+
"tiger": "Forests, grasslands, and mangroves in Asia.",
|
| 27 |
+
"elephant": "Grasslands and forests in Africa and Asia.",
|
| 28 |
+
"penguin": "Antarctic and coastal areas in the Southern Hemisphere.",
|
| 29 |
+
"kangaroo": "Open plains, woodlands, and savannas in Australia.",
|
| 30 |
+
"panda": "Bamboo forests in China.",
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
for p in predictions[:3]:
|
| 34 |
+
species = p['label'].lower()
|
| 35 |
+
confidence = round(p['score'] * 100, 1)
|
| 36 |
+
col2.subheader(f"**{species.capitalize()}**: {confidence}%")
|
| 37 |
+
|
| 38 |
+
# Show habitat info if available
|
| 39 |
+
habitat = habitat_info.get(species, "Habitat information not available.")
|
| 40 |
+
col2.write(f"**Habitat:** {habitat}")
|