Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
import torch
|
4 |
+
from transformers import pipeline
|
5 |
+
import time
|
6 |
+
|
7 |
+
# Custom CSS for styling
|
8 |
+
st.markdown("""
|
9 |
+
<style>
|
10 |
+
.title {
|
11 |
+
font-size: 40px;
|
12 |
+
color: #4CAF50;
|
13 |
+
font-weight: bold;
|
14 |
+
text-align: center;
|
15 |
+
}
|
16 |
+
.sub-title {
|
17 |
+
font-size: 20px;
|
18 |
+
color: #333;
|
19 |
+
text-align: center;
|
20 |
+
}
|
21 |
+
.upload-button {
|
22 |
+
background-color: #4CAF50;
|
23 |
+
color: white;
|
24 |
+
border-radius: 5px;
|
25 |
+
padding: 10px 20px;
|
26 |
+
font-size: 18px;
|
27 |
+
}
|
28 |
+
.upload-button:hover {
|
29 |
+
background-color: #45a049;
|
30 |
+
}
|
31 |
+
.prediction {
|
32 |
+
font-size: 22px;
|
33 |
+
font-weight: bold;
|
34 |
+
color: #388E3C;
|
35 |
+
text-align: center;
|
36 |
+
}
|
37 |
+
.confidence {
|
38 |
+
font-size: 18px;
|
39 |
+
color: #555;
|
40 |
+
text-align: center;
|
41 |
+
}
|
42 |
+
.image-container {
|
43 |
+
display: flex;
|
44 |
+
justify-content: center;
|
45 |
+
margin-top: 30px;
|
46 |
+
}
|
47 |
+
.footer {
|
48 |
+
text-align: center;
|
49 |
+
margin-top: 50px;
|
50 |
+
font-size: 14px;
|
51 |
+
color: #777;
|
52 |
+
}
|
53 |
+
</style>
|
54 |
+
""", unsafe_allow_html=True)
|
55 |
+
|
56 |
+
# Load model from Hugging Face
|
57 |
+
@st.cache_resource
|
58 |
+
def load_model():
|
59 |
+
model = pipeline('image-classification', model='google/vit-base-patch16-224-in21k')
|
60 |
+
return model
|
61 |
+
|
62 |
+
model = load_model()
|
63 |
+
|
64 |
+
# Streamlit app UI
|
65 |
+
st.markdown('<p class="title">🌱 Plant Identification App 🌱</p>', unsafe_allow_html=True)
|
66 |
+
st.markdown('<p class="sub-title">Upload a plant image and let the app identify its species!</p>', unsafe_allow_html=True)
|
67 |
+
|
68 |
+
# File uploader for plant image
|
69 |
+
uploaded_file = st.file_uploader("Choose a plant image...", type=["jpg", "jpeg", "png"], label_visibility="collapsed")
|
70 |
+
|
71 |
+
if uploaded_file is not None:
|
72 |
+
# Show the uploaded image with a loading effect
|
73 |
+
image = Image.open(uploaded_file)
|
74 |
+
st.image(image, caption="Uploaded Plant Image.", use_column_width=True)
|
75 |
+
|
76 |
+
# Display loading indicator and classify
|
77 |
+
with st.spinner('Classifying plant species...'):
|
78 |
+
time.sleep(2) # Simulate loading time
|
79 |
+
predictions = model(image)
|
80 |
+
|
81 |
+
# Display prediction result
|
82 |
+
st.markdown(f'<p class="prediction">Predicted Species: {predictions[0]["label"]}</p>', unsafe_allow_html=True)
|
83 |
+
st.markdown(f'<p class="confidence">Confidence: {predictions[0]["score"]*100:.2f}%</p>', unsafe_allow_html=True)
|
84 |
+
|
85 |
+
# Footer for extra info or tips
|
86 |
+
st.markdown('<div class="footer">Powered by Streamlit 🌸</div>', unsafe_allow_html=True)
|
87 |
+
|