From 725ccb657072146adf3e6594c340a5fe5867bf5f Mon Sep 17 00:00:00 2001 From: JacobLinCool Date: Sat, 12 Oct 2024 16:45:07 +0800 Subject: [PATCH] feat: support gradio 5 --- Cargo.toml | 2 +- README.md | 2 +- examples/whisper-turbo.rs | 32 ++++++++++++++++++++++++++++++++ src/client.rs | 10 +++++++--- src/structs.rs | 1 + 5 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 examples/whisper-turbo.rs diff --git a/Cargo.toml b/Cargo.toml index cf0c985..89728b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gradio" -version = "0.3.0" +version = "0.3.1" edition = "2021" authors = ["Jacob Lin "] description = "Gradio Client in Rust." diff --git a/README.md b/README.md index 704341d..3099da0 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Gradio Client in Rust. - [x] Command-line interface - [x] Synchronous and asynchronous API -> Supposed to work with Gradio 4, other versions are not tested. +> Supposed to work with Gradio 5 & 4, other versions are not tested. ## Documentation diff --git a/examples/whisper-turbo.rs b/examples/whisper-turbo.rs new file mode 100644 index 0000000..2c329f2 --- /dev/null +++ b/examples/whisper-turbo.rs @@ -0,0 +1,32 @@ +use gradio::{Client, ClientOptions, PredictionInput}; + +#[tokio::main] +async fn main() { + if std::env::args().len() < 2 { + println!("Please provide an audio file path as an argument"); + std::process::exit(1); + } + let args: Vec = std::env::args().collect(); + let file_path = &args[1]; + println!("File: {}", file_path); + + // Gradio v5 + let client = Client::new("hf-audio/whisper-large-v3-turbo", ClientOptions::default()) + .await + .unwrap(); + + let output = client + .predict( + "/predict", + vec![ + PredictionInput::from_file(file_path), + PredictionInput::from_value("transcribe"), + ], + ) + .await + .unwrap(); + println!( + "Output: {}", + output[0].clone().as_value().unwrap().as_str().unwrap() + ); +} diff --git a/src/client.rs b/src/client.rs index 99f812b..d152a49 100644 --- a/src/client.rs +++ b/src/client.rs @@ -83,7 +83,7 @@ impl Client { let http_client = Client::build_http_client(&options.hf_token)?; - let (api_root, space_id) = + let (mut api_root, space_id) = Client::resolve_app_reference(&http_client, app_reference).await?; if let Some((username, password)) = &options.auth { @@ -95,6 +95,10 @@ impl Client { } let config = Client::fetch_config(&http_client, &api_root).await?; + if let Some(ref api_prefix) = config.api_prefix { + api_root.push_str(api_prefix); + } + let api_info = Client::fetch_api_info(&http_client, &api_root).await?; Ok(Self { @@ -229,9 +233,9 @@ impl Client { let json = res.json::().await?; let config: AppConfigVersionOnly = serde_json::from_value(json.clone())?; - if !config.version.starts_with("4.") { + if !config.version.starts_with("5.") && !config.version.starts_with("4.") { eprintln!( - "Warning: This client is supposed to work with Gradio 4. The current version of the app is {}, which may cause issues.", + "Warning: This client is supposed to work with Gradio 5 & 4. The current version of the app is {}, which may cause issues.", config.version ); } diff --git a/src/structs.rs b/src/structs.rs index 78e95bd..86d10cd 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -33,6 +33,7 @@ pub struct AppConfig { pub theme_hash: Option, pub username: Option, pub max_file_size: Option, + pub api_prefix: Option, #[serde(default)] pub auth_required: Option, #[serde(default)]