| 35 | const MODEL_NAME: &'static str = "flux"; |
| 36 | |
| 37 | async fn load(context: &mut Context) -> Result<Option<Box<Self>>> { |
| 38 | let model_repo = &context.args.model; |
| 39 | |
| 40 | let cache_dir = dirs::cache_dir() |
| 41 | .unwrap_or_else(std::env::temp_dir) |
| 42 | .to_string_lossy() |
| 43 | .to_string(); |
| 44 | |
| 45 | // Load tokenizer (lightweight, always in memory) |
| 46 | info!("Loading FLUX tokenizer..."); |
| 47 | let tokenizer_path = FluxModelFile::Tokenizer.get(model_repo, &cache_dir)?; |
| 48 | let tokenizer = Tokenizer::from_file(tokenizer_path) |
| 49 | .map_err(|e| anyhow!("failed to load tokenizer: {e}"))?; |
| 50 | info!("FLUX tokenizer loaded"); |
| 51 | |
| 52 | // Pre-download all component files so generate_image doesn't block on network |
| 53 | info!("Pre-downloading FLUX model files..."); |
| 54 | let _ = FluxModelFile::TextEncoder.get_all(model_repo, &cache_dir)?; |
| 55 | let _ = FluxModelFile::Transformer.get(model_repo, &cache_dir)?; |
| 56 | let _ = FluxModelFile::Vae.get(model_repo, &cache_dir)?; |
| 57 | info!("All FLUX model files ready"); |
| 58 | |
| 59 | let height = context.args.flux_args.height; |
| 60 | let width = context.args.flux_args.width; |
| 61 | |
| 62 | Ok(Some(Box::new(Self { |
| 63 | tokenizer, |
| 64 | context: context.clone(), |
| 65 | height, |
| 66 | width, |
| 67 | }))) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | #[async_trait] |