Khanh0210 commited on
Commit
36c4b01
·
verified ·
1 Parent(s): b1a4371

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -27
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import os
2
  import base64
3
  import io
@@ -6,9 +7,6 @@ import re
6
  import cv2
7
  import numpy as np
8
  import gradio as gr
9
-
10
- os.environ["YOLO_CONFIG_DIR"] = "/tmp"
11
-
12
  from fastapi import FastAPI, File, UploadFile, Form
13
  from fastapi.middleware.cors import CORSMiddleware
14
  from gtts import gTTS
@@ -16,6 +14,8 @@ from pydantic import BaseModel
16
  from ultralytics import YOLO
17
  from PIL import Image
18
 
 
 
19
  try:
20
  from google import genai
21
  except ImportError:
@@ -30,10 +30,6 @@ fastapi_app.add_middleware(
30
  allow_headers=["*"],
31
  )
32
 
33
- # ==========================================
34
- # 1. LAZY INITIALIZATION CHO MODEL YOLO
35
- # Tránh crash khi HuggingFace khởi động app
36
- # ==========================================
37
  desk_model = None
38
  pose_model = None
39
 
@@ -49,9 +45,16 @@ def get_pose_model():
49
  pose_model = YOLO('yolov8n-pose.pt')
50
  return pose_model
51
 
52
- # ==========================================
53
- # 2. CẤU HÌNH API KEY VÀ CÁC BIẾN CONSTANT
54
- # ==========================================
 
 
 
 
 
 
 
55
  GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY", "").strip()
56
  gemini_client = None
57
  if GEMINI_API_KEY and genai:
@@ -70,17 +73,6 @@ class ImageData(BaseModel):
70
  baseline_eye_dist: float = 0.0
71
  baseline_shoulder_y: float = 0.0
72
 
73
- # ==========================================
74
- # 3. CÁC HÀM XỬ LÝ LÕI (CORE FUNCTIONS)
75
- # ==========================================
76
- def infer_desk(img_out):
77
- model = get_desk_model() # Gọi lazy
78
- return model(img_out, conf=0.20, imgsz=640, verbose=False, device='cpu')
79
-
80
- def infer_pose(img):
81
- model = get_pose_model() # Gọi lazy
82
- return model(img, imgsz=320, verbose=False, device='cpu')
83
-
84
  def calculate_distance(p1, p2):
85
  return math.sqrt((p2[0] - p1[0])**2 + (p2[1] - p1[1])**2)
86
 
@@ -118,7 +110,6 @@ def process_single_image(img, user_height):
118
  object_coords = {}
119
  for r in results:
120
  for box in r.boxes:
121
- # Dùng get_desk_model() thay cho desk_model
122
  label = get_desk_model().names[int(box.cls[0])].lower()
123
  x_center, y_center, width, height = box.xywh[0].tolist()
124
  if label not in object_coords:
@@ -170,9 +161,6 @@ def process_single_image(img, user_height):
170
  score = max(0, 100 - deductions)
171
  return img_out, list(dict.fromkeys(violations)), score
172
 
173
- # ==========================================
174
- # 4. FASTAPI ENDPOINTS
175
- # ==========================================
176
  @fastapi_app.get("/")
177
  def read_root():
178
  return {"status": "online", "message": "ErgoAI Production Backend is running!"}
@@ -181,8 +169,7 @@ def read_root():
181
  def assess_desk(
182
  file: UploadFile = File(...),
183
  user_height: float = Form(170.0),
184
- fatigue_level: int = Form(30),
185
- custom_notes: str = Form("")
186
  ):
187
  contents = file.file.read()
188
  nparr = np.frombuffer(contents, np.uint8)
 
1
+ import spaces
2
  import os
3
  import base64
4
  import io
 
7
  import cv2
8
  import numpy as np
9
  import gradio as gr
 
 
 
10
  from fastapi import FastAPI, File, UploadFile, Form
11
  from fastapi.middleware.cors import CORSMiddleware
12
  from gtts import gTTS
 
14
  from ultralytics import YOLO
15
  from PIL import Image
16
 
17
+ os.environ["YOLO_CONFIG_DIR"] = "/tmp"
18
+
19
  try:
20
  from google import genai
21
  except ImportError:
 
30
  allow_headers=["*"],
31
  )
32
 
 
 
 
 
33
  desk_model = None
34
  pose_model = None
35
 
 
45
  pose_model = YOLO('yolov8n-pose.pt')
46
  return pose_model
47
 
48
+ @spaces.GPU
49
+ def infer_desk(img_out):
50
+ model = get_desk_model()
51
+ return model(img_out, conf=0.20, imgsz=640, verbose=False)
52
+
53
+ @spaces.GPU
54
+ def infer_pose(img):
55
+ model = get_pose_model()
56
+ return model(img, imgsz=320, verbose=False)
57
+
58
  GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY", "").strip()
59
  gemini_client = None
60
  if GEMINI_API_KEY and genai:
 
73
  baseline_eye_dist: float = 0.0
74
  baseline_shoulder_y: float = 0.0
75
 
 
 
 
 
 
 
 
 
 
 
 
76
  def calculate_distance(p1, p2):
77
  return math.sqrt((p2[0] - p1[0])**2 + (p2[1] - p1[1])**2)
78
 
 
110
  object_coords = {}
111
  for r in results:
112
  for box in r.boxes:
 
113
  label = get_desk_model().names[int(box.cls[0])].lower()
114
  x_center, y_center, width, height = box.xywh[0].tolist()
115
  if label not in object_coords:
 
161
  score = max(0, 100 - deductions)
162
  return img_out, list(dict.fromkeys(violations)), score
163
 
 
 
 
164
  @fastapi_app.get("/")
165
  def read_root():
166
  return {"status": "online", "message": "ErgoAI Production Backend is running!"}
 
169
  def assess_desk(
170
  file: UploadFile = File(...),
171
  user_height: float = Form(170.0),
172
+ fatigue_level: int = Form(30)
 
173
  ):
174
  contents = file.file.read()
175
  nparr = np.frombuffer(contents, np.uint8)