Spaces:
Sleeping
Sleeping
Commit ·
4030d18
1
Parent(s): 214cd0e
3 unmodified lines45678891091011121314151617183 unmodified lines222324202125262728293031233233343536371 unmodified line39404142434445463435474838394950514352535455565758596061526263646556663 unmodified linesimport numpy as npimport tensorflow as tfimport gradio as grfrom huggingface_hub import hf_hub_download
# Pathsworking_dir = os.path.dirname(os.path.abspath(__file__))model_path = f"{working_dir}/trained_model/plant_disease_model.tflite"# -----------------------------# Download TFLite model from Hugging Face model repo# -----------------------------model_path = hf_hub_download( repo_id="sidd-harth011/checkingPDRMod", # your model repo filename="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"))# 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))
# Function to preprocess the image# -----------------------------# 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 line img_array = img_array / 255.0 return img_array
# -----------------------------# Prediction function# -----------------------------def predict_image_class(image): preprocessed_img = load_and_preprocess_image(image) # Set input tensor interpreter.set_tensor(input_details[0]['index'], preprocessed_img) interpreter.invoke() # Get predictions 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." description="Upload a plant leaf image to classify its disease using a compressed TFLite model hosted on Hugging Face.")
if __name__ == "__main__": interface.launch() interface.launch()app.py+23-13