Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +11 -0
- main.py +45 -0
- requirements.txt +10 -0
Dockerfile
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
RUN useradd -m -u 1000 user
|
| 3 |
+
USER user
|
| 4 |
+
ENV HOME=/home/user \
|
| 5 |
+
PATH=/home/user/.local/bin:$PATH
|
| 6 |
+
WORKDIR $HOME/app
|
| 7 |
+
COPY --chown=user . $HOME/app
|
| 8 |
+
RUN pip install -r requirements.txt
|
| 9 |
+
|
| 10 |
+
EXPOSE 7860
|
| 11 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI,UploadFile,File,HTTPException
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from facenet_pytorch import MTCNN
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
app=FastAPI(title="Deepfake")
|
| 8 |
+
|
| 9 |
+
print("Face Detector Loading...")
|
| 10 |
+
mtcnn=MTCNN(keep_all=False,device='cpu')
|
| 11 |
+
|
| 12 |
+
print("Deepfake AI Loading...")
|
| 13 |
+
pipe=pipeline("image-classification",model="dima806/deepfake_vs_real_image_detection")
|
| 14 |
+
print("All Systems Loaded!")
|
| 15 |
+
|
| 16 |
+
@app.post("/api/v1/predict/image")
|
| 17 |
+
async def predict_image(file:UploadFile=File(...)):
|
| 18 |
+
if not file.content_type.startswith("image/"):
|
| 19 |
+
raise HTTPException(status_code=400,detail="File must be an image.")
|
| 20 |
+
|
| 21 |
+
try:
|
| 22 |
+
image_bytes=await file.read()
|
| 23 |
+
image=Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 24 |
+
# check for human faces
|
| 25 |
+
boxes, _ =mtcnn.detect(image)
|
| 26 |
+
|
| 27 |
+
if boxes is None:
|
| 28 |
+
return {
|
| 29 |
+
"status":"failed",
|
| 30 |
+
"message":"No human face detected in the image. Please upload a clear human portrait."
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
results=pipe(image)
|
| 34 |
+
formatted_results={res['label'].lower(): round(res['score'] * 100, 2) for res in results}
|
| 35 |
+
verdict=max(formatted_results,key=formatted_results.get)
|
| 36 |
+
|
| 37 |
+
return {
|
| 38 |
+
"status":"success 200",
|
| 39 |
+
"faces_detected":len(boxes),
|
| 40 |
+
"verdict":verdict,
|
| 41 |
+
"confidence":formatted_results[verdict]
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
except Exception as e:
|
| 45 |
+
raise HTTPException(status_code=500, detail=str(e))
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
--extra-index-url https://download.pytorch.org/whl/cpu
|
| 2 |
+
torch==2.2.2
|
| 3 |
+
torchvision==0.17.2
|
| 4 |
+
numpy<2.0.0
|
| 5 |
+
transformers<4.44.0
|
| 6 |
+
facenet-pytorch
|
| 7 |
+
pillow
|
| 8 |
+
fastapi
|
| 9 |
+
uvicorn
|
| 10 |
+
python-multipart
|