HariLogicgo commited on
Commit
15642e3
Β·
1 Parent(s): 31384f7

api db changed

Browse files
Files changed (1) hide show
  1. app.py +12 -22
app.py CHANGED
@@ -127,17 +127,17 @@ def verify_token(credentials: HTTPAuthorizationCredentials = Security(security))
127
  raise HTTPException(status_code=401, detail="Invalid or missing token")
128
  return credentials.credentials
129
 
130
- # --------------------- Logging Helper ---------------------
131
  async def log_faceswap_hit(token: str, status: str = "success"):
132
- if not database:
 
133
  return
134
- log_entry = {
135
- "endpoint": "/faceswap",
136
  "token": token,
 
137
  "status": status,
138
  "timestamp": datetime.utcnow()
139
- }
140
- await database.api_logs.insert_one(log_entry)
141
 
142
  # --------------------- Face Swap Pipeline ---------------------
143
  swap_lock = threading.Lock()
@@ -237,18 +237,15 @@ class FaceSwapRequest(BaseModel):
237
  source_id: str
238
  target_id: str
239
 
240
- @fastapi_app.post("/faceswap")
241
- async def perform_faceswap(
242
- request: FaceSwapRequest,
243
- credentials: HTTPAuthorizationCredentials = Security(security)
244
- ):
245
  try:
246
  src_stream = await fs_bucket.open_download_stream(ObjectId(request.source_id))
247
  src_bytes = await src_stream.read()
248
  tgt_stream = await fs_bucket.open_download_stream(ObjectId(request.target_id))
249
  tgt_bytes = await tgt_stream.read()
250
  except Exception:
251
- await log_faceswap_hit(credentials.credentials, status="not_found")
252
  raise HTTPException(status_code=404, detail="Source or Target not found")
253
 
254
  src_array = np.frombuffer(src_bytes, np.uint8)
@@ -257,7 +254,7 @@ async def perform_faceswap(
257
  tgt_bgr = cv2.imdecode(tgt_array, cv2.IMREAD_COLOR)
258
 
259
  if src_bgr is None or tgt_bgr is None:
260
- await log_faceswap_hit(credentials.credentials, status="bad_request")
261
  raise HTTPException(status_code=400, detail="Invalid image data")
262
 
263
  src_rgb = cv2.cvtColor(src_bgr, cv2.COLOR_BGR2RGB)
@@ -265,7 +262,7 @@ async def perform_faceswap(
265
 
266
  final_img, final_path, err = face_swap_and_enhance(src_rgb, tgt_rgb)
267
  if err:
268
- await log_faceswap_hit(credentials.credentials, status="failed")
269
  raise HTTPException(status_code=500, detail=err)
270
 
271
  with open(final_path, "rb") as f:
@@ -273,7 +270,7 @@ async def perform_faceswap(
273
 
274
  result_id = await fs_bucket.upload_from_stream("enhanced.png", final_bytes, metadata={"type": "result"})
275
 
276
- # βœ… Log successful request
277
  await log_faceswap_hit(credentials.credentials, status="success")
278
 
279
  return {"result_id": str(result_id)}
@@ -292,13 +289,6 @@ async def download_result(result_id: str):
292
  headers={"Content-Disposition": "attachment; filename=result.png"}
293
  )
294
 
295
- # --------------------- Stats Endpoint ---------------------
296
- @fastapi_app.get("/stats", dependencies=[Depends(verify_token)])
297
- async def get_stats(credentials: HTTPAuthorizationCredentials = Security(security)):
298
- token = credentials.credentials
299
- count = await database.api_logs.count_documents({"endpoint": "/faceswap", "token": token})
300
- return {"token": token, "faceswap_calls": count}
301
-
302
  # --------------------- Mount Gradio ---------------------
303
  fastapi_app = mount_gradio_app(fastapi_app, demo, path="/gradio")
304
 
 
127
  raise HTTPException(status_code=401, detail="Invalid or missing token")
128
  return credentials.credentials
129
 
130
+ # --------------------- Logging API Hits ---------------------
131
  async def log_faceswap_hit(token: str, status: str = "success"):
132
+ global database
133
+ if database is None: # βœ… FIXED: explicit None check
134
  return
135
+ await database.api_logs.insert_one({
 
136
  "token": token,
137
+ "endpoint": "/faceswap",
138
  "status": status,
139
  "timestamp": datetime.utcnow()
140
+ })
 
141
 
142
  # --------------------- Face Swap Pipeline ---------------------
143
  swap_lock = threading.Lock()
 
237
  source_id: str
238
  target_id: str
239
 
240
+ @fastapi_app.post("/faceswap", dependencies=[Depends(verify_token)])
241
+ async def perform_faceswap(request: FaceSwapRequest, credentials: HTTPAuthorizationCredentials = Security(security)):
 
 
 
242
  try:
243
  src_stream = await fs_bucket.open_download_stream(ObjectId(request.source_id))
244
  src_bytes = await src_stream.read()
245
  tgt_stream = await fs_bucket.open_download_stream(ObjectId(request.target_id))
246
  tgt_bytes = await tgt_stream.read()
247
  except Exception:
248
+ await log_faceswap_hit(credentials.credentials, status="error")
249
  raise HTTPException(status_code=404, detail="Source or Target not found")
250
 
251
  src_array = np.frombuffer(src_bytes, np.uint8)
 
254
  tgt_bgr = cv2.imdecode(tgt_array, cv2.IMREAD_COLOR)
255
 
256
  if src_bgr is None or tgt_bgr is None:
257
+ await log_faceswap_hit(credentials.credentials, status="error")
258
  raise HTTPException(status_code=400, detail="Invalid image data")
259
 
260
  src_rgb = cv2.cvtColor(src_bgr, cv2.COLOR_BGR2RGB)
 
262
 
263
  final_img, final_path, err = face_swap_and_enhance(src_rgb, tgt_rgb)
264
  if err:
265
+ await log_faceswap_hit(credentials.credentials, status="error")
266
  raise HTTPException(status_code=500, detail=err)
267
 
268
  with open(final_path, "rb") as f:
 
270
 
271
  result_id = await fs_bucket.upload_from_stream("enhanced.png", final_bytes, metadata={"type": "result"})
272
 
273
+ # βœ… Log success
274
  await log_faceswap_hit(credentials.credentials, status="success")
275
 
276
  return {"result_id": str(result_id)}
 
289
  headers={"Content-Disposition": "attachment; filename=result.png"}
290
  )
291
 
 
 
 
 
 
 
 
292
  # --------------------- Mount Gradio ---------------------
293
  fastapi_app = mount_gradio_app(fastapi_app, demo, path="/gradio")
294