Skip to content

Commit 8b6a788

Browse files
authored
Merge pull request #7 from JacobLinCool/gradio-5
feat: support gradio 5
2 parents 8228f83 + 725ccb6 commit 8b6a788

File tree

5 files changed

+42
-5
lines changed

5 files changed

+42
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "gradio"
3-
version = "0.3.0"
3+
version = "0.3.1"
44
edition = "2021"
55
authors = ["Jacob Lin <jacob@csie.cool>"]
66
description = "Gradio Client in Rust."

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Gradio Client in Rust.
1414
- [x] Command-line interface
1515
- [x] Synchronous and asynchronous API
1616

17-
> Supposed to work with Gradio 4, other versions are not tested.
17+
> Supposed to work with Gradio 5 & 4, other versions are not tested.
1818
1919
## Documentation
2020

examples/whisper-turbo.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use gradio::{Client, ClientOptions, PredictionInput};
2+
3+
#[tokio::main]
4+
async fn main() {
5+
if std::env::args().len() < 2 {
6+
println!("Please provide an audio file path as an argument");
7+
std::process::exit(1);
8+
}
9+
let args: Vec<String> = std::env::args().collect();
10+
let file_path = &args[1];
11+
println!("File: {}", file_path);
12+
13+
// Gradio v5
14+
let client = Client::new("hf-audio/whisper-large-v3-turbo", ClientOptions::default())
15+
.await
16+
.unwrap();
17+
18+
let output = client
19+
.predict(
20+
"/predict",
21+
vec![
22+
PredictionInput::from_file(file_path),
23+
PredictionInput::from_value("transcribe"),
24+
],
25+
)
26+
.await
27+
.unwrap();
28+
println!(
29+
"Output: {}",
30+
output[0].clone().as_value().unwrap().as_str().unwrap()
31+
);
32+
}

src/client.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl Client {
8383

8484
let http_client = Client::build_http_client(&options.hf_token)?;
8585

86-
let (api_root, space_id) =
86+
let (mut api_root, space_id) =
8787
Client::resolve_app_reference(&http_client, app_reference).await?;
8888

8989
if let Some((username, password)) = &options.auth {
@@ -95,6 +95,10 @@ impl Client {
9595
}
9696

9797
let config = Client::fetch_config(&http_client, &api_root).await?;
98+
if let Some(ref api_prefix) = config.api_prefix {
99+
api_root.push_str(api_prefix);
100+
}
101+
98102
let api_info = Client::fetch_api_info(&http_client, &api_root).await?;
99103

100104
Ok(Self {
@@ -229,9 +233,9 @@ impl Client {
229233
let json = res.json::<serde_json::Value>().await?;
230234
let config: AppConfigVersionOnly = serde_json::from_value(json.clone())?;
231235

232-
if !config.version.starts_with("4.") {
236+
if !config.version.starts_with("5.") && !config.version.starts_with("4.") {
233237
eprintln!(
234-
"Warning: This client is supposed to work with Gradio 4. The current version of the app is {}, which may cause issues.",
238+
"Warning: This client is supposed to work with Gradio 5 & 4. The current version of the app is {}, which may cause issues.",
235239
config.version
236240
);
237241
}

src/structs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub struct AppConfig {
3333
pub theme_hash: Option<StringOrI64>,
3434
pub username: Option<String>,
3535
pub max_file_size: Option<i64>,
36+
pub api_prefix: Option<String>,
3637
#[serde(default)]
3738
pub auth_required: Option<bool>,
3839
#[serde(default)]

0 commit comments

Comments
 (0)