File size: 2,534 Bytes
7dc564b
 
 
 
b40c9fc
7dc564b
 
 
 
 
 
 
 
 
d3b3e87
7dc564b
 
 
 
 
 
 
 
d3b3e87
7dc564b
 
 
 
 
 
 
 
 
 
 
 
b40c9fc
7dc564b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
import gradio as gr
from peft import PeftModel
import spaces

# Define the base model ID
base_model_id = "meta-llama/Llama-2-13b-hf"

# Create a BitsAndBytesConfig object with the corrected settings
quantization_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_use_double_quant=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype=torch.bfloat16
)

# Load the base model with the updated quantization configuration
# Adjust 'device_map' based on your system's GPU configuration
base_model = AutoModelForCausalLM.from_pretrained(
    base_model_id,  
    quantization_config=quantization_config,
    trust_remote_code=True,
    token=True  # Update this to use the token parameter
)

# Load the tokenizer
tokenizer = AutoTokenizer.from_pretrained(base_model_id, add_bos_token=True, trust_remote_code=True)

# Load the fine-tuned model
ft_model = PeftModel.from_pretrained(base_model, "checkpoint-2800")

def formatting_func(job_description):
    text = f"### The job description: {job_description}\n ### The skills: "
    return text

@spaces.GPU
def generate_skills(job_description):
    formatted_text = formatting_func(job_description)
    model_input = tokenizer(formatted_text, return_tensors="pt").to("cuda")

    ft_model.eval()
    with torch.no_grad():
        output_tokens = ft_model.generate(**model_input, max_new_tokens=200)[0]
        generated_text = tokenizer.decode(output_tokens, skip_special_tokens=True)
    
    # Extract the text after "### The skills:" and before "### The qualifications:"
    skills_start_index = generated_text.find("### The skills:") + len("### The skills:")
    qualifications_start_index = generated_text.find("### The qualifications:")
    
    if qualifications_start_index != -1:
        skills_text = generated_text[skills_start_index:qualifications_start_index].strip()
    else:
        skills_text = generated_text[skills_start_index:].strip()

    return skills_text

# Define the Gradio interface
inputs = gr.Textbox(lines=10, label="Job description:", placeholder="Enter or paste the job description here...")
outputs = gr.Textbox(label="Required skills:", placeholder="The required skills will be displayed here...")

gr.Interface(fn=generate_skills, inputs=inputs, outputs=outputs, title="Job Skills Analysis",
             description="Paste the job description in the text box below and the model will show the required skills for candidates.").launch()