mployd-engineering commited on
Commit
f5f1423
·
verified ·
1 Parent(s): 692d002

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -30
app.py CHANGED
@@ -2,12 +2,14 @@ import gradio as gr
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.
9
- BASE_MODEL_ID = "akjindal53244/Llama-3.1-Storm-8B"
10
- LORA_MODEL_ID = "LlamaFactoryAI/cv-job-description-matching"
 
 
11
  # Set API Key for external model calls. Leave empty for Canvas models.
12
  API_KEY = ""
13
 
@@ -45,18 +47,16 @@ def load_model():
45
  if tokenizer.pad_token is None:
46
  tokenizer.pad_token = tokenizer.eos_token
47
 
48
- # 3. Load and fix the PeftConfig (FINAL FIX)
49
- # We pass the desired task_type directly as a keyword argument to PeftConfig.from_pretrained.
50
- # This overrides the invalid 'text-generation' value in the remote config file,
51
- # ensuring the configuration object is created successfully before validation fails.
52
  print("Loading and fixing PeftConfig with direct override...")
53
  peft_config = PeftConfig.from_pretrained(
54
  LORA_MODEL_ID,
55
- task_type="CAUSAL_LM" # <-- CRITICAL OVERRIDE HERE
56
  )
57
 
58
- # We no longer need the explicit line: peft_config.task_type = "CAUSAL_LM"
59
-
60
  # 4. Load the LoRA adapter weights using the corrected config object
61
  model = PeftModel.from_pretrained(
62
  base_model,
@@ -74,22 +74,12 @@ def load_model():
74
  print(f"FATAL ERROR: Failed to load model or adapter. Details: {e}")
75
  raise
76
 
77
- # Ensure the model is loaded when the script starts
78
- load_model()
79
-
80
 
81
  # --- Core Inference Function ---
82
 
83
  def match_cv_jd(cv_text: str, job_description: str) -> str:
84
  """
85
  Generates a structured match analysis between a CV and a Job Description.
86
-
87
- Args:
88
- cv_text: The content of the candidate's CV.
89
- job_description: The content of the job description.
90
-
91
- Returns:
92
- A formatted JSON string containing the matching analysis, score, and recommendation.
93
  """
94
  # Quick check for loaded model/tokenizer
95
  if model is None or tokenizer is None:
@@ -203,7 +193,7 @@ EXAMPLE_JD = """
203
  """
204
 
205
  # Define the Gradio Interface
206
- with gr.Blocks(title="CV & Job Matcher (Llama 3 LoRA)") as demo:
207
  gr.Markdown(
208
  """
209
  # 🤖 CV & Job Description Matcher (Llama 3.1 LoRA)
@@ -246,11 +236,4 @@ with gr.Blocks(title="CV & Job Matcher (Llama 3 LoRA)") as demo:
246
 
247
  # Link the function to the button click event
248
  match_button.click(
249
- fn=match_cv_jd,
250
- inputs=[cv_input, jd_input],
251
- outputs=output_display
252
- )
253
-
254
- # Launch the Gradio app
255
- if __name__ == "__main__":
256
- demo.launch()
 
2
  import torch
3
  import json
4
  from transformers import AutoModelForCausalLM, AutoTokenizer
5
+ from peft import PeftModel, PeftConfig
6
 
7
  # --- Configuration ---
8
+ # Base model: The instruction-tuned version of Llama 3.1 8B.
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
 
 
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,
 
74
  print(f"FATAL ERROR: Failed to load model or adapter. Details: {e}")
75
  raise
76
 
 
 
 
77
 
78
  # --- Core Inference Function ---
79
 
80
  def match_cv_jd(cv_text: str, job_description: str) -> str:
81
  """
82
  Generates a structured match analysis between a CV and a Job Description.
 
 
 
 
 
 
 
83
  """
84
  # Quick check for loaded model/tokenizer
85
  if model is None or tokenizer is None:
 
193
  """
194
 
195
  # Define the Gradio Interface
196
+ with gr.Blocks(title="CV & Job Matcher (Llama 3.1 LoRA)") as demo:
197
  gr.Markdown(
198
  """
199
  # 🤖 CV & Job Description Matcher (Llama 3.1 LoRA)
 
236
 
237
  # Link the function to the button click event
238
  match_button.click(
239
+ fn=match