Spaces:
Running
on
T4
Running
on
T4
Commit
·
c0b300b
1
Parent(s):
ec15dbc
added app.py
Browse files
app.py
CHANGED
|
@@ -71,8 +71,8 @@ inswapper_path = download_models()
|
|
| 71 |
# -------------------------------------------------
|
| 72 |
providers = ['CUDAExecutionProvider', 'CPUExecutionProvider']
|
| 73 |
logger.info(f"Initializing FaceAnalysis with providers: {providers}")
|
| 74 |
-
|
| 75 |
-
|
| 76 |
swapper = insightface.model_zoo.get_model(inswapper_path, providers=providers)
|
| 77 |
logger.info("FaceAnalysis and swapper initialized")
|
| 78 |
|
|
@@ -132,8 +132,8 @@ def face_swap_and_enhance(src_img, tgt_img):
|
|
| 132 |
tgt_bgr = cv2.cvtColor(tgt_img, cv2.COLOR_RGB2BGR)
|
| 133 |
|
| 134 |
logger.info("Detecting faces...")
|
| 135 |
-
src_faces =
|
| 136 |
-
tgt_faces =
|
| 137 |
if not src_faces or not tgt_faces:
|
| 138 |
logger.error("Face not detected in one of the images")
|
| 139 |
return None, None, "❌ Face not detected in one of the images"
|
|
@@ -201,17 +201,17 @@ with gr.Blocks() as demo:
|
|
| 201 |
# -------------------------------------------------
|
| 202 |
# FastAPI App
|
| 203 |
# -------------------------------------------------
|
| 204 |
-
|
| 205 |
|
| 206 |
-
@
|
| 207 |
def root():
|
| 208 |
return RedirectResponse("/gradio")
|
| 209 |
|
| 210 |
-
@
|
| 211 |
async def health():
|
| 212 |
return {"status": "healthy"}
|
| 213 |
|
| 214 |
-
@
|
| 215 |
async def upload_source(image: UploadFile = File(...)):
|
| 216 |
logger.info(f"Uploading source image: {image.filename}")
|
| 217 |
contents = await image.read()
|
|
@@ -224,7 +224,7 @@ async def upload_source(image: UploadFile = File(...)):
|
|
| 224 |
logger.info(f"Source image uploaded with ID: {str(result.inserted_id)}")
|
| 225 |
return {"source_id": str(result.inserted_id)}
|
| 226 |
|
| 227 |
-
@
|
| 228 |
async def upload_target(image: UploadFile = File(...)):
|
| 229 |
logger.info(f"Uploading target image: {image.filename}")
|
| 230 |
contents = await image.read()
|
|
@@ -241,7 +241,7 @@ class FaceSwapRequest(BaseModel):
|
|
| 241 |
source_id: str
|
| 242 |
target_id: str
|
| 243 |
|
| 244 |
-
@
|
| 245 |
async def perform_faceswap(request: FaceSwapRequest):
|
| 246 |
logger.info(f"Starting face swap for source_id: {request.source_id}, target_id: {request.target_id}")
|
| 247 |
source_doc = await source_images_collection.find_one({"_id": ObjectId(request.source_id)})
|
|
@@ -287,7 +287,7 @@ async def perform_faceswap(request: FaceSwapRequest):
|
|
| 287 |
logger.info(f"Face swap result stored with ID: {str(result.inserted_id)}")
|
| 288 |
return {"result_id": str(result.inserted_id)}
|
| 289 |
|
| 290 |
-
@
|
| 291 |
async def download_result(result_id: str):
|
| 292 |
logger.info(f"Downloading result: {result_id}")
|
| 293 |
doc = await results_collection.find_one({"_id": ObjectId(result_id)})
|
|
@@ -301,8 +301,8 @@ async def download_result(result_id: str):
|
|
| 301 |
)
|
| 302 |
|
| 303 |
# Mount Gradio at /gradio
|
| 304 |
-
|
| 305 |
|
| 306 |
# Run the app with Uvicorn
|
| 307 |
if __name__ == "__main__":
|
| 308 |
-
uvicorn.run(
|
|
|
|
| 71 |
# -------------------------------------------------
|
| 72 |
providers = ['CUDAExecutionProvider', 'CPUExecutionProvider']
|
| 73 |
logger.info(f"Initializing FaceAnalysis with providers: {providers}")
|
| 74 |
+
face_analysis_app = FaceAnalysis(name="buffalo_l", root=MODELS_DIR, providers=providers)
|
| 75 |
+
face_analysis_app.prepare(ctx_id=0, det_size=(640, 640))
|
| 76 |
swapper = insightface.model_zoo.get_model(inswapper_path, providers=providers)
|
| 77 |
logger.info("FaceAnalysis and swapper initialized")
|
| 78 |
|
|
|
|
| 132 |
tgt_bgr = cv2.cvtColor(tgt_img, cv2.COLOR_RGB2BGR)
|
| 133 |
|
| 134 |
logger.info("Detecting faces...")
|
| 135 |
+
src_faces = face_analysis_app.get(src_bgr)
|
| 136 |
+
tgt_faces = face_analysis_app.get(tgt_bgr)
|
| 137 |
if not src_faces or not tgt_faces:
|
| 138 |
logger.error("Face not detected in one of the images")
|
| 139 |
return None, None, "❌ Face not detected in one of the images"
|
|
|
|
| 201 |
# -------------------------------------------------
|
| 202 |
# FastAPI App
|
| 203 |
# -------------------------------------------------
|
| 204 |
+
fastapi_app = FastAPI()
|
| 205 |
|
| 206 |
+
@fastapi_app.get("/")
|
| 207 |
def root():
|
| 208 |
return RedirectResponse("/gradio")
|
| 209 |
|
| 210 |
+
@fastapi_app.get("/health")
|
| 211 |
async def health():
|
| 212 |
return {"status": "healthy"}
|
| 213 |
|
| 214 |
+
@fastapi_app.post("/source")
|
| 215 |
async def upload_source(image: UploadFile = File(...)):
|
| 216 |
logger.info(f"Uploading source image: {image.filename}")
|
| 217 |
contents = await image.read()
|
|
|
|
| 224 |
logger.info(f"Source image uploaded with ID: {str(result.inserted_id)}")
|
| 225 |
return {"source_id": str(result.inserted_id)}
|
| 226 |
|
| 227 |
+
@fastapi_app.post("/target")
|
| 228 |
async def upload_target(image: UploadFile = File(...)):
|
| 229 |
logger.info(f"Uploading target image: {image.filename}")
|
| 230 |
contents = await image.read()
|
|
|
|
| 241 |
source_id: str
|
| 242 |
target_id: str
|
| 243 |
|
| 244 |
+
@fastapi_app.post("/faceswap")
|
| 245 |
async def perform_faceswap(request: FaceSwapRequest):
|
| 246 |
logger.info(f"Starting face swap for source_id: {request.source_id}, target_id: {request.target_id}")
|
| 247 |
source_doc = await source_images_collection.find_one({"_id": ObjectId(request.source_id)})
|
|
|
|
| 287 |
logger.info(f"Face swap result stored with ID: {str(result.inserted_id)}")
|
| 288 |
return {"result_id": str(result.inserted_id)}
|
| 289 |
|
| 290 |
+
@fastapi_app.get("/download/{result_id}")
|
| 291 |
async def download_result(result_id: str):
|
| 292 |
logger.info(f"Downloading result: {result_id}")
|
| 293 |
doc = await results_collection.find_one({"_id": ObjectId(result_id)})
|
|
|
|
| 301 |
)
|
| 302 |
|
| 303 |
# Mount Gradio at /gradio
|
| 304 |
+
fastapi_app = mount_gradio_app(fastapi_app, demo, path="/gradio")
|
| 305 |
|
| 306 |
# Run the app with Uvicorn
|
| 307 |
if __name__ == "__main__":
|
| 308 |
+
uvicorn.run(fastapi_app, host="0.0.0.0", port=7860)
|
test.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import requests
|
| 2 |
+
|
| 3 |
+
# response = requests.get("https://logicgoinfotechspaces-faceswap.hf.space/health")
|
| 4 |
+
# print(response.status_code) # Should be 200
|
| 5 |
+
# print(response.json()) # Should print: {'status': 'healthy'}
|
| 6 |
+
|
| 7 |
+
# import requests
|
| 8 |
+
|
| 9 |
+
# with open("./source.jpeg", "rb") as f:
|
| 10 |
+
# files = {"image": ("./source.jpeg", f, "image/jpeg")}
|
| 11 |
+
# response = requests.post("https://logicgoinfotechspaces-faceswap.hf.space/source", files=files)
|
| 12 |
+
# print(response.status_code) # Should be 200
|
| 13 |
+
# print(response.json()) # Should print: {'source_id': '<id>'}
|
| 14 |
+
# source_id = response.json().get("source_id")
|
| 15 |
+
|
| 16 |
+
# import requests
|
| 17 |
+
|
| 18 |
+
# with open("./target.jpeg", "rb") as f:
|
| 19 |
+
# files = {"image": ("./target.jpeg", f, "image/jpeg")}
|
| 20 |
+
# response = requests.post("https://logicgoinfotechspaces-faceswap.hf.space/target", files=files)
|
| 21 |
+
# print(response.status_code) # Should be 200
|
| 22 |
+
# print(response.json()) # Should print: {'target_id': '<id>'}
|
| 23 |
+
# target_id = response.json().get("target_id")
|
| 24 |
+
|
| 25 |
+
import requests
|
| 26 |
+
|
| 27 |
+
source_id = "68bff17eb3b2e06c24cafa22" # Replace with actual ID
|
| 28 |
+
target_id = "68bff1a1b3b2e06c24cafa23" # Replace with actual ID
|
| 29 |
+
payload = {"source_id": source_id, "target_id": target_id}
|
| 30 |
+
response = requests.post("https://logicgoinfotechspaces-faceswap.hf.space/faceswap", json=payload)
|
| 31 |
+
print(response.status_code) # Should be 200
|
| 32 |
+
print(response.json()) # Should print: {'result_id': '<id>'}
|
| 33 |
+
result_id = response.json().get("result_id")
|