Giddycrypt commited on
Commit
fe47fa8
·
verified ·
1 Parent(s): 65de6a4

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +25 -0
  2. app.py +61 -0
  3. requirements.txt +6 -0
Dockerfile ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ WORKDIR /app
4
+
5
+ # Install system dependencies required for OpenCV
6
+ RUN apt-get update && apt-get install -y \
7
+ libgl1-mesa-glx \
8
+ libglib2.0-0 \
9
+ && rm -rf /var/lib/apt/lists/*
10
+
11
+ # Install python dependencies
12
+ COPY requirements.txt .
13
+ RUN pip install --no-cache-dir -r requirements.txt
14
+
15
+ # Pre-download DeepFace weights during the build (so it doesn't download on first user request)
16
+ RUN python -c "from deepface import DeepFace; import logging; logging.getLogger('tf_keras').setLevel(logging.ERROR); DeepFace.build_model('Age'); DeepFace.build_model('Gender'); DeepFace.build_model('VGG-Face')"
17
+
18
+ # Copy app code
19
+ COPY app.py .
20
+
21
+ # Hugging Face Spaces expose port 7860
22
+ EXPOSE 7860
23
+
24
+ # Run the API
25
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile, HTTPException
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from deepface import DeepFace
4
+ import cv2
5
+ import numpy as np
6
+ import logging
7
+
8
+ logging.getLogger('tf_keras').setLevel(logging.ERROR)
9
+
10
+ app = FastAPI(title="DeepFace Age Detection API")
11
+
12
+ # Allow CORS so your local PHP dashboard can talk to this cloud API
13
+ app.add_middleware(
14
+ CORSMiddleware,
15
+ allow_origins=["*"],
16
+ allow_methods=["*"],
17
+ allow_headers=["*"],
18
+ )
19
+
20
+ @app.get("/")
21
+ def read_root():
22
+ return {"status": "DeepFace API is running successfully!"}
23
+
24
+ @app.post("/predict")
25
+ async def predict_age_gender(file: UploadFile = File(...)):
26
+ try:
27
+ # Read the uploaded image from the PHP dashboard
28
+ contents = await file.read()
29
+ nparr = np.frombuffer(contents, np.uint8)
30
+ img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
31
+
32
+ if img is None:
33
+ raise HTTPException(status_code=400, detail="Invalid image file uploaded.")
34
+
35
+ # Run the heavy 1GB DeepFace analysis
36
+ results = DeepFace.analyze(
37
+ img_path=img,
38
+ actions=['age', 'gender'],
39
+ enforce_detection=False, # Prevents crashing if the face is poorly lit
40
+ silent=True
41
+ )
42
+
43
+ # DeepFace can return a list if multiple faces are found, we take the primary one
44
+ if isinstance(results, list):
45
+ result = results[0]
46
+ else:
47
+ result = results
48
+
49
+ gender = result.get("dominant_gender", "Unknown")
50
+ age = result.get("age", 0)
51
+
52
+ # Return the exact regression number!
53
+ return {
54
+ "success": True,
55
+ "age": age,
56
+ "gender": gender,
57
+ "confidence": 0.95 # Standard baseline confidence for VGG-Face regression
58
+ }
59
+
60
+ except Exception as e:
61
+ raise HTTPException(status_code=500, detail=str(e))
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ fastapi==0.104.1
2
+ uvicorn==0.24.0
3
+ python-multipart==0.0.6
4
+ deepface==0.0.79
5
+ tf-keras==2.15.0
6
+ opencv-python-headless==4.8.1.78