replicate_client/models/
prediction.rs1use crate::models::file::{FileEncodingStrategy, FileInput};
4use serde::{Deserialize, Serialize};
5use serde_json::Value;
6use std::collections::HashMap;
7
8#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
10#[serde(rename_all = "lowercase")]
11pub enum PredictionStatus {
12 Starting,
14 Processing,
16 Succeeded,
18 Failed,
20 Canceled,
22}
23
24impl PredictionStatus {
25 pub fn is_terminal(&self) -> bool {
27 matches!(self, Self::Succeeded | Self::Failed | Self::Canceled)
28 }
29
30 pub fn is_running(&self) -> bool {
32 matches!(self, Self::Starting | Self::Processing)
33 }
34}
35
36#[derive(Debug, Clone, Serialize, Deserialize)]
38pub struct PredictionUrls {
39 pub get: String,
41 pub cancel: String,
43 pub stream: Option<String>,
45}
46
47#[derive(Debug, Clone, Serialize, Deserialize)]
49pub struct Prediction {
50 pub id: String,
52
53 pub model: String,
55
56 pub version: String,
58
59 pub status: PredictionStatus,
61
62 pub input: Option<HashMap<String, Value>>,
64
65 pub output: Option<Value>,
67
68 pub logs: Option<String>,
70
71 pub error: Option<String>,
73
74 pub metrics: Option<HashMap<String, Value>>,
76
77 pub created_at: Option<String>,
79
80 pub started_at: Option<String>,
82
83 pub completed_at: Option<String>,
85
86 pub urls: Option<PredictionUrls>,
88}
89
90impl Prediction {
91 pub fn is_complete(&self) -> bool {
93 self.status.is_terminal()
94 }
95
96 pub fn is_successful(&self) -> bool {
98 self.status == PredictionStatus::Succeeded
99 }
100
101 pub fn is_failed(&self) -> bool {
103 self.status == PredictionStatus::Failed
104 }
105
106 pub fn is_canceled(&self) -> bool {
108 self.status == PredictionStatus::Canceled
109 }
110}
111
112#[derive(Debug, Clone, Serialize)]
114pub struct CreatePredictionRequest {
115 pub version: String,
117
118 pub input: HashMap<String, Value>,
120
121 #[serde(skip_serializing_if = "Option::is_none")]
123 pub webhook: Option<String>,
124
125 #[serde(skip_serializing_if = "Option::is_none")]
127 pub webhook_completed: Option<String>,
128
129 #[serde(skip_serializing_if = "Option::is_none")]
131 pub webhook_events_filter: Option<Vec<String>>,
132
133 #[serde(skip_serializing_if = "Option::is_none")]
135 pub stream: Option<bool>,
136
137 #[serde(skip)]
139 pub file_inputs: HashMap<String, FileInput>,
140
141 #[serde(skip)]
143 pub file_encoding_strategy: FileEncodingStrategy,
144}
145
146impl CreatePredictionRequest {
147 pub fn new(version: impl Into<String>) -> Self {
149 Self {
150 version: version.into(),
151 input: HashMap::new(),
152 webhook: None,
153 webhook_completed: None,
154 webhook_events_filter: None,
155 stream: None,
156 file_inputs: HashMap::new(),
157 file_encoding_strategy: FileEncodingStrategy::default(),
158 }
159 }
160
161 pub fn with_input(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
163 self.input.insert(key.into(), value.into());
164 self
165 }
166
167 pub fn with_webhook(mut self, webhook: impl Into<String>) -> Self {
169 self.webhook = Some(webhook.into());
170 self
171 }
172
173 pub fn with_streaming(mut self) -> Self {
175 self.stream = Some(true);
176 self
177 }
178}