wangzerui's picture
token
d3b3e87
raw
history blame
2.53 kB
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()