Spaces:
Runtime error
Runtime error
| 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 | |
| import uvicorn | |
| app = FastAPI() | |
| # LOAD MODEL | |
| model = tf.keras.models.load_model( | |
| "plant_model (1).h5", | |
| compile=False | |
| ) | |
| 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 home(): | |
| return {"message": "Plant Disease API Running"} | |
| async def predict(file: UploadFile = File(...)): | |
| image_bytes = await file.read() | |
| image = Image.open(io.BytesIO(image_bytes)).convert("RGB") | |
| 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 JSONResponse({ | |
| "prediction": predicted_class, | |
| "confidence": f"{confidence:.2f}%" | |
| }) | |
| if __name__ == "__main__": | |
| uvicorn.run(app, host="0.0.0.0", port=7860) |