rahideer commited on
Commit
bce5713
Β·
verified Β·
1 Parent(s): de1fcc7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -66
app.py CHANGED
@@ -1,87 +1,38 @@
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
 
 
 
 
 
1
  import streamlit as st
2
  from PIL import Image
3
  import torch
4
+ from transformers import pipeline, AutoFeatureExtractor
 
5
 
6
+ # Load the model and feature extractor (image processor)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  @st.cache_resource
8
  def load_model():
9
  model = pipeline('image-classification', model='google/vit-base-patch16-224-in21k')
10
  return model
11
 
12
+ # Initialize the feature extractor
13
+ extractor = AutoFeatureExtractor.from_pretrained('google/vit-base-patch16-224-in21k')
14
+
15
  model = load_model()
16
 
17
  # Streamlit app UI
18
+ st.title("🌱 Plant Identification App 🌱")
19
+ st.write("Upload a plant image and let the app identify its species!")
20
 
21
  # File uploader for plant image
22
+ uploaded_file = st.file_uploader("Choose a plant image...", type=["jpg", "jpeg", "png"])
23
 
24
  if uploaded_file is not None:
25
+ # Open and display the uploaded image
26
  image = Image.open(uploaded_file)
27
  st.image(image, caption="Uploaded Plant Image.", use_column_width=True)
28
 
29
+ # Preprocess the image with the feature extractor
30
+ inputs = extractor(images=image, return_tensors="pt", padding=True)
 
 
31
 
32
+ # Run the classification
33
+ with st.spinner('Classifying plant species...'):
34
+ predictions = model(inputs['pixel_values'])
 
 
 
35
 
36
+ # Display prediction results
37
+ st.write(f"Predicted Species: {predictions[0]['label']}")
38
+ st.write(f"Confidence: {predictions[0]['score']*100:.2f}%")