mployd-engineering commited on
Commit
e4a56be
·
verified ·
1 Parent(s): 9392df0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -12
app.py CHANGED
@@ -10,8 +10,9 @@ BASE_MODEL_ID = "meta-llama/Llama-3.1-8B-Instruct"
10
  # LoRA adapter: The CV/JD matching fine-tuned weights you requested.
11
  LORA_MODEL_ID = "LlamaFactoryAI/Llama-3.1-8B-Instruct-cv-job-description-matching"
12
 
13
- # Set API Key for external model calls. Leave empty for Canvas models.
14
- API_KEY = ""
 
15
 
16
  # --- Model and Tokenizer Initialization ---
17
  # Global variables to store the loaded components
@@ -22,7 +23,7 @@ def load_model():
22
  """
23
  Loads the base model, tokenizer, and the LoRA adapter.
24
  Includes the critical fix for the 'Invalid task type' error by correctly
25
- loading and modifying the PeftConfig object.
26
  """
27
  global model, tokenizer
28
 
@@ -32,36 +33,42 @@ def load_model():
32
  try:
33
  print(f"Loading base model: {BASE_MODEL_ID}...")
34
 
35
- # 1. Load the BASE model
36
  # Using bfloat16 for efficiency and device_map="auto" for memory management
37
  base_model = AutoModelForCausalLM.from_pretrained(
38
  BASE_MODEL_ID,
39
  torch_dtype=torch.bfloat16,
40
- device_map="auto"
 
41
  )
42
 
43
- # 2. Load the base model's tokenizer
44
- tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID)
 
 
 
45
 
46
  # Set padding token for batch inference
47
  if tokenizer.pad_token is None:
48
  tokenizer.pad_token = tokenizer.eos_token
49
 
50
- # 3. Load and fix the PeftConfig
51
  # CRITICAL FIX: The model's remote config file has an invalid 'text-generation'
52
  # task type. We must override it with the correct 'CAUSAL_LM' during loading
53
  # to prevent the ValueError.
54
  print("Loading and fixing PeftConfig with direct override...")
55
  peft_config = PeftConfig.from_pretrained(
56
  LORA_MODEL_ID,
57
- task_type="CAUSAL_LM" # <-- CRITICAL OVERRIDE
 
58
  )
59
 
60
- # 4. Load the LoRA adapter weights using the corrected config object
61
  model = PeftModel.from_pretrained(
62
  base_model,
63
  LORA_MODEL_ID,
64
- config=peft_config
 
65
  )
66
 
67
  # 5. Merge the LoRA weights into the base model for faster inference
@@ -72,6 +79,7 @@ def load_model():
72
 
73
  except Exception as e:
74
  print(f"FATAL ERROR: Failed to load model or adapter. Details: {e}")
 
75
  raise
76
 
77
 
@@ -224,7 +232,6 @@ with gr.Blocks(title="CV & Job Matcher (Llama 3.1 LoRA)") as demo:
224
  output_display = gr.JSON(
225
  label="Match Analysis Output (JSON)",
226
  value={"status": "Click 'Analyze Match' to generate the report."}
227
- # Removed 'lines=25' which caused the TypeError
228
  )
229
 
230
  # Example button to load the example inputs
 
10
  # LoRA adapter: The CV/JD matching fine-tuned weights you requested.
11
  LORA_MODEL_ID = "LlamaFactoryAI/Llama-3.1-8B-Instruct-cv-job-description-matching"
12
 
13
+ # Set the Hugging Face User Access Token here to authenticate and load gated models (like Llama 3.1).
14
+ # You MUST replace the empty string with your actual token.
15
+ HF_TOKEN = os.environ.get("HF_TOKEN")
16
 
17
  # --- Model and Tokenizer Initialization ---
18
  # Global variables to store the loaded components
 
23
  """
24
  Loads the base model, tokenizer, and the LoRA adapter.
25
  Includes the critical fix for the 'Invalid task type' error by correctly
26
+ loading and modifying the PeftConfig object, and adds authentication.
27
  """
28
  global model, tokenizer
29
 
 
33
  try:
34
  print(f"Loading base model: {BASE_MODEL_ID}...")
35
 
36
+ # 1. Load the BASE model (Authentication via 'token' argument is added)
37
  # Using bfloat16 for efficiency and device_map="auto" for memory management
38
  base_model = AutoModelForCausalLM.from_pretrained(
39
  BASE_MODEL_ID,
40
  torch_dtype=torch.bfloat16,
41
+ device_map="auto",
42
+ token=HF_TOKEN # Pass the token for authentication
43
  )
44
 
45
+ # 2. Load the base model's tokenizer (Authentication via 'token' argument is added)
46
+ tokenizer = AutoTokenizer.from_pretrained(
47
+ BASE_MODEL_ID,
48
+ token=HF_TOKEN # Pass the token for authentication
49
+ )
50
 
51
  # Set padding token for batch inference
52
  if tokenizer.pad_token is None:
53
  tokenizer.pad_token = tokenizer.eos_token
54
 
55
+ # 3. Load and fix the PeftConfig (Authentication via 'token' argument is added)
56
  # CRITICAL FIX: The model's remote config file has an invalid 'text-generation'
57
  # task type. We must override it with the correct 'CAUSAL_LM' during loading
58
  # to prevent the ValueError.
59
  print("Loading and fixing PeftConfig with direct override...")
60
  peft_config = PeftConfig.from_pretrained(
61
  LORA_MODEL_ID,
62
+ task_type="CAUSAL_LM", # <-- CRITICAL OVERRIDE
63
+ token=HF_TOKEN # Pass the token for authentication
64
  )
65
 
66
+ # 4. Load the LoRA adapter weights using the corrected config object (Authentication via 'token' argument is added)
67
  model = PeftModel.from_pretrained(
68
  base_model,
69
  LORA_MODEL_ID,
70
+ config=peft_config,
71
+ token=HF_TOKEN # Pass the token for authentication
72
  )
73
 
74
  # 5. Merge the LoRA weights into the base model for faster inference
 
79
 
80
  except Exception as e:
81
  print(f"FATAL ERROR: Failed to load model or adapter. Details: {e}")
82
+ # Re-raise the error to halt execution if loading failed
83
  raise
84
 
85
 
 
232
  output_display = gr.JSON(
233
  label="Match Analysis Output (JSON)",
234
  value={"status": "Click 'Analyze Match' to generate the report."}
 
235
  )
236
 
237
  # Example button to load the example inputs