Spaces:
Paused
Paused
A newer version of the Gradio SDK is available: 6.14.0
Fine-tuning Guide for Reply Generation Model
Overview
This guide explains how to fine-tune a language model to generate conversational replies based on conversation context, trigger, and move patterns.
Prerequisites
Dataset:
new_data_selected.csvwith columns:user_text: Male's messagepartner_text: Female's responsetrigger_*: Binary columns for triggersmove_*: Binary columns for moves
Hardware: GPU recommended (CUDA-compatible) for faster training
Dependencies: All required packages are in
requirements.txt
Step 1: Prepare Dataset
Place your dataset CSV file in the project root:
cp /path/to/new_data_selected.csv /Users/paul/Documents/paul/huggingface/lovebird25/
Step 2: Run Fine-tuning
Basic Usage
cd /Users/paul/Documents/paul/huggingface/lovebird25
python finetune_model.py --data_path new_data_selected.csv --output_dir ./finetuned_reply_model
Advanced Options
python finetune_model.py \
--data_path new_data_selected.csv \
--output_dir ./finetuned_reply_model \
--model_name vinai/PhoGPT-4B-Chat \
--num_epochs 3 \
--batch_size 2 \
--learning_rate 2e-4 \
--use_history
Parameters
--data_path: Path to training data CSV (default:new_data_selected.csv)--output_dir: Output directory for fine-tuned model (default:./finetuned_reply_model)--model_name: Base model name (default:vinai/PhoGPT-4B-Chat)--num_epochs: Number of training epochs (default: 3)--batch_size: Training batch size (default: 2, adjust based on GPU memory)--learning_rate: Learning rate (default: 2e-4)--use_history: Use conversation history in training
Step 3: Use Fine-tuned Model
After fine-tuning, update app.py or reply_service.py to use the fine-tuned model:
from finetuned_reply_service import get_finetuned_reply_service
# Get service
service = get_finetuned_reply_service(
base_model_name="vinai/PhoGPT-4B-Chat",
finetuned_model_path="./finetuned_reply_model"
)
# Generate reply
reply = service.generate_reply(
user_text="Tối nay anh có lịch đột xuất",
partner_text="Thế mai được không?",
trigger="invite_propose",
move="invite"
)
Step 4: Deploy to Hugging Face Spaces
Option 1: Upload Fine-tuned Model to Hugging Face Hub
- Login to Hugging Face:
huggingface-cli login
- Upload model:
cd finetuned_reply_model
huggingface-cli upload your-username/your-model-name .
- Update code to load from Hub:
service = get_finetuned_reply_service(
base_model_name="vinai/PhoGPT-4B-Chat",
finetuned_model_path="your-username/your-model-name" # Load from Hub
)
Option 2: Include Model in Space
Add model files to Space:
- Upload
finetuned_reply_model/folder to your Space - Or use Git LFS for large files
- Upload
Update
app.pyto use fine-tuned model:
from finetuned_reply_service import get_finetuned_reply_service
# In predict_reply function, replace:
# reply_service = get_reply_service()
# with:
finetuned_service = get_finetuned_reply_service()
suggestion = finetuned_service.generate_reply(
male=male,
female=female,
tone=top_tone,
intent=top_intent # Use as trigger/move
)
Training Tips
GPU Memory: If you run out of memory, reduce
--batch_sizeor use gradient checkpointingTraining Time:
- Small dataset (< 1000 examples): 1-2 epochs may be enough
- Large dataset (> 5000 examples): 3-5 epochs recommended
Learning Rate:
- Start with 2e-4
- If loss doesn't decrease, try 1e-4 or 5e-4
Validation: Monitor
eval_lossto avoid overfitting
Troubleshooting
Out of Memory Error
- Reduce
--batch_sizeto 1 - Reduce
max_lengthin tokenization (currently 512)
Model Not Found
- Ensure base model name is correct
- Check Hugging Face token is set:
export HF_TOKEN=your_token
Training Too Slow
- Use GPU if available
- Reduce dataset size for testing
- Use smaller base model
File Structure After Fine-tuning
lovebird25/
├── finetuned_reply_model/
│ ├── adapter_config.json
│ ├── adapter_model.bin
│ └── ...
├── finetune_model.py
├── finetuned_reply_service.py
└── ...
Next Steps
- Evaluate model on test set
- Fine-tune hyperparameters if needed
- Deploy to Hugging Face Spaces
- Integrate into production app