project21 commited on
Commit
3641fe9
·
verified ·
1 Parent(s): b0d9a2b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -196
app.py CHANGED
@@ -1,234 +1,128 @@
 
1
  import gradio as gr
2
- from tensorflow.keras.utils import img_to_array,load_img
3
  from keras.models import load_model
4
  import numpy as np
 
5
 
6
  # Load the pre-trained model from the local path
7
  model_path = 'chilli.h5'
8
- model = load_model(model_path) # Load the model here
9
-
10
- def predict_disease(image_file, model, all_labels):
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  try:
13
  # Load and preprocess the image
14
- img = load_img(image_file, target_size=(224, 224)) # Use load_img from tensorflow.keras.utils
 
15
  img_array = img_to_array(img)
16
  img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
17
  img_array = img_array / 255.0 # Normalize the image
18
 
19
  # Predict the class
20
- predictions = model.predict(img_array) # Use the loaded model here
21
  predicted_class = np.argmax(predictions[0])
22
-
23
  # Get the predicted class label
24
  predicted_label = all_labels[predicted_class]
25
-
26
- # Print the predicted label to the console
27
-
28
- if predicted_label=='Chilli Healthy':
29
- predicted_label = predicted_label = """<h3 align="center">Chilli Healthy</h3><br><br>
30
- <center>No need use Pesticides</center>"""
31
- elif predicted_label=='Chilli Yellowish':
32
- predicted_label = """
33
- <style>
34
- li{
35
- font-size: 15px;
36
- margin-left: 90px;
37
- margin-top: 15px;
38
- margin-bottom: 15px;
39
- }
40
- h4{
41
- font-size: 17px;
42
- margin-top: 15px;
43
- }
44
- h4:hover{
45
- cursor: pointer;
46
- }
47
-
48
- h3:hover{
49
- cursor: pointer;
50
- color: blue;
51
- transform: scale(1.3);
52
- }
53
- .note{
54
- text-align: center;
55
- font-size: 16px;
56
- }
57
- p{
58
- font-size: 13px;
59
- text-align: center;
60
- }
61
-
62
- </style>
63
- <h3><center><b>Chilli Yellowish</b></center></h3>
64
- <h4>PESTICIDES TO BE USED:</h4>
65
- <ul>
66
  <li>1. Insecticidal Soap</li>
67
  <li>2. Carbaryl (Sevin)</li>
68
  <li>3. Diatomaceous Earth</li>
69
  <li>4. Malathion</li>
70
  <li>5. Spinosad</li>
71
-
72
-
73
- </ul>
74
- <p class="note"><b>* * * IMPORTANT NOTE * * *</b></p>
75
- <p>Be sure to follow local regulations and guidelines for application</p>
76
-
77
-
78
  """
79
- elif predicted_label=='Chilli whitefly':
80
- predicted_label = """
81
- <style>
82
- li{
83
- font-size: 15px;
84
- margin-left: 90px;
85
- margin-top: 15px;
86
- margin-bottom: 15px;
87
- }
88
- h4{
89
- font-size: 17px;
90
- margin-top: 15px;
91
- }
92
- h4:hover{
93
- cursor: pointer;
94
- }
95
-
96
- h3:hover{
97
- cursor: pointer;
98
- color: blue;
99
- transform: scale(1.3);
100
- }
101
- .note{
102
- text-align: center;
103
- font-size: 16px;
104
- }
105
- p{
106
- font-size: 13px;
107
- text-align: center;
108
- }
109
-
110
- </style>
111
- <h3><center><b>Chilli whitefly</b></center></h3>
112
- <h4>PESTICIDES TO BE USED:</h4>
113
- <ul>
114
  <li>1. peppermint</li>
115
  <li>2. Spinosad</li>
116
  <li>3. Chili Pepper Wax</li>
117
  <li>4. Sulfured or Potassium Bicarbonate </li>
118
-
119
-
120
- </ul>
121
- <p class="note"><b>* * * IMPORTANT NOTE * * *</b></p>
122
- <p>Be sure to follow local regulations and guidelines for application</p>
123
-
124
-
125
  """
