File size: 2,715 Bytes
f2040a9
7dc564b
cd77bd3
7dc564b
 
9357bc1
7dc564b
 
 
 
f2b26e1
 
 
 
 
cd77bd3
7dc564b
 
 
9357bc1
b7235ce
7dc564b
 
3a4d3f2
 
 
 
9357bc1
3a4d3f2
7dc564b
b7235ce
 
7dc564b
 
 
 
 
82bed23
7dc564b
 
f2040a9
7dc564b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9357bc1
 
 
7dc564b
 
 
 
 
 
 
9357bc1
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
65
66
67
68
import os
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
import gradio as gr
from peft import PeftModel
import spaces

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

# Ensure you have the Hugging Face token set as an environment variable
huggingface_token = os.getenv('HUGGINGFACE_TOKEN')
if not huggingface_token:
    raise Exception("Hugging Face token not found. Please set it as an environment variable 'HUGGINGFACE_TOKEN'.")

# Load the base model without quantization configuration
base_model = AutoModelForCausalLM.from_pretrained(
    base_model_id,  
    trust_remote_code=True,
    use_auth_token=huggingface_token  # Use the correct parameter
).to("cuda")  # Move model to CUDA

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

# Load the fine-tuned model and move to CUDA
ft_model = PeftModel.from_pretrained(base_model, "checkpoint-2800").to("cuda")  # Move model to CUDA

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

@spaces.GPU  # Decorate the function to ensure it uses GPU
def generate_skills(job_description):
    formatted_text = formatting_func(job_description)
    model_input = tokenizer(formatted_text, return_tensors="pt").to("cuda")  # Ensure input is on 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()

    # Clear CUDA memory
    torch.cuda.empty_cache()

    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(share=True)