ogenius 0.1.6

Lightweight AI inference server using HuggingFace models directly - a simpler alternative to Ollama
Documentation
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
//! # Ogenius: The Voice
//! The `ogenius` CLI provides an interactive chat REPL and an OpenAI-compatible API server,
//! with automatic model downloading from Huggingface.

mod api;

use anyhow::Result;
use api::{chat_completions, list_models, ApiState};
use async_std::sync::Mutex;
use clap::{Parser, Subcommand};
use colored::*;
use futures::channel::mpsc;
use futures::sink::SinkExt;
use futures::StreamExt;
use indicatif::{MultiProgress, ProgressBar, ProgressDrawTarget, ProgressStyle};
use rusty_genius_core::protocol::{
    AssetEvent, BrainstemBody, BrainstemCommand, BrainstemInput, BrainstemOutput, InferenceConfig,
    InferenceEvent,
};
use rusty_genius_stem::Orchestrator;
use std::io::IsTerminal;
use std::io::{self, Write};
use std::process;
use std::sync::Arc;
use tide_websockets::{Message, WebSocket};

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Download a model from HuggingFace
    Download {
        /// HuggingFace model repo (e.g., Qwen/Qwen2.5-1.5B-Instruct)
        repo: String,
    },
    /// Start interactive chat in CLI
    Serve {
        /// HTTP server address
        #[arg(long, default_value = "127.0.0.1:8080")]
        addr: String,
        /// WebSocket server address
        #[arg(long, default_value = "127.0.0.1:8081")]
        ws_addr: String,
        /// Model repository to pre-load
        #[arg(long)]
        model: Option<String>,
        /// Do not open the browser automatically
        #[arg(long)]
        no_open: bool,
        /// Unload model after inactivity (seconds)
        #[arg(long, default_value = "300")]
        unload_after: u64,
        /// Quantization level (e.g. Q4_K_M)
        #[arg(long, default_value = "Q4_K_M")]
        quant: String,
        /// Context size
        #[arg(long, default_value = "2048")]
        context_size: u32,
        /// Show thinking tokens
        #[arg(long, default_value = "true")]
        /// Show thinking tokens
        #[arg(long, default_value = "true")]
        show_thinking: bool,
        /// Models to pre-load (download/verify) before starting
        #[arg(long)]
        load_models: Vec<String>,
    },
    /// Start interactive chat in CLI
    Chat {
        /// Model repository
        #[arg(long, default_value = "Qwen/Qwen2.5-1.5B-Instruct")]
        model: String,
        /// Quantization level
        #[arg(long, default_value = "Q4_K_M")]
        quant: String,
        /// Context size
        #[arg(long, default_value = "2048")]
        context_size: u32,
        /// Show thinking tokens
        #[arg(long, default_value = "true")]
        show_thinking: bool,
        /// Models to pre-load (download/verify) before starting
        #[arg(long)]
        load_models: Vec<String>,
    },
    /// Generate embeddings for input text
    Embed {
        /// Model repository
        #[arg(long, default_value = "Qwen/Qwen2.5-1.5B-Instruct")]
        model: String,
        /// Quantization level
        #[arg(long, default_value = "Q4_K_M")]
        quant: String,
        /// Text input to embed
        #[arg(long)]
        input: String,
        /// Context size
        #[arg(long, default_value = "2048")]
        context_size: u32,
    },
}