126
- elif predicted_label=='Chilli leaf Spot':
127
- predicted_label = """
128
- <style>
129
- li{
130
- font-size: 15px;
131
- margin-left: 90px;
132
- margin-top: 15px;
133
- margin-bottom: 15px;
134
- }
135
- h4{
136
- font-size: 17px;
137
- margin-top: 15px;
138
- }
139
- h4:hover{
140
- cursor: pointer;
141
- }
142
-
143
- h3:hover{
144
- cursor: pointer;
145
- color: blue;
146
- transform: scale(1.3);
147
- }
148
- .note{
149
- text-align: center;
150
- font-size: 16px;
151
- }
152
- p{
153
- font-size: 13px;
154
- text-align: center;
155
- }
156
-
157
- </style>
158
- <h3><center><b>Chilli leaf Spot</b></center></h3>
159
- <h4>PESTICIDES TO BE USED:</h4>
160
- <ul>
161
  <li>1. Chlorothalonil </li>
162
  <li>2. Bacillus subtilis </li>
163
  <li>3. Fungicidal Sprays with Sulfur</li>
164
  <li>4. Trifloxystrobin</li>
165
-
166
-
167
- </ul>
168
- <p class="note"><b>* * * IMPORTANT NOTE * * *</b></p>
169
- <p>Be sure to follow local regulations and guidelines for application</p>
170
-
171
-
172
  """
173
- elif predicted_label=='Chilli Leaf Curl':
174
- predicted_label = """
175
- <style>
176
- li{
177
- font-size: 15px;
178
- margin-left: 90px;
179
- margin-top: 15px;
180
- margin-bottom: 15px;
181
- }
182
- h4{
183
- font-size: 17px;
184
- margin-top: 15px;
185
- }
186
- h4:hover{
187
- cursor: pointer;
188
- }
189
-
190
- h3:hover{
191
- cursor: pointer;
192
- color: blue;
193
- transform: scale(1.3);
194
- }
195
- .note{
196
- text-align: center;
197
- font-size: 16px;
198
- }
199
- p{
200
- font-size: 13px;
201
- text-align: center;
202
- }
203
-
204
- </style>
205
- <h3><center><b>Chilli Leaf Curl</b></center></h3>
206
- <h4>PESTICIDES TO BE USED:</h4>
207
- <ul>
208
  <li>1. Horticultural Oil</li>
209
  <li>2. Spinosad</li>
210
  <li>3. Pyrethrin </li>
211
  <li>4. Neem Oil</li>
212
-
213
-
214
- </ul>
215
- <p class="note"><b>* * * IMPORTANT NOTE * * *</b></p>
216
- <p>Be sure to follow local regulations and guidelines for application</p>
217
-
218
-
219
  """
220
-
221
-
 
 
 
222
  else:
223
- predicted_label = """<h3 align="center">Choose Correct image</h3><br><br>
224
- """
225
 
226
- return predicted_label
227
 
 
 
 
 
 
 
 
 
 
 
228
 
229
  except Exception as e:
230
- print(f"Error: {e}")
231
- return None
232
 
233
  # List of class labels
