Spaces:
Runtime error

future / app.py
addcorpbc
Fixed dataset loading with Hugging Face dataset name
739ccde
Raw History Blame Contribute Delete
2.96 kB
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