Spaces:
Sleeping
Sleeping
Commit ·
4030d18
1
Parent(s): 214cd0e
3 unmodified lines456789101112133 unmodified lines171819202122232425261 unmodified line28293031323334353637383940414243444546474849505152535455563 unmodified linesimport numpy as npimport tensorflow as tfimport gradio as gr# Pathsworking_dir = os.path.dirname(os.path.abspath(__file__))model_path = f"{working_dir}/trained_model/plant_disease_model.tflite"# Load TFLite modelinterpreter = tf.lite.Interpreter(model_path=model_path)3 unmodified linesinput_details = interpreter.get_input_details()output_details = interpreter.get_output_details()# Load class indicesclass_indices = json.load(open(f"{working_dir}/class_indices.json"))# Function to preprocess the imagedef load_and_preprocess_image(image, target_size=(224, 224)):img = image.resize(target_size)img_array = np.array(img, dtype=np.float32)1 unmodified lineimg_array = img_array / 255.0return img_array# Prediction functiondef predict_image_class(image):preprocessed_img = load_and_preprocess_image(image)# Set input tensorinterpreter.set_tensor(input_details[0]['index'], preprocessed_img)interpreter.invoke()# Get predictionspredictions = interpreter.get_tensor(output_details[0]['index'])predicted_class_index = np.argmax(predictions, axis=1)[0]predicted_class_name = class_indices[str(predicted_class_index)]return f"Prediction: {predicted_class_name}"# Gradio Interfaceinterface = gr.Interface(fn=predict_image_class,inputs=gr.Image(type="pil", label="Upload an Image"),outputs=gr.Textbox(label="Prediction"),title="🌱 Plant Disease Classifier (TFLite)",description="Upload a plant leaf image to classify its disease using a compressed TFLite model.")if __name__ == "__main__":interface.launch()3 unmodified lines4567891011121314151617183 unmodified lines222324252627282930313233343536371 unmodified line394041424344454647484950515253545556575859606162636465663 unmodified linesimport numpy as npimport tensorflow as tfimport gradio as grfrom huggingface_hub import hf_hub_download# -----------------------------# Download TFLite model from Hugging Face model repo# -----------------------------model_path = hf_hub_download(repo_id="sidd-harth011/checkingPDRMod", # your model repofilename="plant_disease_model.tflite")# Load TFLite modelinterpreter = tf.lite.Interpreter(model_path=model_path)3 unmodified linesinput_details = interpreter.get_input_details()output_details = interpreter.get_output_details()# Download/load class indicesclass_indices_path = hf_hub_download(repo_id="sidd-harth011/checkingPDRMod",filename="class_indices.json")class_indices = json.load(open(class_indices_path))# -----------------------------# Preprocessing function# -----------------------------def load_and_preprocess_image(image, target_size=(224, 224)):img = image.resize(target_size)img_array = np.array(img, dtype=np.float32)1 unmodified lineimg_array = img_array / 255.0return img_array# -----------------------------# Prediction function# -----------------------------def predict_image_class(image):preprocessed_img = load_and_preprocess_image(image)interpreter.set_tensor(input_details[0]['index'], preprocessed_img)interpreter.invoke()predictions = interpreter.get_tensor(output_details[0]['index'])predicted_class_index = np.argmax(predictions, axis=1)[0]predicted_class_name = class_indices[str(predicted_class_index)]return f"Prediction: {predicted_class_name}"# -----------------------------# Gradio Interface# -----------------------------interface = gr.Interface(fn=predict_image_class,inputs=gr.Image(type="pil", label="Upload an Image"),outputs=gr.Textbox(label="Prediction"),title="🌱 Plant Disease Classifier (TFLite)",description="Upload a plant leaf image to classify its disease using a compressed TFLite model hosted on Hugging Face.")if __name__ == "__main__":interface.launch()
app.py+23-13