from fastapi import FastAPI, File, UploadFile from fastapi.responses import JSONResponse import tensorflow as tf import numpy as np from PIL import Image import io app = FastAPI() model = tf.keras.models.load_model( "model_fixed.h5", compile=False ) # Classes CLASS_NAMES = [ "Apple_Apple_Scab", "Apple_Black_Rot", "Apple_Cedar_Rust", "Apple_Healthy", "Blueberry_Healthy", "Cherry_Powdery_Mildew", "Cherry_Healthy", "Corn_Cercospora_Leaf_Spot", "Corn_Common_Rust", "Corn_Northern_Leaf_Blight", "Corn_Healthy", "Grape_Black_Rot", "Grape_Esca", "Grape_Leaf_Blight", "Grape_Healthy", "Peach_Bacterial_Spot", "Peach_Healthy", "Pepper_Bacterial_Spot", "Pepper_Healthy", "Potato_Early_Blight", "Potato_Late_Blight", "Potato_Healthy", "Strawberry_Leaf_Scorch", "Strawberry_Healthy", "Soybean_Healthy", "Squash_Powdery_Mildew", "Raspberry_Healthy", "Tomato_Bacterial_Spot", "Tomato_Early_Blight", "Tomato_Late_Blight", "Tomato_Leaf_Mold", "Tomato_Septoria_Leaf_Spot", "Tomato_Spider_Mites", "Tomato_Target_Spot", "Tomato_Yellow_Leaf_Curl_Virus", "Tomato_Mosaic_Virus", "Tomato_Healthy", "Orange_Citrus_Greening" ] IMG_SIZE = 224 def predict(image): image = image.resize((IMG_SIZE, IMG_SIZE)) image = np.array(image) image = tf.keras.applications.efficientnet.preprocess_input(image) image = np.expand_dims(image, axis=0) prediction = model.predict(image)[0] predicted_class = CLASS_NAMES[np.argmax(prediction)] confidence = float(np.max(prediction)) * 100 return { "prediction": predicted_class, "confidence": f"{confidence:.2f}%" } demo = gr.Interface( fn=predict, inputs=gr.Image(type="pil"), outputs="json", title="Plant Disease Classifier" ) demo.launch()