DawnC commited on
Commit
57fb906
·
verified ·
1 Parent(s): 7466dd7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -23
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
- # Initialize SBERT model for semantic analysis
121
  try:
122
- # Use default model configuration
123
- model_name = 'all-MiniLM-L6-v2'
124
- fallback_models = ['all-mpnet-base-v2', 'all-MiniLM-L12-v2']
125
-
126
- for model_name_attempt in [model_name] + fallback_models:
 
 
127
  try:
128
- self._sbert_model = SentenceTransformer(model_name_attempt)
129
- print(f"Initialized SBERT model: {model_name_attempt}")
 
130
  break
131
  except Exception as e:
132
- print(f"Failed to load SBERT model {model_name_attempt}: {str(e)}")
133
  continue
134
-
135
  if self._sbert_model is None:
136
- print("All SBERT models failed to load, enhanced system will use keyword-only analysis")
137
-
138
  except Exception as e:
139
- print(f"SBERT initialization failed: {str(e)}")
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: {str(e)}")
147
- ModelManager._enhanced_system_initialized = True # Mark as attempted
148
  except Exception as e:
149
- print(f"Enhanced system initialization failed: {str(e)}")
150
  print(traceback.format_exc())
151
- ModelManager._enhanced_system_initialized = True # Mark as attempted
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()