Skip to content

Commit

Permalink
feat: sync version of submit
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobLinCool committed Aug 30, 2024
1 parent 8d1491d commit 34eaed9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
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.4"
version = "0.2.5"
edition = "2021"
authors = ["Jacob Lin <[email protected]>"]
description = "Gradio Client in Rust."
Expand Down
3 changes: 3 additions & 0 deletions src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ impl PredictionStream {

pub async fn next(&mut self) -> Option<Result<QueueDataMessage>> {
let event = self.es.next().await;
if event.is_none() {
return Some(Err(Error::msg("Stream ended")));
}
let event = event.unwrap();

match event {
Expand Down
26 changes: 25 additions & 1 deletion src/sync.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::client::{Client, ClientOptions};
use crate::data::{GradioFileData, PredictionInput, PredictionOutput};
use anyhow::Result;
use crate::stream::PredictionStream;
use crate::structs::QueueDataMessage;
use anyhow::{Error, Result};
use std::path::Path;
use tokio::runtime::Runtime;

Expand All @@ -11,6 +13,16 @@ impl Client {
Ok(client)
}

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

pub fn predict_sync(
&self,
path: &str,
Expand Down Expand Up @@ -39,3 +51,15 @@ impl GradioFileData {
Ok(())
}
}

impl PredictionStream {
pub fn next_sync(&mut self) -> Option<Result<QueueDataMessage>> {
let rt = Runtime::new();
if rt.is_err() {
return Some(Err(Error::msg("Runtime error")));
}
let rt = rt.unwrap();
let output = rt.block_on(self.next())?;
Some(output)
}
}

0 comments on commit 34eaed9

Please sign in to comment.