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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -34
app.py CHANGED
@@ -112,53 +112,62 @@ class ModelManager:
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):
158
  """Get SBERT model for semantic analysis"""
159
  if not ModelManager._enhanced_system_initialized:
160
  self._initialize_enhanced_system()
161
- return self._sbert_model
162
 
163
  @property
164
  def config_manager(self):
 
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
+ # Defer SBERT model loading until needed in GPU context
121
+ # This prevents CUDA initialization issues in ZeroGPU environment
122
+ self._sbert_model = None # Will be loaded lazily
123
+ self._sbert_loading_attempted = False
124
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  ModelManager._enhanced_system_initialized = True
126
+ print("Enhanced recommendation system initialized (SBERT loading deferred)")
127
+
128
  except ImportError as e:
129
+ print(f"Enhanced modules not available: {str(e)}")
130
+ ModelManager._enhanced_system_initialized = True # Mark as attempted
131
  except Exception as e:
132
+ print(f"Enhanced system initialization failed: {str(e)}")
133
  print(traceback.format_exc())
134
+ ModelManager._enhanced_system_initialized = True # Mark as attempted
135
+
136
+ def _load_sbert_model_if_needed(self):
137
+ """Load SBERT model in GPU context if not already loaded"""
138
+ if self._sbert_model is not None or self._sbert_loading_attempted:
139
+ return self._sbert_model
140
+
141
+ try:
142
+ # Initialize SBERT model for semantic analysis
143
+ print("Loading SBERT model in GPU context...")
144
+ model_name = 'all-MiniLM-L6-v2'
145
+ fallback_models = ['all-mpnet-base-v2', 'all-MiniLM-L12-v2']
146
+
147
+ for model_name_attempt in [model_name] + fallback_models:
148
+ try:
149
+ self._sbert_model = SentenceTransformer(model_name_attempt, device='cuda' if torch.cuda.is_available() else 'cpu')
150
+ print(f"SBERT model {model_name_attempt} loaded successfully in GPU context")
151
+ return self._sbert_model
152
+ except Exception as e:
153
+ print(f"Failed to load SBERT model {model_name_attempt}: {str(e)}")
154
+ continue
155
+
156
+ print("All SBERT models failed to load, enhanced system will use keyword-only analysis")
157
+ return None
158
+
159
+ except Exception as e:
160
+ print(f"SBERT initialization failed: {str(e)}")
161
+ return None
162
+ finally:
163
+ self._sbert_loading_attempted = True
164
 
165
  @property
166
  def sbert_model(self):
167
  """Get SBERT model for semantic analysis"""
168
  if not ModelManager._enhanced_system_initialized:
169
  self._initialize_enhanced_system()
170
+ return self._load_sbert_model_if_needed()
171
 
172
  @property
173
  def config_manager(self):