wangzerui's picture
token
3a4d3f2
raw
history blame
3.21 kB
import os # Ensure os is imported
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
import gradio as gr
from peft import PeftModel
import spaces # Ensure spaces is imported
# 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
)
# 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'.")
# Define cache directory
cache_dir = "./cache"
# Load the base model with the updated quantization configuration
base_model = AutoModelForCausalLM.from_pretrained(
base_model_id,
quantization_config=quantization_config,
trust_remote_code=True,
token=huggingface_token, # Use the token parameter
cache_dir=cache_dir # Specify cache directory
).to("cuda") # Move model to CUDA
# Load the tokenizer
tokenizer = AutoTokenizer.from_pretrained(
base_model_id,
add_bos_token=True,
trust_remote_code=True,
token=huggingface_token,
cache_dir=cache_dir # Specify cache directory
)
# Load the fine-tuned model
ft_model = PeftModel.from_pretrained(base_model, "checkpoint-2800", cache_dir=cache_dir).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") # Use CUDA for GPU support
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(share=True) # Set share=True to create a public link