Skip to content

Commit

Permalink
Merge pull request #1 from Gitzoz/downloader
Browse files Browse the repository at this point in the history
Download whisper model from hugging face
  • Loading branch information
Gitzoz authored Oct 15, 2023
2 parents 38ac8d7 + b6b9709 commit bf2fc2f
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Cargo.lock

.vscode/*

models/
models/*

whisper.**

.DS_Store
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "transkriboi"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
whisper-rs = "0.8.0"
reqwest = { version = "0.11", features = ["json"] }
tokio = { version = "1", features = ["full"] }
2 changes: 2 additions & 0 deletions src/downloader/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod models;
pub mod web;
43 changes: 43 additions & 0 deletions src/downloader/models.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use core::fmt;

pub enum ModelFormat {
GGML,
COREML,
}

impl fmt::Display for ModelFormat {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ModelFormat::COREML => write!(f, "whisper.cpp-coreml"),
ModelFormat::GGML => write!(f, "whisper.cpp"),
}
}
}

pub enum ModelSize {
TinyEn,
Tiny,
BaseEn,
Base,
SmallEn,
Small,
Medium,
MediumEn,
Large,
}

impl fmt::Display for ModelSize {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ModelSize::Base => write!(f, "base"),
ModelSize::BaseEn => write!(f, "base.en"),
ModelSize::Tiny => write!(f, "tiny"),
ModelSize::TinyEn => write!(f, "tiny.en"),
ModelSize::Small => write!(f, "small"),
ModelSize::SmallEn => write!(f, "small.en"),
ModelSize::Medium => write!(f, "medium"),
ModelSize::MediumEn => write!(f, "medium.en"),
ModelSize::Large => write!(f, "large"),
}
}
}
23 changes: 23 additions & 0 deletions src/downloader/web.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::downloader::models::{ModelFormat, ModelSize};
use tokio::{fs::File, io::AsyncWriteExt};

const MODEL_SRC_URL: &str = "https://huggingface.co/ggerganov/";
const PFX: &str = "resolve/main/ggml";

pub async fn download_model(
output_path: &str,
model_size: ModelSize,
model_format: ModelFormat,
) -> Result<String, Box<dyn std::error::Error>> {
let url = format!(
"{}{}/{}-{}.bin",
MODEL_SRC_URL, model_format, PFX, model_size
);
let file_name = format!("{}{}_{}", output_path, model_format, model_size);
let mut response = reqwest::get(url).await?;
let mut file = File::create(file_name.clone()).await?;
while let Some(chunk) = response.chunk().await? {
file.write_all(&chunk).await?;
}
Ok(file_name)
}
14 changes: 14 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
mod downloader;
use crate::downloader::models::*;
use crate::downloader::web::download_model;

#[tokio::main]
async fn main() {
println!("Hello, world!");
let op = download_model("./models/", ModelSize::Tiny, ModelFormat::GGML).await;
match op {
Ok(n) => println!("Ok {}", n),
Err(e) => println!("Error {}", e)
}

}

0 comments on commit bf2fc2f

Please sign in to comment.