mployd-engineering commited on
Commit
dbbd706
·
verified ·
1 Parent(s): d65dd41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -28
app.py CHANGED
@@ -8,7 +8,6 @@ import os
8
  # --- Configuration ---
9
  BASE_MODEL_ID = "meta-llama/Llama-3.1-8B-Instruct"
10
  LORA_MODEL_ID = "LlamaFactoryAI/Llama-3.1-8B-Instruct-cv-job-description-matching"
11
-
12
  HF_TOKEN = os.environ.get("HF_TOKEN")
13
 
14
  # --- Model and Tokenizer ---
@@ -21,15 +20,15 @@ def load_model():
21
  return
22
 
23
  print("Loading base model...")
 
24
  base_model = AutoModelForCausalLM.from_pretrained(
25
  BASE_MODEL_ID,
26
  torch_dtype=torch.bfloat16,
27
  device_map="auto",
28
  token=HF_TOKEN
29
  )
30
-
31
  tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID, token=HF_TOKEN)
32
-
33
  if tokenizer.pad_token is None:
34
  tokenizer.pad_token = tokenizer.eos_token
35
 
@@ -39,43 +38,39 @@ def load_model():
39
  task_type="CAUSAL_LM",
40
  token=HF_TOKEN
41
  )
42
-
43
  model_with_lora = PeftModel.from_pretrained(
44
  base_model,
45
  LORA_MODEL_ID,
46
  config=peft_config,
47
  token=HF_TOKEN
48
  )
49
-
50
  model = model_with_lora.merge_and_unload()
51
  model.eval()
52
  print("Model fully loaded!")
53
 
54
  # --- Core Inference ---
55
  def match_cv_jd(cv_text: str, job_description: str) -> dict:
 
56
  if model is None or tokenizer is None:
57
- return {"error": "Model not loaded yet."}
58
-
59
- system_prompt = """
60
- You are a world-class CV and Job Description matching AI.
61
- Output a structured JSON with fields:
62
  - matching_analysis
63
  - description
64
  - score (0-100)
65
  - recommendation (2 concrete steps)
66
- Output MUST be valid JSON.
67
- """
68
 
69
- user_prompt = f"""
70
- CV:
71
  ---
72
  {cv_text}
73
  ---
74
  Job Description:
75
  ---
76
  {job_description}
77
- ---
78
- """
79
 
80
  messages = [
81
  {"role": "system", "content": system_prompt},
@@ -112,35 +107,35 @@ Job Description:
112
  return {"raw": response_text}
113
 
114
  # --- Example Data ---
115
- EXAMPLE_CV = """
116
- **John Doe**
117
  Email: john.doe@example.com
118
  **Summary**
119
- Experienced software engineer with 5 years in Python and backend development.
120
- """
121
- EXAMPLE_JD = """
122
- **Job Title: Senior Backend Engineer**
123
- Responsibilities: Develop scalable backend services using Python.
124
- """
125
 
126
  # --- Gradio Interface ---
127
  with gr.Blocks(title="CV & Job Matcher") as demo:
128
  gr.Markdown("# 🤖 CV & Job Description Matcher")
129
-
130
  with gr.Row():
131
  cv_input = gr.Textbox(label="CV", lines=15, value=EXAMPLE_CV)
132
  jd_input = gr.Textbox(label="Job Description", lines=15, value=EXAMPLE_JD)
133
-
134
  analyze_btn = gr.Button("Analyze Match")
135
  output_display = gr.JSON(label="Match Output")
136
 
 
137
  analyze_btn.click(
138
  fn=match_cv_jd,
139
  inputs=[cv_input, jd_input],
140
- outputs=output_display
 
141
  )
142
 
143
  # --- Launch ---
144
  if __name__ == "__main__":
145
  load_model()
146
- demo.launch(api_mode=True)
 
 
8
  # --- Configuration ---
9
  BASE_MODEL_ID = "meta-llama/Llama-3.1-8B-Instruct"
10
  LORA_MODEL_ID = "LlamaFactoryAI/Llama-3.1-8B-Instruct-cv-job-description-matching"
 
11
  HF_TOKEN = os.environ.get("HF_TOKEN")
12
 
13
  # --- Model and Tokenizer ---
 
20
  return
21
 
22
  print("Loading base model...")
23
+ # Load base model
24
  base_model = AutoModelForCausalLM.from_pretrained(
25
  BASE_MODEL_ID,
26
  torch_dtype=torch.bfloat16,
27
  device_map="auto",
28
  token=HF_TOKEN
29
  )
30
+
31
  tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID, token=HF_TOKEN)
 
32
  if tokenizer.pad_token is None:
33
  tokenizer.pad_token = tokenizer.eos_token
34
 
 
38
  task_type="CAUSAL_LM",
39
  token=HF_TOKEN
40
  )
41
+
42
  model_with_lora = PeftModel.from_pretrained(
43
  base_model,
44
  LORA_MODEL_ID,
45
  config=peft_config,
46
  token=HF_TOKEN
47
  )
48
+
49
  model = model_with_lora.merge_and_unload()
50
  model.eval()
51
  print("Model fully loaded!")
52
 
53
  # --- Core Inference ---
54
  def match_cv_jd(cv_text: str, job_description: str) -> dict:
55
+ # Ensure model is loaded if running in a context where global init failed
56
  if model is None or tokenizer is None:
57
+ load_model()
58
+
59
+ system_prompt = """You are a world-class CV and Job Description matching AI. Output a structured JSON with fields:
 
 
60
  - matching_analysis
61
  - description
62
  - score (0-100)
63
  - recommendation (2 concrete steps)
64
+ Output MUST be valid JSON."""
 
65
 
66
+ user_prompt = f"""CV:
 
67
  ---
68
  {cv_text}
69
  ---
70
  Job Description:
71
  ---
72
  {job_description}
73
+ ---"""
 
74
 
75
  messages = [
76
  {"role": "system", "content": system_prompt},
 
107
  return {"raw": response_text}
108
 
109
  # --- Example Data ---
110
+ EXAMPLE_CV = """**John Doe**
 
111
  Email: john.doe@example.com
112
  **Summary**
113
+ Experienced software engineer with 5 years in Python and backend development."""
114
+
115
+ EXAMPLE_JD = """**Job Title: Senior Backend Engineer**
116
+ Responsibilities: Develop scalable backend services using Python."""
 
 
117
 
118
  # --- Gradio Interface ---
119
  with gr.Blocks(title="CV & Job Matcher") as demo:
120
  gr.Markdown("# 🤖 CV & Job Description Matcher")
121
+
122
  with gr.Row():
123
  cv_input = gr.Textbox(label="CV", lines=15, value=EXAMPLE_CV)
124
  jd_input = gr.Textbox(label="Job Description", lines=15, value=EXAMPLE_JD)
125
+
126
  analyze_btn = gr.Button("Analyze Match")
127
  output_display = gr.JSON(label="Match Output")
128
 
129
+ # The api_name argument exposes this function as a named endpoint
130
  analyze_btn.click(
131
  fn=match_cv_jd,
132
  inputs=[cv_input, jd_input],
133
+ outputs=output_display,
134
+ api_name="predict"
135
  )
136
 
137
  # --- Launch ---
138
  if __name__ == "__main__":
139
  load_model()
140
+ # Removed api_mode=True, unnecessary in modern Gradio
141
+ demo.launch()