234
  all_labels = [
@@ -238,18 +132,42 @@ all_labels = [
238
  'Chilli Healthy'
239
  ]
240
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
241
  # Define the Gradio interface
242
- def gradio_predict(image_file):
243
- return predict_disease(image_file, model, all_labels) # Pass the model to the function
 
 
244
 
245
- # Create a Gradio interface
246
  gr_interface = gr.Interface(
247
- fn=gradio_predict, # Function to call for predictions
248
- inputs=gr.Image(type="filepath"), # Upload image as file path
249
- outputs="html", # Output will be the class label as text
 
 
 
250
  title="Chilli Disease Predictor",
251
- description="Upload an image of a plant to predict the disease.",
252
  )
253
 
254
  # Launch the Gradio app
255
- gr_interface.launch(share=True)
 
1
+ import h5py
2
  import gradio as gr
3
+ from tensorflow.keras.utils import img_to_array, load_img
4
  from keras.models import load_model
5
  import numpy as np
6
+ from deep_translator import GoogleTranslator
7
 
8
  # Load the pre-trained model from the local path
9
  model_path = 'chilli.h5'
 
 
 
10
 
11
+ # Check if the model is loading correctly
12
+ try:
13
+ with h5py.File(model_path, 'r+') as f:
14
+ if 'groups' in f.attrs['model_config']:
15
+ model_config_string = f.attrs['model_config']
16
+ model_config_string = model_config_string.replace('"groups": 1,', '')
17
+ model_config_string = model_config_string.replace('"groups": 1}', '}')
18
+ f.attrs['model_config'] = model_config_string.encode('utf-8')
19
+
20
+ model = load_model(model_path)
21
+ print("Model loaded successfully.")
22
+ except Exception as e:
23
+ print(f"Error loading model: {e}")
24
+
25
+ def predict_disease(image_file, model, all_labels, target_language):
26
  try:
27
  # Load and preprocess the image
28
+ print(f"Received image file: {image_file}")
29
+ img = load_img(image_file, target_size=(224, 224)) # Ensure image size matches model input
30
  img_array = img_to_array(img)
31
  img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
32
  img_array = img_array / 255.0 # Normalize the image
33
 
34
  # Predict the class
35
+ predictions = model.predict(img_array)
36
  predicted_class = np.argmax(predictions[0])
37
+
38
  # Get the predicted class label
39
  predicted_label = all_labels[predicted_class]
40
+
41
+ # Translate the predicted label to the selected language
42
+ translated_label = GoogleTranslator(source='en', target=target_language).translate(predicted_label)
43
+
44
+ # Provide pesticide information based on the predicted label
45
+ if predicted_label == 'Chilli Yellowish':
46
+ pesticide_info = """
47
+ <h2><center><b>Chilli Yellowish</b></center></h2>
48
+ <h4>PESTICIDES TO BE USED:</h4><br>
49
+
50
+ <ul style="font-size:17px;margin-left:40px;">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  <li>1. Insecticidal Soap</li>
52
  <li>2. Carbaryl (Sevin)</li>
53
  <li>3. Diatomaceous Earth</li>
54
  <li>4. Malathion</li>
55
  <li>5. Spinosad</li>
56
+ </ul><br>
57
+ <center><p class="note" style="font-size:15px;"><b>* * * IMPORTANT NOTE * * *</b></p></center><br>
58
+ <center><p style="font-size:13px;">Be sure to follow local regulations and guidelines for application</p></center>
 
 
 
 
59
  """
60
+ elif predicted_label == 'Chilli whitefly':
61
+ pesticide_info = """
62
+ <h2><center><b>Chilli whitefly</b></center></h2>
63
+ <h4>PESTICIDES TO BE USED:</h4><br>
64
+
65
+ <ul style="font-size:17px;margin-left:40px;">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  <li>1. peppermint</li>
67
  <li>2. Spinosad</li>
68
  <li>3. Chili Pepper Wax</li>
69
  <li>4. Sulfured or Potassium Bicarbonate </li>
70
+ </ul><br>
71
+ <center><p class="note" style="font-size:15px;"><b>* * * IMPORTANT NOTE * * *</b></p></center><br>
72
+ <center><p style="font-size:13px;">Be sure to follow local regulations and guidelines for application</p></center>
 
 
 
 
73
  """
74
+ elif predicted_label == 'Chilli leaf Spot':
75
+ pesticide_info = """
76
+ <h2><center><b>Chilli leaf Spot</b></center></h2>
77
+ <h4>PESTICIDES TO BE USED:</h4><br>
78
+
79
+ <ul style="font-size:17px;margin-left:40px;">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  <li>1. Chlorothalonil </li>
81
  <li>2. Bacillus subtilis </li>
82
  <li>3. Fungicidal Sprays with Sulfur</li>
83
  <li>4. Trifloxystrobin</li>
84
+ </ul><br>
85
+ <center><p class="note" style="font-size:15px;"><b>* * * IMPORTANT NOTE * * *</b></p></center><br>
86
+ <center><p style="font-size:13px;">Be sure to follow local regulations and guidelines for application</p></center>
 
 
 
 
87
  """
88
+ elif predicted_label == 'Chilli Leaf Curl':
89
+ pesticide_info = """
90
+ <h2><center><b>Chilli Leaf Curl</b></center></h2>
91
+ <h4>PESTICIDES TO BE USED:</h4><br>
92
+
93
+ <ul style="font-size:17px;margin-left:40px;">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  <li>1. Horticultural Oil</li>
95
  <li>2. Spinosad</li>
96
  <li>3. Pyrethrin </li>
97
  <li>4. Neem Oil</li>
98
+ </ul><br>
99
+ <center><p class="note" style="font-size:15px;"><b>* * * IMPORTANT NOTE * * *</b></p></center><br>
100
+ <center><p style="font-size:13px;">Be sure to follow local regulations and guidelines for application</p></center>
 
 
 
 
101
  """
102
+
103
+ elif predicted_label == 'Chilli Healthy':
104
+ pesticide_info = """<h2><center><b>Chilli Healthy</b></center></h2>
105
+ <h5> No pesticides needed"""
106
+
107
  else:
108
+ pesticide_info = 'No pesticide information available.'
 
109
 
110
+ print(f"Pesticide Info (Before Translation): {pesticide_info}")
111
 
112
+ # Translate the pesticide information to the selected language
113
+ translated_pesticide_info = GoogleTranslator(source='en', target=target_language).translate(pesticide_info)
114
+ print(f"Translated Pesticide Info: {translated_pesticide_info}")
115
+
116
+ # Return translated label and pesticide information with associated styling
117
+ predicted_label_html = f"""
118
+
119
+ {translated_pesticide_info}
120
+ """
121
+ return predicted_label_html
122
 
123
  except Exception as e:
124
+ print(f"Error during prediction: {e}")
125
+ return f"<h3>Error: {e}</h3>"
126
 
127
  # List of class labels
128
  all_labels = [
 
132
  'Chilli Healthy'
133
  ]
134
 
135
+ # Language codes and their full names (display full names in dropdown)
136
+ language_choices = {
137
+ 'hi': 'Hindi',
138
+ 'te': 'Telugu',
139
+ 'en': 'English',
140
+ 'ml': 'Malayalam',
141
+ 'ta': 'Tamil',
142
+ 'bn': 'Bengali',
143
+ 'gu': 'Gujarati',
144
+ 'kn': 'Kannada',
145
+ 'mr': 'Marathi'
146
+ }
147
+
148
+ # Mapping full names back to their corresponding language code
149
+ full_to_code = {value: key for key, value in language_choices.items()}
150
+
151
+ # Create a dropdown of full language names, using the full name in the UI
152
+ languages = list(language_choices.values()) # List of full language names
153
+
154
  # Define the Gradio interface
155
+ def gradio_predict(image_file, target_language):
156
+ # Map full name back to language code for translation
157
+ language_code = full_to_code.get(target_language, 'en')
158
+ return predict_disease(image_file, model, all_labels, language_code)
159
 
160
+ # Create the Gradio interface
161
  gr_interface = gr.Interface(
162
+ fn=gradio_predict,
163
+ inputs=[
164
+ gr.Image(type="filepath"), # Image input for disease prediction
165
+ gr.Dropdown(label="Select language", choices=languages, value='English') # Language selection dropdown with full names
166
+ ],
167
+ outputs="html", # Output will be in HTML (translated text)
168
  title="Chilli Disease Predictor",
169
+ description="Upload an image of a plant to predict the disease and get the translated label and pesticide information in the selected language."
170
  )
171
 
172
  # Launch the Gradio app
173
+ gr_interface.launch()