Spaces:
Running
Running
Fix LoRA adapter compatibility issues and add robust fallbacks
Browse files- backend/models/character_manager.py +59 -32
- requirements.txt +4 -4
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
|
| 140 |
-
#
|
| 141 |
self.character_prompts = {
|
| 142 |
-
"moses": "You are Moses, the
|
| 143 |
-
|
| 144 |
-
"
|
|
|
|
|
|
|
| 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 |
-
#
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
except Exception as e:
|
| 192 |
-
logger.error(f"β
|
| 193 |
logger.error(f" Adapter path: {adapter_path}")
|
| 194 |
-
#
|
| 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.
|
| 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
|