Spaces:
Sleeping
Sleeping
Commit ·
5e234f7
1
Parent(s): 0e72817
Adding all files
Browse files- Dockerfile +24 -0
- IAPLD .h5 +3 -0
- app.py +86 -0
- requirements.txt +5 -0
Dockerfile
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use a lightweight Python base image
|
| 2 |
+
FROM python:3.10-slim
|
| 3 |
+
|
| 4 |
+
# Set working directory
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Install system dependencies (if needed for TensorFlow)
|
| 8 |
+
RUN apt-get update && apt-get install -y \
|
| 9 |
+
libgl1-mesa-glx \
|
| 10 |
+
libglib2.0-0 \
|
| 11 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 12 |
+
|
| 13 |
+
# Copy requirements file and install dependencies
|
| 14 |
+
COPY requirements.txt .
|
| 15 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 16 |
+
|
| 17 |
+
# Copy all application files
|
| 18 |
+
COPY . .
|
| 19 |
+
|
| 20 |
+
# Expose the port Hugging Face Spaces expects
|
| 21 |
+
EXPOSE 7860
|
| 22 |
+
|
| 23 |
+
# Run the FastAPI app with Uvicorn
|
| 24 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
IAPLD .h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:93166046ba63f4305eef4e3a3c28604a477c1e101cd8ea346b3661614afe96ae
|
| 3 |
+
size 106687904
|
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 2 |
+
from tensorflow.keras.models import load_model
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
+
|
| 8 |
+
# Initialize FastAPI app
|
| 9 |
+
app = FastAPI(title="Image Classification API")
|
| 10 |
+
|
| 11 |
+
# Add CORS middleware
|
| 12 |
+
app.add_middleware(
|
| 13 |
+
CORSMiddleware,
|
| 14 |
+
allow_origins=["*"],
|
| 15 |
+
allow_credentials=True,
|
| 16 |
+
allow_methods=["*"],
|
| 17 |
+
allow_headers=["*"],
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# Load the Keras model once at startup
|
| 21 |
+
try:
|
| 22 |
+
model = load_model('IAPLD.h5')
|
| 23 |
+
except Exception as e:
|
| 24 |
+
raise RuntimeError(f"Failed to load model 'IAPLD.h5': {str(e)}")
|
| 25 |
+
|
| 26 |
+
# Define class names (adjust if model outputs 3 classes instead of 4)
|
| 27 |
+
CLASS_NAMES = ['Potato___healthy', 'Potato___Early_blight','Potato___Late_blight']
|
| 28 |
+
|
| 29 |
+
# Function to preprocess the uploaded image
|
| 30 |
+
def preprocess_image(image: Image.Image) -> np.ndarray:
|
| 31 |
+
|
| 32 |
+
# Resize to match model input shape (250, 250 as per your code)
|
| 33 |
+
image = image.resize((250, 250)) # Adjust to (256, 256) if model expects that
|
| 34 |
+
|
| 35 |
+
# Convert to NumPy array and normalize to 0-1 range
|
| 36 |
+
image_array = np.array(image) / 255.0
|
| 37 |
+
|
| 38 |
+
# Add batch dimension (1, 250, 250, 3)
|
| 39 |
+
image_array = np.expand_dims(image_array, axis=0)
|
| 40 |
+
|
| 41 |
+
return image_array
|
| 42 |
+
|
| 43 |
+
# Root endpoint
|
| 44 |
+
@app.get("/")
|
| 45 |
+
async def root():
|
| 46 |
+
return {"message": "Welcome to the Image Classification API. Use POST /predict/ to upload an image."}
|
| 47 |
+
|
| 48 |
+
# Prediction endpoint
|
| 49 |
+
@app.post("/predict/")
|
| 50 |
+
async def predict(file: UploadFile = File(...)):
|
| 51 |
+
if not file.content_type.startswith('image/'):
|
| 52 |
+
raise HTTPException(status_code=400, detail="Uploaded file must be an image")
|
| 53 |
+
|
| 54 |
+
try:
|
| 55 |
+
# Read the image bytes
|
| 56 |
+
contents = await file.read()
|
| 57 |
+
|
| 58 |
+
# Open as PIL image
|
| 59 |
+
image = Image.open(io.BytesIO(contents))
|
| 60 |
+
print("Image size:", image.size) # Debug: Check image size
|
| 61 |
+
# Preprocess the image
|
| 62 |
+
image_array = preprocess_image(image)
|
| 63 |
+
print("Image shape:", image_array.shape) # Debug: Check input shape
|
| 64 |
+
|
| 65 |
+
# Make prediction (model outputs probabilities directly)
|
| 66 |
+
predictions = model.predict(image_array)
|
| 67 |
+
print("Probabilities:", predictions) # Debug: Direct probabilities
|
| 68 |
+
|
| 69 |
+
# Get predicted class and confidence
|
| 70 |
+
class_index = np.argmax(predictions[0])
|
| 71 |
+
class_name = CLASS_NAMES[class_index]
|
| 72 |
+
probability = float(predictions[0][class_index])
|
| 73 |
+
|
| 74 |
+
# Return prediction result
|
| 75 |
+
return {
|
| 76 |
+
"predicted_class": class_name,
|
| 77 |
+
"confidence": probability
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
except Exception as e:
|
| 81 |
+
raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}")
|
| 82 |
+
|
| 83 |
+
# Run the app with: uvicorn main:app --reload
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
import uvicorn
|
| 86 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.115.4
|
| 2 |
+
uvicorn==0.32.0
|
| 3 |
+
tensorflow==2.15.0
|
| 4 |
+
numpy==1.26.4
|
| 5 |
+
pillow==10.2.0
|