Spaces:
Running
Running
update
Browse files
app.py
CHANGED
|
@@ -37,7 +37,10 @@ except Exception as e:
|
|
| 37 |
# ==========================================
|
| 38 |
# MODELS
|
| 39 |
# ==========================================
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
class VerifyResponse(BaseModel):
|
| 43 |
is_match: bool
|
|
@@ -56,7 +59,7 @@ class ValidationResponse(BaseModel):
|
|
| 56 |
# ==========================================
|
| 57 |
def download_image(url: str) -> str:
|
| 58 |
try:
|
| 59 |
-
response = requests.get(url, timeout=10)
|
| 60 |
response.raise_for_status()
|
| 61 |
unique_name = f"{uuid.uuid4().hex}.jpg"
|
| 62 |
file_path = os.path.join(TEMP_DIR, unique_name)
|
|
@@ -79,10 +82,52 @@ def health_check():
|
|
| 79 |
status = "ok" if face_app else "error"
|
| 80 |
return {
|
| 81 |
"status": status,
|
| 82 |
-
"message": "🚀 GawayKu Face Recognition API is running (v1.
|
| 83 |
"model": "YOLOv8 + ArcFace (WideResNet-101-2)",
|
| 84 |
}
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
@app.post("/verify-file", response_model=VerifyResponse)
|
| 87 |
async def verify_file(
|
| 88 |
reference_image_url: str = Form(...),
|
|
|
|
| 37 |
# ==========================================
|
| 38 |
# MODELS
|
| 39 |
# ==========================================
|
| 40 |
+
|
| 41 |
+
class VerifyRequest(BaseModel):
|
| 42 |
+
reference_image_url: HttpUrl
|
| 43 |
+
query_image_url: HttpUrl
|
| 44 |
|
| 45 |
class VerifyResponse(BaseModel):
|
| 46 |
is_match: bool
|
|
|
|
| 59 |
# ==========================================
|
| 60 |
def download_image(url: str) -> str:
|
| 61 |
try:
|
| 62 |
+
response = requests.get(str(url), timeout=10)
|
| 63 |
response.raise_for_status()
|
| 64 |
unique_name = f"{uuid.uuid4().hex}.jpg"
|
| 65 |
file_path = os.path.join(TEMP_DIR, unique_name)
|
|
|
|
| 82 |
status = "ok" if face_app else "error"
|
| 83 |
return {
|
| 84 |
"status": status,
|
| 85 |
+
"message": "🚀 GawayKu Face Recognition API is running (v1.3 - URL & Direct Upload)!",
|
| 86 |
"model": "YOLOv8 + ArcFace (WideResNet-101-2)",
|
| 87 |
}
|
| 88 |
|
| 89 |
+
@app.post("/verify", response_model=VerifyResponse)
|
| 90 |
+
async def verify(request: VerifyRequest):
|
| 91 |
+
"""
|
| 92 |
+
Membandingkan Wajah Referensi (URL) vs Wajah Query (URL).
|
| 93 |
+
Cocok untuk skenario dimana foto sudah diupload ke Cloudinary/Storage.
|
| 94 |
+
"""
|
| 95 |
+
if not face_app:
|
| 96 |
+
raise HTTPException(status_code=503, detail="Model belum siap")
|
| 97 |
+
|
| 98 |
+
start_time = time.time()
|
| 99 |
+
path_ref = None
|
| 100 |
+
path_query = None
|
| 101 |
+
|
| 102 |
+
try:
|
| 103 |
+
# 1. Download Both Images
|
| 104 |
+
path_ref = download_image(request.reference_image_url)
|
| 105 |
+
path_query = download_image(request.query_image_url)
|
| 106 |
+
|
| 107 |
+
# 2. Compare (Path vs Path)
|
| 108 |
+
similarity, is_same = face_app.compare(path_ref, path_query)
|
| 109 |
+
|
| 110 |
+
exec_time = time.time() - start_time
|
| 111 |
+
|
| 112 |
+
return {
|
| 113 |
+
"is_match": bool(is_same),
|
| 114 |
+
"similarity_score": round(float(similarity), 4),
|
| 115 |
+
"execution_time": round(exec_time, 4),
|
| 116 |
+
"error": None
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
except Exception as e:
|
| 120 |
+
exec_time = time.time() - start_time
|
| 121 |
+
return {
|
| 122 |
+
"is_match": False,
|
| 123 |
+
"similarity_score": 0.0,
|
| 124 |
+
"execution_time": round(exec_time, 4),
|
| 125 |
+
"error": str(e)
|
| 126 |
+
}
|
| 127 |
+
finally:
|
| 128 |
+
clear_temp_file(path_ref)
|
| 129 |
+
clear_temp_file(path_query)
|
| 130 |
+
|
| 131 |
@app.post("/verify-file", response_model=VerifyResponse)
|
| 132 |
async def verify_file(
|
| 133 |
reference_image_url: str = Form(...),
|