Skip to content

Commit

Permalink
feat: synchronous api
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobLinCool committed Aug 20, 2024
1 parent b37f26b commit 88198fc
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gradio"
version = "0.2.2"
version = "0.2.3"
edition = "2021"
authors = ["Jacob Lin <[email protected]>"]
description = "Gradio Client in Rust."
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Gradio Client in Rust.
- [x] The blocking `predict` method
- [x] The non-blocking `submit` method
- [x] Command-line interface
- [x] Synchronous and asynchronous API

## Documentation

Expand Down
14 changes: 14 additions & 0 deletions examples/hello-sync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use gradio::{Client, ClientOptions, PredictionInput};

fn main() {
let client = Client::new_sync("gradio/hello_world", ClientOptions::default()).unwrap();

let output = client
.predict_sync("/predict", vec![PredictionInput::from_value("Jacob")])
.unwrap();

println!(
"Output: {}",
output[0].clone().as_value().unwrap().as_str().unwrap()
);
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod data;
pub mod space;
pub mod stream;
pub mod structs;
pub mod sync;

pub use client::*;
pub use data::*;
Expand Down
41 changes: 41 additions & 0 deletions src/sync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use crate::client::{Client, ClientOptions};
use crate::data::{GradioFileData, PredictionInput, PredictionOutput};
use anyhow::Result;
use std::path::Path;
use tokio::runtime::Runtime;

impl Client {
pub fn new_sync(app_reference: &str, options: ClientOptions) -> Result<Self> {
let rt = Runtime::new()?;
let client = rt.block_on(Client::new(app_reference, options))?;
Ok(client)
}

pub fn predict_sync(
&self,
path: &str,
inputs: Vec<PredictionInput>,
) -> Result<Vec<PredictionOutput>> {
let rt = Runtime::new()?;
let output = rt.block_on(self.predict(path, inputs))?;
Ok(output)
}
}

impl GradioFileData {
pub fn download_sync(&self, http_client: Option<reqwest::Client>) -> Result<bytes::Bytes> {
let rt = Runtime::new()?;
let bytes = rt.block_on(self.download(http_client))?;
Ok(bytes)
}

pub fn save_to_path_sync(
&self,
path: impl AsRef<Path>,
http_client: Option<reqwest::Client>,
) -> Result<()> {
let rt = Runtime::new()?;
rt.block_on(self.save_to_path(path, http_client))?;
Ok(())
}
}

0 comments on commit 88198fc

Please sign in to comment.