Spaces:
Sleeping
Sleeping
Download python_backend/api/Predictors/routes/faceFind_routes.py from VashuTheGreat2/ML-Learner: direct link, hf CLI and curl.
- Browser
- Download file 1.31 kB
-
https://huggingface.co/spaces/VashuTheGreat2/ML-Learner/resolve/main/python_backend/api/Predictors/routes/faceFind_routes.py
- Command line
-
hf download hf://spaces/VashuTheGreat2/ML-Learner/python_backend/api/Predictors/routes/faceFind_routes.py
-
curl -L -o faceFind_routes.py https://huggingface.co/spaces/VashuTheGreat2/ML-Learner/resolve/main/python_backend/api/Predictors/routes/faceFind_routes.py
1.31 kB
| import fastapi | |
| from fastapi import WebSocket, WebSocketDisconnect | |
| from PIL import Image | |
| import io | |
| from src.Predictors.pipelines.faceFinder_pipeline import FaceFinderPipeline | |
| router = fastapi.APIRouter() | |
| async def find_face(websocket: WebSocket): | |
| await websocket.accept() | |
| pipeline = FaceFinderPipeline() | |
| try: | |
| while True: | |
| data = await websocket.receive_bytes() | |
| try: | |
| img = Image.open(io.BytesIO(data)).convert("RGB") | |
| result = await pipeline.initiate(img=img) | |
| await websocket.send_json(result) | |
| except WebSocketDisconnect: | |
| raise | |
| except Exception as e: | |
| if "cannot identify image file" in str(e).lower(): | |
| continue | |
| try: | |
| await websocket.send_json({ | |
| "is_error": True, | |
| "error_message": str(e), | |
| "data": None | |
| }) | |
| except Exception: | |
| print(f"Could not send error to client: {e}") | |
| except WebSocketDisconnect: | |
| print("Client safely disconnected") | |
| except Exception as e: | |
| print("Unexpected error:", e) |