File size: 10,424 Bytes
3df89a1
f21249a
3df89a1
 
f21249a
 
3df89a1
f21249a
 
 
 
 
 
 
 
 
3df89a1
f21249a
 
 
 
 
 
 
 
 
 
 
 
 
3df89a1
 
f21249a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3df89a1
f21249a
3df89a1
f21249a
 
 
3df89a1
f21249a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3df89a1
f21249a
 
 
 
 
3df89a1
f21249a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3df89a1
f21249a
 
 
3df89a1
f21249a
 
 
3df89a1
f21249a
 
 
 
 
 
 
3df89a1
f21249a
 
 
 
 
 
 
 
3df89a1
f21249a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3df89a1
 
f21249a
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
"""

Enhanced CLI for KerdosAI with rich output and better UX.

"""

import typer
from typing import Optional
from pathlib import Path
from rich.console import Console
from rich.table import Table
from rich.progress import Progress, SpinnerColumn, TextColumn
from rich.panel import Panel
from rich import print as rprint
import logging

from config import load_config, KerdosConfig
from exceptions import KerdosError

# Initialize Typer app and Rich console
app = typer.Typer(
    name="kerdosai",
    help="KerdosAI - Universal LLM Training Agent",
    add_completion=False
)
console = Console()

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)


@app.command()
def train(

    config_file: Optional[Path] = typer.Option(

        None,

        "--config",

        "-c",

        help="Path to configuration file",

        exists=True

    ),

    model: Optional[str] = typer.Option(

        None,

        "--model",

        "-m",

        help="Base model name or path"

    ),

    data: Optional[Path] = typer.Option(

        None,

        "--data",

        "-d",

        help="Path to training data"

    ),

    output: Path = typer.Option(

        "./output",

        "--output",

        "-o",

        help="Output directory"

    ),

    epochs: int = typer.Option(3, "--epochs", "-e", help="Number of epochs"),

    batch_size: int = typer.Option(4, "--batch-size", "-b", help="Batch size"),

    use_lora: bool = typer.Option(True, "--lora/--no-lora", help="Use LoRA"),

    use_quantization: bool = typer.Option(

        False,

        "--quantize/--no-quantize",

        help="Use quantization"

    ),

):
    """

    Train a language model with custom data.

    """
    try:
        console.print(Panel.fit(
            "[bold cyan]KerdosAI Training[/bold cyan]",
            subtitle="Universal LLM Training Agent"
        ))
        
        # Load configuration
        if config_file:
            console.print(f"πŸ“„ Loading configuration from: [cyan]{config_file}[/cyan]")
            config = load_config(config_file)
        else:
            console.print("βš™οΈ  Using default configuration")
            config = load_config()
            
            # Override with CLI arguments
            if model:
                config.base_model = model
            if data:
                config.data.train_file = str(data)
            config.output_dir = str(output)
            config.training.epochs = epochs
            config.training.batch_size = batch_size
            config.lora.enabled = use_lora
            config.quantization.enabled = use_quantization
        
        # Validate configuration
        config.validate_compatibility()
        
        # Display configuration
        _display_config(config)
        
        # Import here to avoid slow startup
        from agent import KerdosAgent
        
        with Progress(
            SpinnerColumn(),
            TextColumn("[progress.description]{task.description}"),
            console=console
        ) as progress:
            # Initialize agent
            task = progress.add_task("Initializing agent...", total=None)
            agent = KerdosAgent(
                base_model=config.base_model,
                training_data=config.data.train_file,
                **config.model_dump()
            )
            progress.update(task, description="βœ“ Agent initialized")
            
            # Prepare for training
            task = progress.add_task("Preparing model for training...", total=None)
            if config.lora.enabled or config.quantization.enabled:
                agent.prepare_for_training(
                    use_lora=config.lora.enabled,
                    lora_r=config.lora.r,
                    lora_alpha=config.lora.alpha,
                    use_4bit=config.quantization.enabled and config.quantization.bits == 4,
                    use_8bit=config.quantization.enabled and config.quantization.bits == 8
                )
            progress.update(task, description="βœ“ Model prepared")
        
        # Train model
        console.print("\nπŸš€ Starting training...")
        metrics = agent.train(
            epochs=config.training.epochs,
            batch_size=config.training.batch_size,
            learning_rate=config.training.learning_rate
        )
        
        # Save model
        console.print(f"\nπŸ’Ύ Saving model to: [cyan]{config.output_dir}[/cyan]")
        agent.save(config.output_dir)
        
        # Display results
        console.print("\n[bold green]βœ“ Training completed successfully![/bold green]")
        _display_metrics(metrics)
        
    except KerdosError as e:
        console.print(f"\n[bold red]Error:[/bold red] {e}")
        raise typer.Exit(code=1)
    except Exception as e:
        console.print(f"\n[bold red]Unexpected error:[/bold red] {e}")
        logger.exception("Training failed")
        raise typer.Exit(code=1)


