File size: 2,964 Bytes
a5aa51c 739ccde 0698313 739ccde e818404 af813f1 e818404 e94a060 1e83603 e818404 739ccde e94a060 e818404 908a078 e94a060 7112235 739ccde a5aa51c 739ccde a5aa51c 0698313 e818404 a5aa51c e818404 821a209 a5aa51c 821a209 7112235 af813f1 | 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 69 70 71 72 73 74 75 | from transformers import AutoTokenizer, AutoModelForCausalLM, Trainer, TrainingArguments
import gradio as gr
import os
from datasets import load_dataset
# Authenticate using a token from environment variables
hf_token = os.getenv("HFTOKEN")
if not hf_token:
raise ValueError("Hugging Face token is missing. Please add it as an environment variable named 'HFTOKEN'.")
# Set up caching directory
cache_dir = "./model_cache"
os.makedirs(cache_dir, exist_ok=True)
# Load pre-trained model and tokenizer
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-hf", cache_dir=cache_dir, use_auth_token=hf_token)
model = AutoModelForCausalLM.from_pretrained(
"meta-llama/Llama-2-7b-hf",
device_map="auto", # Automatically map layers to available hardware (CPU here)
cache_dir=cache_dir,
use_auth_token=hf_token # Pass the token for authenticated model loading
)
# Load the dataset directly from Hugging Face
dataset = load_dataset("addcorpbc/personality-assesment")
# Fine-tuning configurations
training_args = TrainingArguments(
output_dir='./results', # output directory
num_train_epochs=3, # number of training epochs
per_device_train_batch_size=8, # batch size for training
per_device_eval_batch_size=8, # batch size for evaluation
warmup_steps=500, # number of warmup steps for learning rate scheduler
weight_decay=0.01, # strength of weight decay
logging_dir='./logs', # directory for storing logs
logging_steps=10,
save_steps=500,
evaluation_strategy="epoch", # evaluate after each epoch
)
trainer = Trainer(
model=model, # the pre-trained model
args=training_args, # training arguments
train_dataset=dataset["train"], # training dataset
eval_dataset=dataset.get("validation", None), # optional: validation dataset if available
)
# Fine-tune the model
trainer.train()
# Define the recommendation function
def recommend(user_input):
"""
Generate career recommendations based on user input using the fine-tuned LLaMA model.
"""
prompt = f"""
User input: {user_input}
Based on this input, provide personalized career recommendations.
"""
inputs = tokenizer(prompt, return_tensors="pt")
outputs = model.generate(**inputs, max_new_tokens=200)
return tokenizer.decode(outputs[0], skip_special_tokens=True)
# Define the Gradio interface
interface = gr.Interface(
fn=recommend,
inputs=gr.Textbox(label="Describe your career interests and goals", lines=2),
outputs=gr.Textbox(label="Career Recommendations", lines=4),
title="AI-Powered Magic City Career Counselor", # Updated title
description="Provide a description of your career goals, and get personalized career suggestions!" # Description update
)
# Launch the Gradio app
interface.launch(share=True) # share=True will make the app public
|