1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use std::fmt::Display;
use crate::{
error::StabilityAIError,
types::{
Artifacts, ImageToImageRequestBody, ImageToImageUpscaleBody, MaskingRequestBody,
TextToImageRequestBody,
},
Client,
};
/// Generate images from text, existing images, or both
pub struct Generate<'c, E: Display> {
client: &'c Client,
engine_id: E,
}
impl<'c, E: Display> Generate<'c, E> {
pub fn new(client: &'c Client, engine_id: E) -> Self {
Self { client, engine_id }
}
/// Generate a new image from a text prompt
pub async fn text_to_image(
&self,
request: TextToImageRequestBody,
) -> Result<Artifacts, StabilityAIError> {
self.client
.post(
&format!("/generation/{}/text-to-image", self.engine_id),
request,
)
.await
}
/// Modify an image based on a text prompt
pub async fn image_to_image(
&self,
request: ImageToImageRequestBody,
) -> Result<Artifacts, StabilityAIError> {
self.client
.post_form(
&format!("/generation/{}/image-to-image", self.engine_id),
request,
)
.await
}
/// Create a higher resolution version of an input image.
///
///
/// This operation outputs an image with a maximum pixel count of **4,194,304**.
/// This is equivalent to dimensions such as `2048x2048` and `4096x1024`.
///
///
/// By default, the input image will be upscaled by a factor of 2.
/// For additional control over the output dimensions, a `width` or `height`
/// parameter may be specified.\n\nFor upscaler engines that are ESRGAN-based,
/// refer to the `RealESRGANUpscaleRequestBody` body option below. For upscaler
/// engines that are Stable Diffusion Latent Upscaler-based, refer to the
/// `LatentUpscalerUpscaleRequestBody` body option below.
///
///
/// For more details on the upscaler engines, refer to the
/// [documentation on the Platform site.](https://platform.stability.ai/docs/features/image-upscaling?tab=python)
pub async fn image_to_image_upscale<R: Into<ImageToImageUpscaleBody>>(
&self,
request: R,
) -> Result<Artifacts, StabilityAIError> {
self.client
.post_form(
&format!("/generation/{}/image-to-image/upscale", self.engine_id),
request.into(),
)
.await
}
/// Selectively modify portions of an image using a mask
pub async fn image_to_image_masking(
&self,
request: MaskingRequestBody,
) -> Result<Artifacts, StabilityAIError> {
self.client
.post_form(
&format!("/generation/{}/image-to-image/masking", self.engine_id),
request,
)
.await
}
}