mployd-engineering commited on
Commit
2aa01b2
·
verified ·
1 Parent(s): 392cef7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -9
app.py CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
2
  import torch
3
  import json
4
  from transformers import AutoModelForCausalLM, AutoTokenizer
5
- from peft import PeftModel
6
 
7
  # --- Configuration ---
8
  # Set the model IDs. Replace these with your actual model paths if different.
@@ -19,7 +19,8 @@ tokenizer = None
19
  def load_model():
20
  """
21
  Loads the base model, tokenizer, and the LoRA adapter.
22
- Includes the critical fix for the 'Invalid task type' error.
 
23
  """
24
  global model, tokenizer
25
 
@@ -44,19 +45,22 @@ def load_model():
44
  if tokenizer.pad_token is None:
45
  tokenizer.pad_token = tokenizer.eos_token
46
 
47
- # 3. Define the override configuration dictionary
 
 
 
 
 
48
  # CRITICAL FIX: The pre-trained LoRA adapter config sometimes incorrectly defaults
49
  # to 'text-generation', which is disallowed by newer PEFT versions.
50
- # We explicitly set it to 'CAUSAL_LM' (Causal Language Modeling) for Llama models.
51
- peft_config_override = {
52
- "task_type": "CAUSAL_LM"
53
- }
54
 
55
- # 4. Load the LoRA adapter weights using the corrected config
 
56
  model = PeftModel.from_pretrained(
57
  base_model,
58
  LORA_MODEL_ID,
59
- config=peft_config_override # Pass the dictionary to override the loaded config
60
  )
61
 
62
  # 5. Merge the LoRA weights into the base model for faster inference
 
2
  import torch
3
  import json
4
  from transformers import AutoModelForCausalLM, AutoTokenizer
5
+ from peft import PeftModel, PeftConfig # Make sure PeftConfig is available
6
 
7
  # --- Configuration ---
8
  # Set the model IDs. Replace these with your actual model paths if different.
 
19
  def load_model():
20
  """
21
  Loads the base model, tokenizer, and the LoRA adapter.
22
+ Includes the critical fix for the 'Invalid task type' error by correctly
23
+ loading and modifying the PeftConfig object.
24
  """
25
  global model, tokenizer
26
 
 
45
  if tokenizer.pad_token is None:
46
  tokenizer.pad_token = tokenizer.eos_token
47
 
48
+ # 3. Load and fix the PeftConfig (NEW FIX)
49
+ # We must load the config object first, modify the incompatible field,
50
+ # and then pass the corrected object to PeftModel.from_pretrained.
51
+ print("Loading and fixing PeftConfig...")
52
+ peft_config = PeftConfig.from_pretrained(LORA_MODEL_ID)
53
+
54
  # CRITICAL FIX: The pre-trained LoRA adapter config sometimes incorrectly defaults
55
  # to 'text-generation', which is disallowed by newer PEFT versions.
56
+ peft_config.task_type = "CAUSAL_LM"
 
 
 
57
 
58
+ # 4. Load the LoRA adapter weights using the corrected config object
59
+ # The 'config' argument now correctly receives a PeftConfig object.
60
  model = PeftModel.from_pretrained(
61
  base_model,
62
  LORA_MODEL_ID,
63
+ config=peft_config
64
  )
65
 
66
  # 5. Merge the LoRA weights into the base model for faster inference