ButterM40 commited on
Commit
114bef9
Β·
1 Parent(s): 44d5aa5

Fix LoRA adapter compatibility issues and add robust fallbacks

Browse files
backend/models/character_manager.py CHANGED
@@ -136,12 +136,14 @@ class CharacterManager:
136
  logger.info("Character manager initialized successfully")
137
 
138
  def _load_character_prompts(self):
139
- """Load character-specific system prompts - simplified for character-focused training"""
140
- # Minimal prompts since the new adapters are trained for direct character embodiment
141
  self.character_prompts = {
142
- "moses": "You are Moses, the biblical prophet and leader of the Israelites.",
143
- "samsung_employee": "You are an enthusiastic Samsung employee who loves technology.",
144
- "jinx": "You are Jinx from Arcane - chaotic, brilliant, and emotionally complex."
 
 
145
  }
146
 
147
  async def _load_character_adapter(self, character_id: str):
@@ -163,36 +165,61 @@ class CharacterManager:
163
  try:
164
  logger.info(f"Attempting to load LoRA adapter for {character_id}...")
165
 
166
- # Create a separate base model instance for this character to avoid conflicts
167
- # This is crucial to prevent the "multiple adapters" warning and character bleed
168
- character_base_model = AutoModelForCausalLM.from_pretrained(
169
- settings.BASE_MODEL,
170
- torch_dtype=torch.float16 if (settings.DEVICE == "cuda" and torch.cuda.is_available()) else torch.float32,
171
- device_map="auto" if (settings.DEVICE == "cuda" and torch.cuda.is_available()) else None,
172
- trust_remote_code=True,
173
- low_cpu_mem_usage=True,
174
- use_cache=True
175
- )
176
-
177
- # Load the LoRA adapter on the fresh model instance
178
- model_with_adapter = PeftModel.from_pretrained(
179
- character_base_model,
180
- adapter_path,
181
- adapter_name=character_id,
182
- is_trainable=False # Set to inference mode
183
- )
184
-
185
- # Ensure adapter is on correct device
186
- device = next(self.base_model.parameters()).device
187
- model_with_adapter = model_with_adapter.to(device)
188
-
189
- self.character_models[character_id] = model_with_adapter
190
- logger.info(f"βœ… Successfully loaded LoRA adapter for {character_id} with dedicated model instance")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
  except Exception as e:
192
- logger.error(f"❌ Could not load LoRA adapter for {character_id}: {e}")
193
  logger.error(f" Adapter path: {adapter_path}")
194
- # Fall back to base model with character prompt only
195
  self.character_models[character_id] = self.base_model
 
196
  else:
197
  missing_files = []
198
  if not os.path.exists(adapter_model_path):
 
136
  logger.info("Character manager initialized successfully")
137
 
138
  def _load_character_prompts(self):
139
+ """Load enhanced character-specific system prompts with fallback support"""
140
+ # Enhanced prompts to work even without LoRA adapters
141
  self.character_prompts = {
142
+ "moses": """You are Moses, the great prophet and lawgiver of Israel. Speak with divine wisdom, authority, and compassion. Use reverent biblical language, offer moral guidance, and show deep spiritual understanding. Always maintain the dignity and wisdom of the biblical Moses.""",
143
+
144
+ "samsung_employee": """You are a friendly, professional Samsung customer service representative and technology expert. Be enthusiastic about Samsung products, provide helpful technical assistance, and show excitement about Samsung innovations. Always represent Samsung positively and professionally.""",
145
+
146
+ "jinx": """You are Jinx from Arcane - the chaotic, brilliant, and unpredictable inventor from Zaun. Be energetic, playful, slightly unhinged, and creatively expressive. Show both genius and instability with colorful language and attitude. Always maintain Jinx's distinctive chaotic personality."""
147
  }
148
 
149
  async def _load_character_adapter(self, character_id: str):
 
165
  try:
166
  logger.info(f"Attempting to load LoRA adapter for {character_id}...")
167
 
168
+ # Try loading with compatibility fixes
169
+ try:
170
+ # First attempt: Load directly on base model (shared approach)
171
+ logger.info(f"Trying shared base model approach for {character_id}")
172
+ model_with_adapter = PeftModel.from_pretrained(
173
+ self.base_model,
174
+ adapter_path,
175
+ adapter_name=character_id,
176
+ is_trainable=False,
177
+ torch_dtype=torch.float32, # Force float32 for compatibility
178
+ )
179
+ self.character_models[character_id] = model_with_adapter
180
+ logger.info(f"βœ… Successfully loaded LoRA adapter for {character_id} (shared model)")
181
+
182
+ except Exception as e1:
183
+ logger.warning(f"Shared model approach failed for {character_id}: {e1}")
184
+
185
+ # Second attempt: Try with separate model instance and compatibility settings
186
+ try:
187
+ logger.info(f"Trying separate model instance for {character_id}")
188
+ character_base_model = AutoModelForCausalLM.from_pretrained(
189
+ settings.BASE_MODEL,
190
+ torch_dtype=torch.float32, # Force float32 for compatibility
191
+ device_map=None, # No device mapping for compatibility
192
+ trust_remote_code=True,
193
+ low_cpu_mem_usage=True,
194
+ use_cache=False # Disable cache for compatibility
195
+ )
196
+
197
+ # Load adapter with strict=False for compatibility
198
+ model_with_adapter = PeftModel.from_pretrained(
199
+ character_base_model,
200
+ adapter_path,
201
+ adapter_name=character_id,
202
+ is_trainable=False,
203
+ torch_dtype=torch.float32,
204
+ )
205
+
206
+ self.character_models[character_id] = model_with_adapter
207
+ logger.info(f"βœ… Successfully loaded LoRA adapter for {character_id} (separate model)")
208
+
209
+ except Exception as e2:
210
+ logger.warning(f"Separate model approach failed for {character_id}: {e2}")
211
+
212
+ # Final fallback: Use base model only with enhanced character prompts
213
+ logger.info(f"Using base model fallback for {character_id}")
214
+ self.character_models[character_id] = self.base_model
215
+ logger.info(f"⚠️ Using base model fallback for {character_id} - character behavior will rely on prompts only")
216
+
217
  except Exception as e:
218
+ logger.error(f"❌ Complete failure loading LoRA adapter for {character_id}: {e}")
219
  logger.error(f" Adapter path: {adapter_path}")
220
+ # Ultimate fallback to base model
221
  self.character_models[character_id] = self.base_model
222
+ logger.info(f"⚠️ Ultimate fallback: Using base model for {character_id}")
223
  else:
224
  missing_files = []
225
  if not os.path.exists(adapter_model_path):
requirements.txt CHANGED
@@ -1,7 +1,7 @@
1
- # Core ML Libraries
2
- torch>=2.0.0
3
- transformers>=4.36.0
4
- peft>=0.13.2,<0.18.0
5
  accelerate>=0.24.0
6
  datasets>=2.14.0
7
  huggingface-hub>=0.19.0
 
1
+ # Core ML Libraries - Fixed versions for compatibility
2
+ torch>=2.0.0,<2.5.0
3
+ transformers>=4.36.0,<4.50.0
4
+ peft>=0.8.0,<0.14.0
5
  accelerate>=0.24.0
6
  datasets>=2.14.0
7
  huggingface-hub>=0.19.0