Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -41,30 +41,30 @@ from ultralytics import YOLO
|
|
| 41 |
import asyncio
|
| 42 |
import traceback
|
| 43 |
|
| 44 |
-
def setup_environment():
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
|
| 67 |
-
cuda_available = setup_environment()
|
| 68 |
|
| 69 |
history_manager = UserHistoryManager()
|
| 70 |
|
|
@@ -95,41 +95,41 @@ class ModelManager:
|
|
| 95 |
self._device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 96 |
return self._device
|
| 97 |
|
| 98 |
-
# @property
|
| 99 |
-
# def yolo_model(self):
|
| 100 |
-
# if self._yolo_model is None:
|
| 101 |
-
# self._yolo_model = YOLO('yolov8x.pt')
|
| 102 |
-
# return self._yolo_model
|
| 103 |
-
|
| 104 |
@property
|
| 105 |
def yolo_model(self):
|
| 106 |
if self._yolo_model is None:
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
|
| 132 |
-
|
| 133 |
|
| 134 |
@property
|
| 135 |
def breed_model(self):
|
|
|
|
| 41 |
import asyncio
|
| 42 |
import traceback
|
| 43 |
|
| 44 |
+
# def setup_environment():
|
| 45 |
+
# """配置適合 ZeroGPU 環境的設置"""
|
| 46 |
+
# # 啟用 CUDA 錯誤的同步報告,幫助診斷問題
|
| 47 |
+
# os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
|
| 48 |
|
| 49 |
+
# # 檢查 CUDA 是否可用
|
| 50 |
+
# if torch.cuda.is_available():
|
| 51 |
+
# # 顯示 GPU 信息
|
| 52 |
+
# device_name = torch.cuda.get_device_name(0)
|
| 53 |
+
# print(f"使用 GPU: {device_name}")
|
| 54 |
|
| 55 |
+
# # 針對 A100 的優化設置
|
| 56 |
+
# if "A100" in device_name:
|
| 57 |
+
# print("檢測到 A100 GPU,應用特殊優化...")
|
| 58 |
+
# torch.backends.cudnn.benchmark = True
|
| 59 |
|
| 60 |
+
# # 清理 GPU 內存
|
| 61 |
+
# torch.cuda.empty_cache()
|
| 62 |
+
# return True
|
| 63 |
+
# else:
|
| 64 |
+
# print("CUDA 不可用,使用 CPU 模式")
|
| 65 |
+
# return False
|
| 66 |
|
| 67 |
+
# cuda_available = setup_environment()
|
| 68 |
|
| 69 |
history_manager = UserHistoryManager()
|
| 70 |
|
|
|
|
| 95 |
self._device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 96 |
return self._device
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
@property
|
| 99 |
def yolo_model(self):
|
| 100 |
if self._yolo_model is None:
|
| 101 |
+
self._yolo_model = YOLO('yolov8x.pt')
|
| 102 |
+
return self._yolo_model
|
| 103 |
+
|
| 104 |
+
# @property
|
| 105 |
+
# def yolo_model(self):
|
| 106 |
+
# if self._yolo_model is None:
|
| 107 |
+
# try:
|
| 108 |
+
# print("正在加載 YOLO 模型...")
|
| 109 |
+
# # 不指定設備,讓 YOLO 自動選擇
|
| 110 |
+
# self._yolo_model = YOLO('yolov8x.pt')
|
| 111 |
|
| 112 |
+
# # 禁用模型融合來避免 CUDA 錯誤
|
| 113 |
+
# if hasattr(self._yolo_model, 'model') and hasattr(self._yolo_model.model, 'fuse'):
|
| 114 |
+
# print("在 ZeroGPU 環境下禁用模型融合以避免 CUDA 錯誤")
|
| 115 |
|
| 116 |
+
# # 備份原始融合方法
|
| 117 |
+
# original_fuse = self._yolo_model.model.fuse
|
| 118 |
|
| 119 |
+
# # 創建一個空的融合方法
|
| 120 |
+
# def no_fuse(*args, **kwargs):
|
| 121 |
+
# print("已跳過融合操作")
|
| 122 |
+
# return self._yolo_model.model
|
| 123 |
|
| 124 |
+
# # 替換融合方法
|
| 125 |
+
# self._yolo_model.model.fuse = no_fuse
|
| 126 |
+
# except Exception as e:
|
| 127 |
+
# print(f"加載 YOLO 模型時出錯: {str(e)}")
|
| 128 |
+
# print("嘗試降級到較小的模型和 CPU 模式...")
|
| 129 |
+
# # 降級到較小的模型並明確使用 CPU
|
| 130 |
+
# self._yolo_model = YOLO('yolov8n.pt', device='cpu')
|
| 131 |
|
| 132 |
+
# return self._yolo_model
|
| 133 |
|
| 134 |
@property
|
| 135 |
def breed_model(self):
|