@app.command()
def generate(

    model_dir: Path = typer.Argument(..., help="Path to trained model"),

    prompt: str = typer.Option(..., "--prompt", "-p", help="Input prompt"),

    max_length: int = typer.Option(100, "--max-length", help="Maximum length"),

    temperature: float = typer.Option(0.7, "--temperature", "-t", help="Temperature"),

):
    """

    Generate text from a trained model.

    """
    try:
        console.print(Panel.fit("[bold cyan]KerdosAI Generation[/bold cyan]"))
        
        # Import here to avoid slow startup
        from agent import KerdosAgent
        
        with Progress(
            SpinnerColumn(),
            TextColumn("[progress.description]{task.description}"),
            console=console
        ) as progress:
            task = progress.add_task("Loading model...", total=None)
            agent = KerdosAgent.load(model_dir)
            progress.update(task, description="βœ“ Model loaded")
            
            task = progress.add_task("Generating...", total=None)
            output = agent.generate(
                prompt=prompt,
                max_length=max_length,
                temperature=temperature
            )
            progress.update(task, description="βœ“ Generation complete")
        
        console.print("\n[bold]Generated Text:[/bold]")
        console.print(Panel(output, border_style="green"))
        
    except Exception as e:
        console.print(f"\n[bold red]Error:[/bold red] {e}")
        raise typer.Exit(code=1)


@app.command()
def info(

    model_dir: Optional[Path] = typer.Argument(None, help="Path to model (optional)"),

):
    """

    Display model information.

    """
    try:
        if model_dir:
            from agent import KerdosAgent
            
            console.print(f"πŸ“Š Loading model info from: [cyan]{model_dir}[/cyan]\n")
            agent = KerdosAgent.load(model_dir)
            info_dict = agent.get_model_info()
            
            table = Table(title="Model Information", show_header=True)
            table.add_column("Property", style="cyan")
            table.add_column("Value", style="green")
            
            for key, value in info_dict.items():
                if key == "trainable_percentage":
                    table.add_row(key, f"{value:.2f}%")
                elif isinstance(value, (int, float)):
                    table.add_row(key, f"{value:,}")
                else:
                    table.add_row(key, str(value))
            
            console.print(table)
        else:
            console.print(Panel.fit(
                "[bold cyan]KerdosAI[/bold cyan]\n"
                "Version: 0.2.0\n"
                "Universal LLM Training Agent",
                title="About"
            ))
    
    except Exception as e:
        console.print(f"\n[bold red]Error:[/bold red] {e}")
        raise typer.Exit(code=1)


@app.command()
def validate_config(

    config_file: Path = typer.Argument(..., help="Path to configuration file"),

):
    """

    Validate a configuration file.

    """
    try:
        console.print(f"πŸ” Validating configuration: [cyan]{config_file}[/cyan]\n")
        
        config = load_config(config_file)
        config.validate_compatibility()
        
        console.print("[bold green]βœ“ Configuration is valid![/bold green]\n")
        _display_config(config)
        
    except KerdosError as e:
        console.print(f"\n[bold red]Validation failed:[/bold red] {e}")
        raise typer.Exit(code=1)


def _display_config(config: KerdosConfig):
    """Display configuration in a formatted table."""
    table = Table(title="Configuration", show_header=True)
    table.add_column("Setting", style="cyan")
    table.add_column("Value", style="green")
    
    table.add_row("Base Model", config.base_model)
    table.add_row("Output Directory", config.output_dir)
    table.add_row("Epochs", str(config.training.epochs))
    table.add_row("Batch Size", str(config.training.batch_size))
    table.add_row("Learning Rate", f"{config.training.learning_rate:.2e}")
    table.add_row("LoRA Enabled", "βœ“" if config.lora.enabled else "βœ—")
    if config.lora.enabled:
        table.add_row("  LoRA Rank", str(config.lora.r))
        table.add_row("  LoRA Alpha", str(config.lora.alpha))
    table.add_row("Quantization", "βœ“" if config.quantization.enabled else "βœ—")
    if config.quantization.enabled:
        table.add_row("  Bits", str(config.quantization.bits))
    
    console.print(table)


def _display_metrics(metrics: dict):
    """Display training metrics in a formatted table."""
    if not metrics:
        return
    
    table = Table(title="Training Metrics", show_header=True)
    table.add_column("Metric", style="cyan")
    table.add_column("Value", style="green")
    
    for key, value in metrics.items():
        if isinstance(value, float):
            table.add_row(key, f"{value:.4f}")
        else:
            table.add_row(key, str(value))
    
    console.print(table)


def main():
    """Main entry point."""
    app()


if __name__ == "__main__":
    main()