Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- Dockerfile +18 -0
- app.py +44 -0
- orange_disease_model.h5 +3 -0
- requirements.txt +6 -0
Dockerfile
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use Python 3.9
|
| 2 |
+
FROM python:3.9
|
| 3 |
+
|
| 4 |
+
# Set working directory
|
| 5 |
+
WORKDIR /code
|
| 6 |
+
|
| 7 |
+
# Copy requirements and install them
|
| 8 |
+
COPY ./requirements.txt /code/requirements.txt
|
| 9 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 10 |
+
|
| 11 |
+
# Copy all files
|
| 12 |
+
COPY . .
|
| 13 |
+
|
| 14 |
+
# Grant permissions
|
| 15 |
+
RUN chmod 777 .
|
| 16 |
+
|
| 17 |
+
# Start the server
|
| 18 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
# Load the model once when server starts
|
| 10 |
+
model = tf.keras.models.load_model("orange_disease_model.h5")
|
| 11 |
+
|
| 12 |
+
# Define your classes (Make sure these match your labels.txt!)
|
| 13 |
+
CLASS_NAMES = [
|
| 14 |
+
'Citrus canker', 'Citrus greening', 'Citrus mealybugs', 'Die back',
|
| 15 |
+
'Foliage damaged', 'Healthy leaf', 'Powdery mildew', 'Shot hole',
|
| 16 |
+
'Spiny whitefly', 'Yellow dragon', 'Yellow leaves'
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
@app.get("/")
|
| 20 |
+
def home():
|
| 21 |
+
return {"message": "Orange Disease Detection API is Running!"}
|
| 22 |
+
|
| 23 |
+
@app.post("/predict")
|
| 24 |
+
async def predict(file: UploadFile = File(...)):
|
| 25 |
+
# 1. Read the image uploaded by the user
|
| 26 |
+
image_data = await file.read()
|
| 27 |
+
image = Image.open(io.BytesIO(image_data))
|
| 28 |
+
|
| 29 |
+
# 2. Preprocess (Resize to 224x224 and Normalize)
|
| 30 |
+
image = image.resize((224, 224))
|
| 31 |
+
img_array = tf.keras.preprocessing.image.img_to_array(image)
|
| 32 |
+
img_array = tf.expand_dims(img_array, 0) # Create a batch
|
| 33 |
+
img_array = img_array / 255.0
|
| 34 |
+
|
| 35 |
+
# 3. Predict
|
| 36 |
+
predictions = model.predict(img_array)
|
| 37 |
+
predicted_class = CLASS_NAMES[np.argmax(predictions[0])]
|
| 38 |
+
confidence = float(np.max(predictions[0]))
|
| 39 |
+
|
| 40 |
+
# 4. Return JSON
|
| 41 |
+
return {
|
| 42 |
+
"class": predicted_class,
|
| 43 |
+
"confidence": confidence
|
| 44 |
+
}
|
orange_disease_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5433c9686e86c839486833e73c98fbf57c4ebb36c9c79a492dd2ddec33b4fc7c
|
| 3 |
+
size 11557472
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
tensorflow-cpu
|
| 4 |
+
pillow
|
| 5 |
+
python-multipart
|
| 6 |
+
numpy
|