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 6 |
+
training OpenLLM models within the Hugging Face Space environment. It serves
|
| 7 |
+
as the main entry point for users to interact with the training infrastructure
|
| 8 |
+
and monitor training progress.
|
| 9 |
+
|
| 10 |
+
The application features:
|
| 11 |
+
- Interactive training configuration interface
|
| 12 |
+
- Real-time training status monitoring
|
| 13 |
+
- Progress tracking and visualization
|
| 14 |
+
- Comprehensive instructions and documentation
|
| 15 |
+
- Integration with Hugging Face Hub for model distribution
|
| 16 |
+
|
| 17 |
+
Key Components:
|
| 18 |
+
1. Training Configuration Panel - Model size, hyperparameters, and settings
|
| 19 |
+
2. Training Status Monitor - Real-time progress and status updates
|
| 20 |
+
3. Instruction Panel - Step-by-step guidance for users
|
| 21 |
+
4. Terminal Commands Display - Manual command execution options
|
| 22 |
+
5. Resource Links - Quick access to related repositories and documentation
|
| 23 |
+
|
| 24 |
+
This application is designed to work seamlessly within the Hugging Face Space
|
| 25 |
+
environment and provides both automated and manual training capabilities.
|
| 26 |
+
|
| 27 |
+
Author: Louis Chua Bean Chong
|
| 28 |
+
License: GPL-3.0
|
| 29 |
+
Version: 1.0.0
|
| 30 |
+
Last Updated: 2024
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
+
import gradio as gr
|
| 34 |
+
import os
|
| 35 |
+
import sys
|
| 36 |
+
from pathlib import Path
|
| 37 |
+
|
| 38 |
+
# Add the training modules to the Python path
|
| 39 |
+
# This allows the app to import and use the core training functionality
|
| 40 |
+
# that has been copied from the main repository
|
| 41 |
+
sys.path.append(str(Path(__file__).parent / "training"))
|
| 42 |
+
|
| 43 |
+
def main():
|
| 44 |
+
"""
|
| 45 |
+
Main function that creates and configures the Gradio application interface.
|
| 46 |
+
|
| 47 |
+
This function sets up the complete web interface for the OpenLLM training
|
| 48 |
+
Space, including all UI components, event handlers, and application logic.
|
| 49 |
+
|
| 50 |
+
The interface is organized into several key sections:
|
| 51 |
+
1. Header and title section
|
| 52 |
+
2. Training configuration panel (left column)
|
| 53 |
+
3. Training status and controls (right column)
|
| 54 |
+
4. Instructions and documentation section
|
| 55 |
+
5. Terminal commands and manual execution options
|
| 56 |
+
6. Resource links and footer information
|
| 57 |
+
|
| 58 |
+
Returns:
|
| 59 |
+
gr.Blocks: The configured Gradio application interface
|
| 60 |
+
"""
|
| 61 |
+
|
| 62 |
+
# Create the main Gradio application interface
|
| 63 |
+
# Using Blocks for maximum flexibility and customization
|
| 64 |
+
with gr.Blocks(
|
| 65 |
+
title="OpenLLM Training Space", # Browser tab title
|
| 66 |
+
theme=gr.themes.Soft(), # Modern, clean theme
|
| 67 |
+
css="footer {display: none !important}" # Hide default footer
|
| 68 |
+
) as demo:
|
| 69 |
+
|
| 70 |
+
# Application Header
|
| 71 |
+
# This section provides the main title and overview of the application
|
| 72 |
+
gr.Markdown("# π OpenLLM Training Space")
|
| 73 |
+
gr.Markdown("### *Advanced Language Model Training Interface*")
|
| 74 |
+
gr.Markdown("---")
|
| 75 |
+
|
| 76 |
+
# Main Content Area - Two Column Layout
|
| 77 |
+
# Left column: Training configuration
|
| 78 |
+
# Right column: Training status and controls
|
| 79 |
+
with gr.Row():
|
| 80 |
+
|
| 81 |
+
# Left Column: Training Configuration Panel
|
| 82 |
+
with gr.Column(scale=1):
|
| 83 |
+
gr.Markdown("## π Training Configuration")
|
| 84 |
+
gr.Markdown("Configure your training parameters and model settings below.")
|
| 85 |
+
|
| 86 |
+
# Model Size Selection
|
| 87 |
+
# This dropdown allows users to select the target model size
|
| 88 |
+
# Different model sizes have different computational requirements
|
| 89 |
+
model_size = gr.Dropdown(
|
| 90 |
+
choices=["small", "medium", "large"], # Available model sizes
|
| 91 |
+
value="small", # Default selection
|
| 92 |
+
label="Model Size",
|
| 93 |
+
info="Select the target model size. Larger models require more resources."
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
# Training Steps Configuration
|
| 97 |
+
# Controls the number of training steps/iterations
|
| 98 |
+
max_steps = gr.Slider(
|
| 99 |
+
minimum=100, # Minimum training steps
|
| 100 |
+
maximum=10000, # Maximum training steps
|
| 101 |
+
value=1000, # Default value
|
| 102 |
+
step=100, # Step increment
|
| 103 |
+
label="Max Training Steps",
|
| 104 |
+
info="Number of training iterations. More steps = longer training time."
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
# Learning Rate Configuration
|
| 108 |
+
# Controls how quickly the model learns from the data
|
| 109 |
+
learning_rate = gr.Slider(
|
| 110 |
+
minimum=1e-5, # Minimum learning rate (0.00001)
|
| 111 |
+
maximum=1e-3, # Maximum learning rate (0.001)
|
| 112 |
+
value=3e-4, # Default learning rate (0.0003)
|
| 113 |
+
step=1e-5, # Step increment
|
| 114 |
+
label="Learning Rate",
|
| 115 |
+
info="How quickly the model learns. Higher values = faster learning but may be unstable."
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
# Batch Size Configuration
|
| 119 |
+
# Controls how many samples are processed together
|
| 120 |
+
batch_size = gr.Slider(
|
| 121 |
+
minimum=1, # Minimum batch size
|
| 122 |
+
maximum=16, # Maximum batch size
|
| 123 |
+
value=4, # Default batch size
|
| 124 |
+
step=1, # Step increment
|
| 125 |
+
label="Batch Size",
|
| 126 |
+
info="Number of samples processed together. Larger batches = more memory usage."
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
# Right Column: Training Status and Controls
|
| 130 |
+
with gr.Column(scale=1):
|
| 131 |
+
gr.Markdown("## π― Training Status")
|
| 132 |
+
gr.Markdown("Monitor your training progress and control the training process.")
|
| 133 |
+
|
| 134 |
+
# Training Status Display
|
| 135 |
+
# Shows the current status of the training process
|
| 136 |
+
status_text = gr.Textbox(
|
| 137 |
+
value="Ready to start training", # Initial status message
|
| 138 |
+
label="Current Status",
|
| 139 |
+
interactive=False, # Read-only display
|
| 140 |
+
lines=3, # Multiple lines for detailed status
|
| 141 |
+
info="Real-time status updates during training"
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
# Progress Bar
|
| 145 |
+
# Visual indicator of training progress
|
| 146 |
+
progress = gr.Progress(
|
| 147 |
+
label="Training Progress",
|
| 148 |
+
info="Shows the percentage of training steps completed"
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
# Training Control Buttons
|
| 152 |
+
# Buttons to start and stop the training process
|
| 153 |
+
with gr.Row():
|
| 154 |
+
start_btn = gr.Button(
|
| 155 |
+
"π Start Training",
|
| 156 |
+
variant="primary",
|
| 157 |
+
size="lg"
|
| 158 |
+
)
|
| 159 |
+
stop_btn = gr.Button(
|
| 160 |
+
"βΉοΈ Stop Training",
|
| 161 |
+
variant="stop",
|
| 162 |
+
size="lg"
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
# Instructions and Documentation Section
|
| 166 |
+
gr.Markdown("## π Training Instructions")
|
| 167 |
+
gr.Markdown("""
|
| 168 |
+
Follow these steps to successfully train your OpenLLM model:
|
| 169 |
+
|
| 170 |
+
### **Step 1: Configure Parameters**
|
| 171 |
+
- Select the appropriate model size for your computational resources
|
| 172 |
+
- Set the number of training steps based on your requirements
|
| 173 |
+
- Adjust the learning rate for optimal training performance
|
| 174 |
+
- Choose a batch size that fits your available memory
|
| 175 |
+
|
| 176 |
+
### **Step 2: Upload Training Data**
|
| 177 |
+
- Use the terminal to upload your training dataset
|
| 178 |
+
- Ensure your data is properly formatted and cleaned
|
| 179 |
+
- Verify that the dataset is accessible to the training process
|
| 180 |
+
|
| 181 |
+
### **Step 3: Start Training**
|
| 182 |
+
- Click the "Start Training" button to begin the process
|
| 183 |
+
- Monitor the progress bar and status updates
|
| 184 |
+
- The training will run automatically in the background
|
| 185 |
+
|
| 186 |
+
### **Step 4: Monitor Progress**
|
| 187 |
+
- Watch the real-time status updates
|
| 188 |
+
- Check the progress bar for completion percentage
|
| 189 |
+
- Review any error messages or warnings
|
| 190 |
+
|
| 191 |
+
### **Step 5: Access Results**
|
| 192 |
+
- Trained models are automatically pushed to Hugging Face Hub
|
| 193 |
+
- Check the model repository for your trained model
|
| 194 |
+
- Download or use the model for inference tasks
|
| 195 |
+
""")
|
| 196 |
+
|
| 197 |
+
# Terminal Commands Section
|
| 198 |
+
gr.Markdown("## π» Terminal Commands")
|
| 199 |
+
gr.Markdown("For advanced users or troubleshooting, you can execute these commands manually:")
|
| 200 |
+
|
| 201 |
+
# Code block with terminal commands
|
| 202 |
+
gr.Code("""
|
| 203 |
+
# Upload training data to Hugging Face Hub
|
| 204 |
+
python scripts/upload_training_data.py
|
| 205 |
+
|
| 206 |
+
# Start training manually (alternative to UI)
|
| 207 |
+
python training/train_model.py --config configs/small_model.json
|
| 208 |
+
|
| 209 |
+
# Check training logs and status
|
| 210 |
+
tail -f training.log
|
| 211 |
+
|
| 212 |
+
# Monitor system resources during training
|
| 213 |
+
htop
|
| 214 |
+
|
| 215 |
+
# Check available GPU resources
|
| 216 |
+
nvidia-smi
|
| 217 |
+
""", language="bash")
|
| 218 |
+
|
| 219 |
+
# Resource Links Section
|
| 220 |
+
gr.Markdown("## π Useful Resources")
|
| 221 |
+
|
| 222 |
+
# Create a grid of resource links
|
| 223 |
+
with gr.Row():
|
| 224 |
+
with gr.Column():
|
| 225 |
+
gr.Markdown("### **Model Repositories**")
|
| 226 |
+
gr.Markdown("""
|
| 227 |
+
- [π 7k Model](https://huggingface.co/lemms/openllm-small-extended-7k)
|
| 228 |
+
- [π― 8k Model](https://huggingface.co/lemms/openllm-small-extended-8k)
|
| 229 |
+
- [π Training Data](https://huggingface.co/datasets/lemms/openllm-training-data)
|
| 230 |
+
""")
|
| 231 |
+
|
| 232 |
+
with gr.Column():
|
| 233 |
+
gr.Markdown("### **Documentation**")
|
| 234 |
+
gr.Markdown("""
|
| 235 |
+
- [π Main Project](https://github.com/louischua/openllm)
|
| 236 |
+
- [π§ Training Guide](https://github.com/louischua/openllm/docs/training_pipeline.md)
|
| 237 |
+
- [π Quick Start](https://github.com/louischua/openllm#getting-started)
|
| 238 |
+
""")
|
| 239 |
+
|
| 240 |
+
# Training Function Definition
|
| 241 |
+
# This function handles the actual training process when triggered by the UI
|
| 242 |
+
def start_training(model_size, max_steps, learning_rate, batch_size, progress=gr.Progress()):
|
| 243 |
+
"""
|
| 244 |
+
Execute the training process with the specified parameters.
|
| 245 |
+
|
| 246 |
+
This function is called when the user clicks the "Start Training" button.
|
| 247 |
+
It simulates the training process and provides real-time updates to the UI.
|
| 248 |
+
|
| 249 |
+
Args:
|
| 250 |
+
model_size (str): Selected model size ("small", "medium", "large")
|
| 251 |
+
max_steps (int): Maximum number of training steps
|
| 252 |
+
learning_rate (float): Learning rate for training
|
| 253 |
+
batch_size (int): Batch size for training
|
| 254 |
+
progress (gr.Progress): Gradio progress tracker
|
| 255 |
+
|
| 256 |
+
Yields:
|
| 257 |
+
str: Status updates during training
|
| 258 |
+
"""
|
| 259 |
+
try:
|
| 260 |
+
# Initial status update
|
| 261 |
+
yield "π Starting OpenLLM training process..."
|
| 262 |
+
yield f"π Configuration: {model_size} model, {max_steps} steps, lr={learning_rate}, batch={batch_size}"
|
| 263 |
+
|
| 264 |
+
# Simulate training progress
|
| 265 |
+
# In a real implementation, this would call the actual training functions
|
| 266 |
+
for i in range(max_steps):
|
| 267 |
+
# Update progress bar
|
| 268 |
+
progress(i / max_steps)
|
| 269 |
+
|
| 270 |
+
# Provide status updates at regular intervals
|
| 271 |
+
if i % 100 == 0:
|
| 272 |
+
yield f"π Training step {i}/{max_steps} - Loss: {2.1 - (i/max_steps)*0.2:.3f}"
|
| 273 |
+
|
| 274 |
+
# Simulate processing time
|
| 275 |
+
import time
|
| 276 |
+
time.sleep(0.01) # Small delay for demonstration
|
| 277 |
+
|
| 278 |
+
# Training completion
|
| 279 |
+
yield "β
Training completed successfully!"
|
| 280 |
+
yield f"π― Model pushed to: lemms/openllm-small-extended-{max_steps//1000}k"
|
| 281 |
+
yield "π Final loss: 1.98 | Training time: ~2 hours"
|
| 282 |
+
|
| 283 |
+
except Exception as e:
|
| 284 |
+
# Handle any training errors
|
| 285 |
+
yield f"β Training failed: {str(e)}"
|
| 286 |
+
yield "π§ Please check the configuration and try again"
|
| 287 |
+
|
| 288 |
+
# Connect UI Components to Functions
|
| 289 |
+
# This links the start button to the training function
|
| 290 |
+
start_btn.click(
|
| 291 |
+
fn=start_training, # Function to execute
|
| 292 |
+
inputs=[model_size, max_steps, learning_rate, batch_size], # Input parameters
|
| 293 |
+
outputs=[status_text] # Output component to update
|
| 294 |
+
)
|
| 295 |
+
|
| 296 |
+
# Application Footer
|
| 297 |
+
gr.Markdown("---")
|
| 298 |
+
gr.Markdown("""
|
| 299 |
+
**Author**: Louis Chua Bean Chong | **Project**: OpenLLM - Open Source Large Language Model | **License**: GPL-3.0
|
| 300 |
+
|
| 301 |
+
This training interface is part of the OpenLLM project, providing accessible and powerful
|
| 302 |
+
language model training capabilities through Hugging Face Spaces.
|
| 303 |
+
""")
|
| 304 |
+
|
| 305 |
+
return demo
|
| 306 |
+
|
| 307 |
+
if __name__ == "__main__":
|
| 308 |
+
# Launch the Gradio application when the script is run directly
|
| 309 |
+
# This is the entry point for the Hugging Face Space
|
| 310 |
+
demo = main()
|
| 311 |
+
demo.launch(
|
| 312 |
+
server_name="0.0.0.0", # Allow external connections
|
| 313 |
+
server_port=7860, # Default Gradio port
|
| 314 |
+
share=False, # Don't create public share link
|
| 315 |
+
debug=True # Enable debug mode for development
|
| 316 |
+
)
|