Spaces:
Sleeping
Sleeping
Update app.py
#3
by
smaDFJGJSDF
- opened
app.py
CHANGED
|
@@ -1,158 +1 @@
|
|
| 1 |
-
|
| 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 = 'apple.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 == 'Cedar Apple Rust':
|
| 46 |
-
pesticide_info = """
|
| 47 |
-
<h2><center><b>Cedar Apple Rust</b></center></h2>
|
| 48 |
-
<h4>PESTICIDES TO BE USED:</h4><br>
|
| 49 |
-
|
| 50 |
-
<ul style="font-size:17px;margin-left:40px;">
|
| 51 |
-
<li>1. Chlorothalonil (Daconil)</li>
|
| 52 |
-
<li>2. Mancozeb (Dithane)</li>
|
| 53 |
-
<li>3. Propiconazole</li>
|
| 54 |
-
<li>4. Azoxystrobin (Heritage)</li>
|
| 55 |
-
<li>5. Pyraclostrobin (Cabrio)</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 == 'Apple Scrab':
|
| 61 |
-
pesticide_info = """<h2><center><b>Apple Scrab</b></center></h2>
|
| 62 |
-
<h4>PESTICIDES TO BE USED:</h4><br>
|
| 63 |
-
<ul style="font-size:17px;margin-left:40px;">
|
| 64 |
-
<li>1. Chlorothalonil (Daconil)</li>
|
| 65 |
-
<li>2. Mancozeb (Dithane)</li>
|
| 66 |
-
<li>3. Propiconazole</li>
|
| 67 |
-
<li>4. Azoxystrobin (Heritage)</li>
|
| 68 |
-
<li>5. Pyraclostrobin (Cabrio)</li>
|
| 69 |
-
</ul><br>
|
| 70 |
-
<center><p class="note" style="font-size:15px;"><b>* * * IMPORTANT NOTE * * *</b></p></center><br>
|
| 71 |
-
<center><p style="font-size:13px;">Be sure to follow local regulations and guidelines for application</p></center>
|
| 72 |
-
"""
|
| 73 |
-
elif predicted_label == 'Apple Black Rot':
|
| 74 |
-
pesticide_info = """<h2><center><b>Apple Black Rot</b></center></h2>
|
| 75 |
-
<h4>PESTICIDES TO BE USED:</h4><br>
|
| 76 |
-
<ul style="font-size:17px;margin-left:40px;">
|
| 77 |
-
<li>1. Chlorothalonil (Daconil)</li>
|
| 78 |
-
<li>2. Mancozeb (Dithane)</li>
|
| 79 |
-
<li>3. Propiconazole</li>
|
| 80 |
-
<li>4. Azoxystrobin (Heritage)</li>
|
| 81 |
-
<li>5. Pyraclostrobin (Cabrio)</li>
|
| 82 |
-
</ul><br>
|
| 83 |
-
<center><p class="note" style="font-size:15px;"><b>* * * IMPORTANT NOTE * * *</b></p></center><br>
|
| 84 |
-
<center><p style="font-size:13px;">Be sure to follow local regulations and guidelines for application</p></center>
|
| 85 |
-
"""
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
elif predicted_label == 'Apple Healthy':
|
| 89 |
-
pesticide_info = """<h2><center><b>Apple Healthy</b></center></h2>
|
| 90 |
-
<h5> No pesticides needed"""
|
| 91 |
-
|
| 92 |
-
else:
|
| 93 |
-
pesticide_info = 'No pesticide information available.'
|
| 94 |
-
|
| 95 |
-
print(f"Pesticide Info (Before Translation): {pesticide_info}")
|
| 96 |
-
|
| 97 |
-
# Translate the pesticide information to the selected language
|
| 98 |
-
translated_pesticide_info = GoogleTranslator(source='en', target=target_language).translate(pesticide_info)
|
| 99 |
-
print(f"Translated Pesticide Info: {translated_pesticide_info}")
|
| 100 |
-
|
| 101 |
-
# Return translated label and pesticide information with associated styling
|
| 102 |
-
predicted_label_html = f"""
|
| 103 |
-
|
| 104 |
-
{translated_pesticide_info}
|
| 105 |
-
"""
|
| 106 |
-
return predicted_label_html
|
| 107 |
-
|
| 108 |
-
except Exception as e:
|
| 109 |
-
print(f"Error during prediction: {e}")
|
| 110 |
-
return f"<h3>Error: {e}</h3>"
|
| 111 |
-
|
| 112 |
-
# List of class labels
|
| 113 |
-
all_labels = [
|
| 114 |
-
'Cedar Apple Rust',
|
| 115 |
-
'Apple Scrab',
|
| 116 |
-
'Apple Healthy',
|
| 117 |
-
'Apple Black Rot'
|
| 118 |
-
]
|
| 119 |
-
|
| 120 |
-
# Language codes and their full names (display full names in dropdown)
|
| 121 |
-
language_choices = {
|
| 122 |
-
'hi': 'Hindi',
|
| 123 |
-
'te': 'Telugu',
|
| 124 |
-
'en': 'English',
|
| 125 |
-
'ml': 'Malayalam',
|
| 126 |
-
'ta': 'Tamil',
|
| 127 |
-
'bn': 'Bengali',
|
| 128 |
-
'gu': 'Gujarati',
|
| 129 |
-
'kn': 'Kannada',
|
| 130 |
-
'mr': 'Marathi'
|
| 131 |
-
}
|
| 132 |
-
|
| 133 |
-
# Mapping full names back to their corresponding language code
|
| 134 |
-
full_to_code = {value: key for key, value in language_choices.items()}
|
| 135 |
-
|
| 136 |
-
# Create a dropdown of full language names, using the full name in the UI
|
| 137 |
-
languages = list(language_choices.values()) # List of full language names
|
| 138 |
-
|
| 139 |
-
# Define the Gradio interface
|
| 140 |
-
def gradio_predict(image_file, target_language):
|
| 141 |
-
# Map full name back to language code for translation
|
| 142 |
-
language_code = full_to_code.get(target_language, 'en')
|
| 143 |
-
return predict_disease(image_file, model, all_labels, language_code)
|
| 144 |
-
|
| 145 |
-
# Create the Gradio interface
|
| 146 |
-
gr_interface = gr.Interface(
|
| 147 |
-
fn=gradio_predict,
|
| 148 |
-
inputs=[
|
| 149 |
-
gr.Image(type="filepath"), # Image input for disease prediction
|
| 150 |
-
gr.Dropdown(label="Select language", choices=languages, value='English') # Language selection dropdown with full names
|
| 151 |
-
],
|
| 152 |
-
outputs="html", # Output will be in HTML (translated text)
|
| 153 |
-
title="Apple Disease Predictor",
|
| 154 |
-
description="Upload an image of a plant to predict the disease and get the translated label and pesticide information in the selected language."
|
| 155 |
-
)
|
| 156 |
-
|
| 157 |
-
# Launch the Gradio app
|
| 158 |
-
gr_interface.launch()
|
|
|
|
| 1 |
+
print("hello")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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