Spaces:
Sleeping
Sleeping
commit pertama docker api
Browse filesFiles changed (4)
- DockerfileAdded+13
- app.pyAdded+175
- model_cnn_best.h5Added+3
- requirements.txtAdded+6
12345678910111213FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 7860
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175from fastapi import FastAPI, HTTPExceptionfrom fastapi.middleware.cors import CORSMiddlewarefrom pydantic import BaseModel, Fieldimport tensorflow as tfimport numpy as np
# =====================================================# LOAD MODEL# =====================================================
MODEL_PATH = "model_cnn_best.h5"
model = tf.keras.models.load_model(MODEL_PATH)
# =====================================================# FASTAPI# =====================================================
app = FastAPI( title="CNN Pose Classifier API", version="1.0.0", description="API klasifikasi aktivitas menggunakan CNN + MediaPipe Pose")
# =====================================================# CORS# =====================================================
app.add_middleware( CORSMiddleware, allow_origins=["*"], # ganti dengan domain website jika production allow_credentials=True, allow_methods=["*"], allow_headers=["*"],)
# =====================================================# REQUEST MODEL# =====================================================
class PoseInput(BaseModel):
features: list[float] = Field( ..., min_length=66, max_length=66, description="66 pose features (x,y landmark)" )
# =====================================================# ROOT# =====================================================
@app.get("/")def home():
return { "status": "running", "message": "CNN Pose Classifier API", "input_shape": [66], "output": "binary classification" }
# =====================================================# HEALTH CHECK# =====================================================
@app.get("/health")
def health():
return { "status": "healthy" }
# =====================================================# PREDICTION# =====================================================
@app.post("/predict")
def predict(data: PoseInput):
try:
# ----------------------------- # Convert ke numpy # -----------------------------
features = np.array( data.features, dtype=np.float32 )
# ----------------------------- # Validasi jumlah fitur # -----------------------------
if features.shape != (66,):
raise HTTPException( status_code=400, detail="Input harus terdiri dari 66 fitur." )
# ----------------------------- # Validasi NaN dan Inf # -----------------------------
if np.isnan(features).any():
raise HTTPException( status_code=400, detail="Input mengandung NaN." )
if np.isinf(features).any():
raise HTTPException( status_code=400, detail="Input mengandung Infinity." )
# ----------------------------- # Reshape sesuai model # (batch,66,1) # -----------------------------
features = features.reshape( 1, 66, 1 )
# ----------------------------- # Predict # -----------------------------
prediction = model.predict( features, verbose=0 )
probability = float(prediction[0][0])
if probability >= 0.5: label = "berbahaya" else: label = "aman"
return {
"success": True,
"prediction": label,
"probability": round(probability, 4),
"confidence": round( max(probability, 1 - probability), 4 )
}
except HTTPException:
raise
except Exception as e:
raise HTTPException( status_code=500, detail=str(e) )123version https://git-lfs.github.com/spec/v1oid sha256:3cb82eebaf13172dcb1f3c644eee1b670c4ad02ca646243844b1049e928c623bsize 900328123456fastapiuvicorntensorflow==2.17.0numpypydantich5pyDockerfileAdded+13
app.pyAdded+175
model_cnn_best.h5Added+3
requirements.txtAdded+6