/// Pre-load and verify models in parallel with progress tracking
async fn wait_for_models(load_models: Vec<String>) -> Result<()> {
    if load_models.is_empty() {
        return Ok(());
    }

    println!("📦 Pre-loading {} models...", load_models.len());
    let authority = facecrab::AssetAuthority::new()?;
    let multi_progress = MultiProgress::new();
    let is_tty = io::stdout().is_terminal();

    if !is_tty {
        println!("Running in non-interactive mode. Progress bars disabled.");
        multi_progress.set_draw_target(ProgressDrawTarget::hidden());
    }

    let tasks: Vec<_> = load_models
        .iter()
        .map(|m| {
            let auth = &authority;
            let name = m.clone();
            let pb = multi_progress.add(ProgressBar::new(0));
            pb.set_style(ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {bytes}/{total_bytes} ({eta}) {msg}")
                .unwrap()
                .progress_chars("#>-"));
            pb.set_message(format!("Waiting: {}", name));

            async move {
                let mut stream = auth.ensure_model_stream(&name);
                let mut last_path = None;
                let mut last_pct = 0;
                while let Some(event) = stream.next().await {
                    match event {
                        AssetEvent::Started(_) => {
                            if is_tty {
                                pb.set_message(format!("Downloading: {}", name));
                            } else {
                                println!("Downloading: {}", name);
                            }
                        }
                        AssetEvent::Progress(current, total) => {
                            if is_tty {
                                pb.set_length(total);
                                pb.set_position(current);
                            } else if total > 0 {
                                let current_pct = (current * 100) / total;
                                if current_pct >= last_pct + 10 {
                                    println!("Downloading: {} {}%", name, current_pct);
                                    last_pct = current_pct;
                                }
                            }
                        }
                        AssetEvent::Complete(path) => {
                            if is_tty {
                                pb.finish_with_message(format!("✅ Ready: {}", name));
                            } else {
                                println!("✅ Ready: {}", name);
                            }
                            last_path = Some(std::path::PathBuf::from(path));
                        }
                        AssetEvent::Error(e) => {
                            if is_tty {
                                pb.abandon_with_message(format!("❌ Error: {}", e));
                            } else {
                                println!("❌ Error: {}", e);
                            }
                            return Err(anyhow::anyhow!("Failed to download {}: {}", name, e));
                        }
                    }
                }
                if let Some(path) = last_path {
                    Ok(path)
                } else {
                    Err(anyhow::anyhow!("Stream ended without completion for {}", name))
                }
            }
        })
        .collect();

    let results = futures::future::join_all(tasks).await;

    // Clear multi_progress to ensure output below it prints clean
    let _ = multi_progress.clear();

    let mut failures = Vec::new();
    for (i, res) in results.into_iter().enumerate() {
        match res {
            Ok(path) => {
                println!("✅ Ready: {} ({})", load_models[i].green(), path.display());
            }
            Err(e) => failures.push(format!("{}: {}", load_models[i], e)),
        }
    }

    if !failures.is_empty() {
        eprintln!("\n❌ Failed to load some models:");
        for f in failures {
            eprintln!("  - {}", f.red());
        }
        anyhow::bail!("Failed to load some models");
    }
    println!("✨ All models loaded.\n");
    Ok(())
}

