Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -112,43 +112,46 @@ class ModelManager:
|
|
| 112 |
return self._breed_model
|
| 113 |
|
| 114 |
def _initialize_enhanced_system(self):
|
| 115 |
-
"""Initialize enhanced multi-dimensional recommendation system"""
|
| 116 |
if ModelManager._enhanced_system_initialized:
|
| 117 |
return
|
| 118 |
-
|
| 119 |
try:
|
| 120 |
-
#
|
| 121 |
try:
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
| 127 |
try:
|
| 128 |
-
|
| 129 |
-
|
|
|
|
| 130 |
break
|
| 131 |
except Exception as e:
|
| 132 |
-
print(f"Failed to load SBERT model {
|
| 133 |
continue
|
| 134 |
-
|
| 135 |
if self._sbert_model is None:
|
| 136 |
-
print("All SBERT models failed to
|
| 137 |
-
|
| 138 |
except Exception as e:
|
| 139 |
-
print(f"SBERT initialization failed: {
|
| 140 |
self._sbert_model = None
|
| 141 |
-
|
| 142 |
ModelManager._enhanced_system_initialized = True
|
| 143 |
-
print("Enhanced recommendation system initialization completed")
|
| 144 |
-
|
| 145 |
except ImportError as e:
|
| 146 |
-
print(f"Enhanced modules not available: {
|
| 147 |
-
ModelManager._enhanced_system_initialized = True
|
| 148 |
except Exception as e:
|
| 149 |
-
print(f"Enhanced system initialization failed: {
|
| 150 |
print(traceback.format_exc())
|
| 151 |
-
ModelManager._enhanced_system_initialized = True
|
| 152 |
|
| 153 |
@property
|
| 154 |
def sbert_model(self):
|
|
@@ -630,4 +633,4 @@ def main():
|
|
| 630 |
|
| 631 |
if __name__ == "__main__":
|
| 632 |
iface = main()
|
| 633 |
-
iface.launch()
|
|
|
|
| 112 |
return self._breed_model
|
| 113 |
|
| 114 |
def _initialize_enhanced_system(self):
|
| 115 |
+
"""Initialize enhanced multi-dimensional recommendation system (CPU-only load)"""
|
| 116 |
if ModelManager._enhanced_system_initialized:
|
| 117 |
return
|
| 118 |
+
|
| 119 |
try:
|
| 120 |
+
# Always load SBERT on CPU in the main process to avoid CUDA init
|
| 121 |
try:
|
| 122 |
+
model_options = [
|
| 123 |
+
'sentence-transformers/all-MiniLM-L6-v2',
|
| 124 |
+
'sentence-transformers/all-mpnet-base-v2',
|
| 125 |
+
'sentence-transformers/all-MiniLM-L12-v2'
|
| 126 |
+
]
|
| 127 |
+
self._sbert_model = None
|
| 128 |
+
for name in model_options:
|
| 129 |
try:
|
| 130 |
+
# CPU 載入,禁止在主行程碰 CUDA
|
| 131 |
+
self._sbert_model = SentenceTransformer(name, device='cpu')
|
| 132 |
+
print(f"Initialized SBERT model on CPU: {name}")
|
| 133 |
break
|
| 134 |
except Exception as e:
|
| 135 |
+
print(f"Failed to load SBERT model {name}: {e}")
|
| 136 |
continue
|
| 137 |
+
|
| 138 |
if self._sbert_model is None:
|
| 139 |
+
print("All SBERT models failed; fallback to keyword-only analysis")
|
| 140 |
+
|
| 141 |
except Exception as e:
|
| 142 |
+
print(f"SBERT initialization failed: {e}")
|
| 143 |
self._sbert_model = None
|
| 144 |
+
|
| 145 |
ModelManager._enhanced_system_initialized = True
|
| 146 |
+
print("Enhanced recommendation system initialization completed (CPU)")
|
| 147 |
+
|
| 148 |
except ImportError as e:
|
| 149 |
+
print(f"Enhanced modules not available: {e}")
|
| 150 |
+
ModelManager._enhanced_system_initialized = True
|
| 151 |
except Exception as e:
|
| 152 |
+
print(f"Enhanced system initialization failed: {e}")
|
| 153 |
print(traceback.format_exc())
|
| 154 |
+
ModelManager._enhanced_system_initialized = True
|
| 155 |
|
| 156 |
@property
|
| 157 |
def sbert_model(self):
|
|
|
|
| 633 |
|
| 634 |
if __name__ == "__main__":
|
| 635 |
iface = main()
|
| 636 |
+
iface.launch()
|