use crate::hf_pipeline::error::FetchError;
use crate::hf_pipeline::export::gguf_writer::GgufQuantization;
use crate::hf_pipeline::export::pipeline::{quantize_and_export, QuantExportResult};
use crate::hf_pipeline::export::weights::ModelWeights;
use crate::hf_pipeline::publish::config::PublishConfig;
use crate::hf_pipeline::publish::publisher::HfPublisher;
use crate::hf_pipeline::publish::result::{PublishError, PublishResult};
use std::path::Path;
#[derive(Debug, Clone)]
pub struct QuantPublishResult {
pub export: QuantExportResult,
pub publish: PublishResult,
}
pub fn quantize_export_publish(
weights: &ModelWeights,
quantization: GgufQuantization,
publish_config: PublishConfig,
output_dir: impl AsRef<Path>,
) -> std::result::Result<QuantPublishResult, QuantPublishError> {
let output_dir = output_dir.as_ref();
let filename = "model.gguf";
let export_result = quantize_and_export(weights, quantization, output_dir, filename)
.map_err(QuantPublishError::Export)?;
let publisher = HfPublisher::new(publish_config).map_err(QuantPublishError::Publish)?;
let gguf_path = output_dir.join(filename);
let files: Vec<(&Path, &str)> = vec![(&gguf_path, filename)];
let mut publish_result = publisher.publish(&files, None).map_err(QuantPublishError::Publish)?;
if let Some(readme) = &export_result.readme {
publisher
.upload_bytes(readme.as_bytes(), "README.md")
.map_err(QuantPublishError::Publish)?;
publish_result.model_card_generated = true;
}
Ok(QuantPublishResult { export: export_result, publish: publish_result })
}
#[derive(Debug, thiserror::Error)]
pub enum QuantPublishError {
#[error("Export failed: {0}")]
Export(FetchError),
#[error("Publish failed: {0}")]
Publish(PublishError),
}