#[async_std::main]
async fn main() -> anyhow::Result<()> {
    println!("DEBUG: ogenius main starting...");
    let _ = io::stdout().flush();
    // Install Ctrl-C handler for graceful shutdown (especially during downloads)
    ctrlc::set_handler(move || {
        println!("\n🛑 Received Ctrl-C, exiting...");
        process::exit(130);
    })?;

    let cli = Cli::parse();

    match cli.command {
        Commands::Download { repo } => {
            println!("📥 Downloading {}", repo.cyan());
            let mut orchestrator = Orchestrator::new().await?;
            let (mut input_tx, input_rx) = mpsc::channel(100);
            let (output_tx, mut output_rx) = mpsc::channel(100);

            async_std::task::spawn(async move {
                let _ = orchestrator.run(input_rx, output_tx).await;
            });

            input_tx
                .send(BrainstemInput {
                    id: None,
                    command: BrainstemCommand::LoadModel(repo),
                })
                .await?;

            while let Some(output) = output_rx.next().await {
                match output.body {
                    BrainstemBody::Asset(AssetEvent::Progress(curr, total)) => {
                        let pct = if total > 0 {
                            (curr as f64 / total as f64) * 100.0
                        } else {
                            0.0
                        };
                        print!("\rProgress: {:.1}% ({}/{})", pct, curr, total);
                        io::stdout().flush()?;
                    }
                    BrainstemBody::Asset(AssetEvent::Complete(path)) => {
                        println!("\n✅ Download complete: {}", path.green());
                        break;
                    }
                    BrainstemBody::Asset(AssetEvent::Error(e)) => {
                        eprintln!("\n❌ Error: {}", e.red());
                        break;
                    }
                    BrainstemBody::Error(e) => {
                        eprintln!("\n❌ Orchestrator Error: {}", e.red());
                        break;
                    }
                    _ => {}
                }
            }
        }
        Commands::Chat {
            model,
            quant: _,
            context_size,
            show_thinking,
            load_models,
        } => {
            // Pre-load models if requested
            wait_for_models(load_models).await?;

            println!("💬 Starting chat with {}", model.cyan());
            let mut orchestrator = Orchestrator::new().await?;
            let (mut input_tx, input_rx) = mpsc::channel(100);
            let (output_tx, mut output_rx) = mpsc::channel(100);

            async_std::task::spawn(async move {
                let _ = orchestrator.run(input_rx, output_tx).await;
            });

            let config = InferenceConfig {
                context_size: Some(context_size),
                show_thinking,
                ..Default::default()
            };

            // Pre-load model
            input_tx
                .send(BrainstemInput {
                    id: None,
                    command: BrainstemCommand::LoadModel(model.clone()),
                })
                .await?;
            println!("⏳ Loading model...");

            while let Some(output) = output_rx.next().await {
                match output.body {
                    BrainstemBody::Asset(AssetEvent::Complete(_)) => break,
                    BrainstemBody::Error(e) => {
                        eprintln!("❌ Failed to load: {}", e.red());
                        return Ok(());
                    }
                    _ => {}
                }
            }
            println!("✅ Model loaded!");
            println!("(Type 'exit' to quit)\n");

            let stdin = io::stdin();
            let mut line = String::new();
            loop {
                print!("{} ", "YOU >".bright_white());
                io::stdout().flush()?;
                line.clear();
                if stdin.read_line(&mut line)? == 0 {
                    break;
                }
                let input = line.trim();
                if input == "exit" || input == "quit" {
                    break;
                }
                let prompt = input.trim();
                if prompt.is_empty() {
                    continue;
                }

                input_tx
                    .send(BrainstemInput {
                        id: None,
                        command: BrainstemCommand::Infer {
                            model: Some(model.clone()),
                            prompt: prompt.to_string(),
                            config: config.clone(),
                        },
                    })
                    .await?;

                print!("{} ", "AI >".bright_green());
                io::stdout().flush()?;

                while let Some(output) = output_rx.next().await {
                    match output.body {
                        BrainstemBody::Event(InferenceEvent::Content(c)) => {
                            print!("{}", c);
                            io::stdout().flush()?;
                        }
                        BrainstemBody::Event(InferenceEvent::Complete) => {
                            println!();
                            break;
                        }
                        BrainstemBody::Error(e) => {
                            eprintln!("\n❌ Error: {}", e.red());
                            break;
                        }
                        _ => {}
                    }
                }
            }
        }
        Commands::Embed {
            model,
            quant: _,
            input,
            context_size,
        } => {
            println!("🔢 Generating embeddings using {}", model.cyan());
            let mut orchestrator = Orchestrator::new().await?;
            let (mut input_tx, input_rx) = mpsc::channel(100);
            let (output_tx, mut output_rx) = mpsc::channel(100);

            async_std::task::spawn(async move {
                let _ = orchestrator.run(input_rx, output_tx).await;
            });

            let config = InferenceConfig {
                context_size: Some(context_size),
                show_thinking: false,
                ..Default::default()
            };

            // Pre-load model
            input_tx
                .send(BrainstemInput {
                    id: None,
                    command: BrainstemCommand::LoadModel(model.clone()),
                })
                .await?;
            println!("⏳ Loading model...");

            while let Some(output) = output_rx.next().await {
                match output.body {
                    BrainstemBody::Asset(AssetEvent::Complete(_)) => break,
                    BrainstemBody::Error(e) => {
                        eprintln!("❌ Failed to load: {}", e.red());
                        return Ok(());
                    }
                    _ => {}
                }
            }
            println!("✅ Model loaded!");

            // Send embedding request
            input_tx
                .send(BrainstemInput {
                    id: None,
                    command: BrainstemCommand::Embed {
                        model: Some(model),
                        input,
                        config,
                    },
                })
                .await?;

            println!("⏳ Generating embedding...");

            while let Some(output) = output_rx.next().await {
                match output.body {
                    BrainstemBody::Event(InferenceEvent::Embedding(emb)) => {
                        println!("✅ Embedding generated ({} dimensions)", emb.len());
                        println!("First 10 values: {:?}", &emb[..10.min(emb.len())]);
                        break;
                    }
                    BrainstemBody::Event(InferenceEvent::Complete) => {
                        break;
                    }
                    BrainstemBody::Error(e) => {
                        eprintln!("❌ Error: {}", e.red());
                        break;
                    }
                    _ => {}
                }
            }
        }
        Commands::Serve {
            addr,
            ws_addr,
            model,
            no_open,
            unload_after: _,
            quant: _,
            context_size,
            show_thinking,
            load_models,
        } => {
            // Pre-load models if requested
            wait_for_models(load_models).await?;

            println!("DEBUG: Initializing Orchestrator...");
            let _ = io::stdout().flush();
            let mut orchestrator = Orchestrator::new().await?;
            println!("DEBUG: Orchestrator initialized.");
            let _ = io::stdout().flush();
            let (input_tx, input_rx) = mpsc::channel(500);
            let (output_tx, mut output_rx) = mpsc::channel(500);

            let broadcast_senders: Arc<Mutex<Vec<mpsc::Sender<BrainstemOutput>>>> =
                Arc::new(Mutex::new(Vec::new()));

            let state = ApiState {
                input_tx: input_tx.clone(),
                output_senders: broadcast_senders.clone(),
                ws_addr: ws_addr.clone(),
            };

            async_std::task::spawn(async move {
                eprintln!("DEBUG: Orchestrator starting...");
                if let Err(e) = orchestrator.run(input_rx, output_tx).await {
                    eprintln!("❌ Orchestrator CRASHED: {}", e);
                }
                eprintln!("DEBUG: Orchestrator exited.");
            });

            let bridge_senders = broadcast_senders.clone();
            async_std::task::spawn(async move {
                while let Some(msg) = output_rx.next().await {
                    let mut senders = bridge_senders.lock().await;
                    let mut to_remove = Vec::new();
                    for (i, sender) in senders.iter_mut().enumerate() {
                        // Use try_send to avoid blocking the whole bridge if one client is slow
                        if let Err(e) = sender.try_send(msg.clone()) {
                            if e.is_disconnected() {
                                to_remove.push(i);
                            }
                        }
                    }
                    for i in to_remove.into_iter().rev() {
                        senders.remove(i);
                    }
                }
            });

            let inference_config = InferenceConfig {
                context_size: Some(context_size),
                show_thinking,
                ..Default::default()
            };

            if let Some(m) = model {
                let _ = input_tx
                    .clone()
                    .send(BrainstemInput {
                        id: None,
                        command: BrainstemCommand::LoadModel(m),
                    })
                    .await;
            }

            let mut app = tide::with_state(state);

            app.at("/").get(|_| async {
                let html = include_str!("index.html");
                Ok(tide::Response::builder(200)
                    .content_type(tide::http::mime::HTML)
                    .body(html)
                    .build())
            });

            app.at("/v1/models").get(list_models);
            app.at("/v1/chat/completions").post(chat_completions);
            app.at("/v1/embeddings").post(api::embeddings);
            app.at("/v1/engine/reset").post(api::reset_engine);
            app.at("/v1/config").get(api::get_config);

            let input_tx_ws = input_tx.clone();
            let bc_senders = broadcast_senders.clone();
            let ws_addr_srv = ws_addr.clone();
            async_std::task::spawn(async move {
                let mut ws_app = tide::new();
                ws_app.at("/").get(WebSocket::new(move |_req, mut stream| {
                    let mut input_tx = input_tx_ws.clone();
                    let bc_senders = bc_senders.clone();
                    let inference_config = inference_config.clone();
                    async move {
                        let (tx, mut rx) = mpsc::channel(500);
                        {
                            let mut senders = bc_senders.lock().await;
                            senders.push(tx);
                        }

                        let stream_write = stream.clone();
                        async_std::task::spawn(async move {
                            while let Some(event) = rx.next().await {
                                if let Ok(json) = serde_json::to_string(&event) {
                                    if stream_write.send_string(json).await.is_err() {
                                        break;
                                    }
                                }
                            }
                        });

                        while let Some(Ok(Message::Text(input))) = stream.next().await {
                            if let Ok(json) = serde_json::from_str::<serde_json::Value>(&input) {
                                let prompt = json["prompt"].as_str().unwrap_or("").to_string();
                                let model = json["model"].as_str().map(|s| s.to_string());
                                let _ = input_tx
                                    .send(BrainstemInput {
                                        id: None,
                                        command: BrainstemCommand::Infer {
                                            model,
                                            prompt,
                                            config: inference_config.clone(),
                                        },
                                    })
                                    .await;
                            }
                        }
                        Ok(())
                    }
                }));
                if let Err(e) = ws_app.listen(ws_addr_srv).await {
                    eprintln!("❌ WebSocket Listen Error: {}", e);
                }
            });

            if !no_open {
                let url = if addr.contains(':') {
                    if addr.starts_with(':') {
                        format!("http://127.0.0.1{}", addr)
                    } else {
                        format!("http://{}", addr)
                    }
                } else {
                    format!("http://{}:8080", addr)
                };
                let _ = open_browser(&url).await;
            }

            eprintln!("🚀 API Server listening on {}", addr.cyan());
            app.listen(addr).await?;
        }
    }

    Ok(())
}

async fn open_browser(url: &str) -> Result<()> {
    #[cfg(target_os = "macos")]
    let cmd = "open";
    #[cfg(target_os = "linux")]
    let cmd = "xdg-open";
    #[cfg(target_os = "windows")]
    let cmd = "start";

    #[cfg(not(target_os = "windows"))]
    let status = async_std::process::Command::new(cmd)
        .arg(url)
        .status()
        .await?;

    #[cfg(target_os = "windows")]
    let status = async_std::process::Command::new("cmd")
        .arg("/c")
        .arg(cmd)
        .arg(url)
        .status()
        .await?;

    if !status.success() {
        eprintln!("⚠️ Failed to open browser: {}", url);
    }

    Ok(())
}