wangzerui commited on
Commit
7dc564b
·
1 Parent(s): 64c6795

Add application file

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
3
+ import gradio as gr
4
+ from peft import PeftModel
5
+
6
+ # Define the base model ID
7
+ base_model_id = "meta-llama/Llama-2-13b-hf"
8
+
9
+ # Create a BitsAndBytesConfig object with the corrected settings
10
+ quantization_config = BitsAndBytesConfig(
11
+ load_in_4bit=True,
12
+ bnb_4bit_use_double_quant=True,
13
+ bnb_4bit_quant_type="nf4",
14
+ bnb_4bit_compute_dtype=torch.bfloat16,
15
+ load_in_8bit_fp32_cpu_offload=True # Set as suggested in the error
16
+ )
17
+
18
+ # Load the base model with the updated quantization configuration
19
+ # Adjust 'device_map' based on your system's GPU configuration
20
+ base_model = AutoModelForCausalLM.from_pretrained(
21
+ base_model_id,
22
+ quantization_config=quantization_config,
23
+ trust_remote_code=True,
24
+ use_auth_token=True
25
+ )
26
+
27
+ # Load the tokenizer
28
+ tokenizer = AutoTokenizer.from_pretrained(base_model_id, add_bos_token=True, trust_remote_code=True)
29
+
30
+ # Load the fine-tuned model
31
+ ft_model = PeftModel.from_pretrained(base_model, "checkpoint-2800")
32
+
33
+ def formatting_func(job_description):
34
+ text = f"### The job description: {job_description}\n ### The skills: "
35
+ return text
36
+
37
+ def generate_skills(job_description):
38
+ formatted_text = formatting_func(job_description)
39
+ model_input = tokenizer(formatted_text, return_tensors="pt").to("cuda")
40
+
41
+ ft_model.eval()
42
+ with torch.no_grad():
43
+ output_tokens = ft_model.generate(**model_input, max_new_tokens=200)[0]
44
+ generated_text = tokenizer.decode(output_tokens, skip_special_tokens=True)
45
+
46
+ # Extract the text after "### The skills:" and before "### The qualifications:"
47
+ skills_start_index = generated_text.find("### The skills:") + len("### The skills:")
48
+ qualifications_start_index = generated_text.find("### The qualifications:")
49
+
50
+ if qualifications_start_index != -1:
51
+ skills_text = generated_text[skills_start_index:qualifications_start_index].strip()
52
+ else:
53
+ skills_text = generated_text[skills_start_index:].strip()
54
+
55
+ return skills_text
56
+
57
+ # Define the Gradio interface
58
+ inputs = gr.Textbox(lines=10, label="Job description:", placeholder="Enter or paste the job description here...")
59
+ outputs = gr.Textbox(label="Required skills:", placeholder="The required skills will be displayed here...")
60
+
61
+ gr.Interface(fn=generate_skills, inputs=inputs, outputs=outputs, title="Job Skills Analysis",
62
+ description="Paste the job description in the text box below and the model will show the required skills for candidates.").launch()