use crate::models::file::{FileEncodingStrategy, FileInput};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum PredictionStatus {
Starting,
Processing,
Succeeded,
Failed,
Canceled,
}
impl PredictionStatus {
pub fn is_terminal(&self) -> bool {
matches!(self, Self::Succeeded | Self::Failed | Self::Canceled)
}
pub fn is_running(&self) -> bool {
matches!(self, Self::Starting | Self::Processing)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PredictionUrls {
pub get: String,
pub cancel: String,
pub stream: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Prediction {
pub id: String,
pub model: String,
pub version: String,
pub status: PredictionStatus,
pub input: Option<HashMap<String, Value>>,
pub output: Option<Value>,
pub logs: Option<String>,
pub error: Option<String>,
pub metrics: Option<HashMap<String, Value>>,
pub created_at: Option<String>,
pub started_at: Option<String>,
pub completed_at: Option<String>,
pub urls: Option<PredictionUrls>,
}
impl Prediction {
pub fn is_complete(&self) -> bool {
self.status.is_terminal()
}
pub fn is_successful(&self) -> bool {
self.status == PredictionStatus::Succeeded
}
pub fn is_failed(&self) -> bool {
self.status == PredictionStatus::Failed
}
pub fn is_canceled(&self) -> bool {
self.status == PredictionStatus::Canceled
}
}
#[derive(Debug, Clone, Serialize)]
pub struct CreatePredictionRequest {
pub version: String,
pub input: HashMap<String, Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub webhook: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub webhook_completed: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub webhook_events_filter: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub stream: Option<bool>,
#[serde(skip)]
pub file_inputs: HashMap<String, FileInput>,
#[serde(skip)]
pub file_encoding_strategy: FileEncodingStrategy,
}
impl CreatePredictionRequest {
pub fn new(version: impl Into<String>) -> Self {
Self {
version: version.into(),
input: HashMap::new(),
webhook: None,
webhook_completed: None,
webhook_events_filter: None,
stream: None,
file_inputs: HashMap::new(),
file_encoding_strategy: FileEncodingStrategy::default(),
}
}
pub fn with_input(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
self.input.insert(key.into(), value.into());
self
}
pub fn with_webhook(mut self, webhook: impl Into<String>) -> Self {
self.webhook = Some(webhook.into());
self
}
pub fn with_streaming(mut self) -> Self {
self.stream = Some(true);
self
}
}