Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- Dockerfile +13 -0
- app.py +46 -0
- labels.txt +2 -0
- model.h5 +3 -0
- requirements.txt +5 -0
Dockerfile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
# Copy requirement dan install dulu (biar cache Docker efisien)
|
| 6 |
+
COPY requirements.txt .
|
| 7 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 8 |
+
|
| 9 |
+
# Copy semua file lain (model, kode Python, dll)
|
| 10 |
+
COPY . .
|
| 11 |
+
|
| 12 |
+
# Jalankan Uvicorn langsung
|
| 13 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8080"]
|
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
from tensorflow.keras.preprocessing.image import img_to_array
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import numpy as np
|
| 7 |
+
import io
|
| 8 |
+
|
| 9 |
+
# Initialize FastAPI app
|
| 10 |
+
app = FastAPI(title="Cat vs Dog Classifier API")
|
| 11 |
+
|
| 12 |
+
# Load the pre-trained model
|
| 13 |
+
model = tf.keras.models.load_model('model.h5')
|
| 14 |
+
|
| 15 |
+
# Define class labels
|
| 16 |
+
class_names = ['Cat', 'Dog']
|
| 17 |
+
|
| 18 |
+
@app.post("/predict/")
|
| 19 |
+
async def predict(file: UploadFile = File(...)):
|
| 20 |
+
# Check if the uploaded file is an image
|
| 21 |
+
if not file.content_type.startswith('image/'):
|
| 22 |
+
raise HTTPException(status_code=400, detail="File must be an image")
|
| 23 |
+
|
| 24 |
+
# Read and preprocess the image
|
| 25 |
+
try:
|
| 26 |
+
contents = await file.read()
|
| 27 |
+
image = Image.open(io.BytesIO(contents)).convert('RGB')
|
| 28 |
+
image = image.resize((224, 224)) # Resize to match model input
|
| 29 |
+
image_array = img_to_array(image) / 255.0 # Rescale to [0, 1]
|
| 30 |
+
image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
|
| 31 |
+
|
| 32 |
+
# Make prediction
|
| 33 |
+
prediction = model.predict(image_array)
|
| 34 |
+
predicted_class = class_names[int(prediction[0][0] > 0.5)] # Sigmoid threshold
|
| 35 |
+
confidence = float(prediction[0][0]) if predicted_class == 'Dog' else float(1 - prediction[0][0])
|
| 36 |
+
|
| 37 |
+
return JSONResponse({
|
| 38 |
+
"predicted_class": predicted_class,
|
| 39 |
+
"confidence": confidence
|
| 40 |
+
})
|
| 41 |
+
except Exception as e:
|
| 42 |
+
raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}")
|
| 43 |
+
|
| 44 |
+
@app.get("/")
|
| 45 |
+
async def root():
|
| 46 |
+
return {"message": "Welcome to the Cat vs Dog Classifier API. Use POST /predict/ to classify an image."}
|
labels.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
cats: 0
|
| 2 |
+
dogs: 1
|
model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:db6ea3352d113127c110573ee48d87b912ade913fa7ba0fa63a765ff9ababcaf
|
| 3 |
+
size 25326488
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
tensorflow
|
| 4 |
+
pillow
|
| 5 |
+
numpy
|