htrnguyen commited on
Commit
3721375
·
1 Parent(s): 5782dca

Update API server health check endpoint

Browse files
Files changed (1) hide show
  1. api_server.py +26 -17
api_server.py CHANGED
@@ -19,6 +19,7 @@ templates = Jinja2Templates(directory=TEMPLATES_DIR)
19
  from main import analyze_video_fast
20
  from reengineer import reengineer_video
21
 
 
22
  @app.get("/")
23
  async def root(request: Request):
24
  """
@@ -26,8 +27,11 @@ async def root(request: Request):
26
  """
27
  return templates.TemplateResponse("index.html", {"request": request})
28
 
 
29
  @app.post("/")
30
- async def analyze_with_video(file: UploadFile = File(...), background_tasks: BackgroundTasks = None):
 
 
31
  """
32
  UI endpoint: Phân tích + Tạo video có overlay.
33
  Trả về video để download.
@@ -45,16 +49,18 @@ async def analyze_with_video(file: UploadFile = File(...), background_tasks: Bac
45
 
46
  # Phân tích (lưu vào output/)
47
  master_json = os.path.join(output_dir, "master_data.json")
48
- result = analyze_video_fast(video_path, production=True, output_file=master_json, output_base="output")
49
-
 
 
50
  # Tạo video có overlay
51
  output_video = os.path.join(output_dir, "analyzed_video.mp4")
52
  reengineer_video(master_json, video_path, output_video, production=True)
53
-
54
  # Cleanup video gốc
55
- if os.path.exists(video_path):
56
  os.remove(video_path)
57
-
58
  # Schedule cleanup after response
59
  def cleanup():
60
  try:
@@ -62,24 +68,23 @@ async def analyze_with_video(file: UploadFile = File(...), background_tasks: Bac
62
  shutil.rmtree(output_dir, ignore_errors=True)
63
  except:
64
  pass
65
-
66
  if background_tasks:
67
  background_tasks.add_task(cleanup)
68
-
69
  # Trả về video file
70
  return FileResponse(
71
- output_video,
72
- media_type="video/mp4",
73
- filename=f"golf_analysis_{job_id}.mp4"
74
  )
75
 
76
  except Exception as e:
77
- if os.path.exists(video_path):
78
  os.remove(video_path)
79
  if os.path.exists(output_dir):
80
  shutil.rmtree(output_dir, ignore_errors=True)
81
  raise HTTPException(status_code=500, detail=str(e))
82
 
 
83
  @app.post("/api/analyze")
84
  async def api_analyze(file: UploadFile = File(...)):
85
  """
@@ -97,22 +102,26 @@ async def api_analyze(file: UploadFile = File(...)):
97
 
98
  # Gọi optimized pipeline (KHÔNG tạo video)
99
  result = analyze_video_fast(video_path, production=True)
100
-
101
  # Cleanup
102
- if os.path.exists(video_path):
103
  os.remove(video_path)
104
-
105
  return result
106
 
107
  except Exception as e:
108
- if os.path.exists(video_path):
109
  os.remove(video_path)
110
  raise HTTPException(status_code=500, detail=str(e))
111
 
112
- @app.post("/api/health")
 
 
113
  async def health_check():
114
  return {"status": "ok"}
115
 
 
116
  if __name__ == "__main__":
117
  import uvicorn
 
118
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
19
  from main import analyze_video_fast
20
  from reengineer import reengineer_video
21
 
22
+
23
  @app.get("/")
24
  async def root(request: Request):
25
  """
 
27
  """
28
  return templates.TemplateResponse("index.html", {"request": request})
29
 
30
+
31
  @app.post("/")
32
+ async def analyze_with_video(
33
+ file: UploadFile = File(...), background_tasks: BackgroundTasks = None
34
+ ):
35
  """
36
  UI endpoint: Phân tích + Tạo video có overlay.
37
  Trả về video để download.
 
49
 
50
  # Phân tích (lưu vào output/)
51
  master_json = os.path.join(output_dir, "master_data.json")
52
+ result = analyze_video_fast(
53
+ video_path, production=True, output_file=master_json, output_base="output"
54
+ )
55
+
56
  # Tạo video có overlay
57
  output_video = os.path.join(output_dir, "analyzed_video.mp4")
58
  reengineer_video(master_json, video_path, output_video, production=True)
59
+
60
  # Cleanup video gốc
61
+ if os.path.exists(video_path):
62
  os.remove(video_path)
63
+
64
  # Schedule cleanup after response
65
  def cleanup():
66
  try:
 
68
  shutil.rmtree(output_dir, ignore_errors=True)
69
  except:
70
  pass
71
+
72
  if background_tasks:
73
  background_tasks.add_task(cleanup)
74
+
75
  # Trả về video file
76
  return FileResponse(
77
+ output_video, media_type="video/mp4", filename=f"golf_analysis_{job_id}.mp4"
 
 
78
  )
79
 
80
  except Exception as e:
81
+ if os.path.exists(video_path):
82
  os.remove(video_path)
83
  if os.path.exists(output_dir):
84
  shutil.rmtree(output_dir, ignore_errors=True)
85
  raise HTTPException(status_code=500, detail=str(e))
86
 
87
+
88
  @app.post("/api/analyze")
89
  async def api_analyze(file: UploadFile = File(...)):
90
  """
 
102
 
103
  # Gọi optimized pipeline (KHÔNG tạo video)
104
  result = analyze_video_fast(video_path, production=True)
105
+
106
  # Cleanup
107
+ if os.path.exists(video_path):
108
  os.remove(video_path)
109
+
110
  return result
111
 
112
  except Exception as e:
113
+ if os.path.exists(video_path):
114
  os.remove(video_path)
115
  raise HTTPException(status_code=500, detail=str(e))
116
 
117
+
118
+ # Check health
119
+ @app.get("/api/health")
120
  async def health_check():
121
  return {"status": "ok"}
122
 
123
+
124
  if __name__ == "__main__":
125
  import uvicorn
126
+
127
  uvicorn.run(app, host="0.0.0.0", port=7860)