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)