1+ from typing import Any , Dict , List , Union , cast
2+ from typing_extensions import NotRequired , TypedDict , Literal , Required
3+ from .request import Request , RequestConfig
4+ from .async_request import AsyncRequest
5+
6+ from typing import List , Union
7+ from ._config import ClientConfig
8+
9+ class ImageGenerationParams (TypedDict ):
10+ prompt : Required [str ]
11+ """"
12+ The text to generate the image from."
13+ """
14+ aspect_ratio : NotRequired [Literal ["1:1" , "16:9" , "21:9" , "3:2" , "2:3" , "4:5" , "5:4" , "3:4" , "4:3" , "9:16" , "9:21" ]]
15+ """
16+ The aspect ratio of the image. The default is 1:1.
17+ """
18+ width : NotRequired [int ]
19+ """"
20+ The width of the image. The default is 512."
21+ """
22+ height : NotRequired [int ]
23+ """
24+ The height of the image. The default is 512."
25+ """
26+ steps : NotRequired [int ]
27+ """"
28+ The number of steps to generate the image.""
29+ """
30+ advance_config : NotRequired [Dict [str , Union [int , str ]]]
31+ """
32+ The advance configuration for the image generation. The default is None.
33+ You can pass the following:
34+ - `seed`: The seed for the image generation. The default is None.
35+ - `guidance`: The guidance for the image generation. The default is None.
36+ - `negative_prompt`: The negative prompt for the image generation. The default is None.
37+ """
38+
39+ class ImageGenerationResponse (TypedDict ):
40+ success : bool
41+ """
42+ Indicates whether the image generation was successful.
43+ """
44+ image : bytes
45+ """
46+ The generated image as a blob.
47+ """
48+
49+ class ImageGeneration (ClientConfig ):
50+ config : RequestConfig
51+
52+ def __init__ (
53+ self ,
54+ api_key : str ,
55+ api_url : str ,
56+ disable_request_logging : Union [bool , None ] = False ,
57+ ):
58+ super ().__init__ (api_key , api_url , disable_request_logging = disable_request_logging )
59+ self .config = RequestConfig (
60+ api_url = api_url ,
61+ api_key = api_key ,
62+ disable_request_logging = disable_request_logging ,
63+ )
64+
65+ def image_generation (self , params : ImageGenerationParams ) -> ImageGenerationResponse :
66+ path = "/ai/image_generation"
67+ resp = Request (
68+ config = self .config ,
69+ path = path ,
70+ params = cast (Dict [Any , Any ], params ), # type: ignore
71+ verb = "post" ,
72+ ).perform ()
73+ return resp
74+
75+ class AsyncImageGeneration (ClientConfig ):
76+ config : RequestConfig
77+
78+ def __init__ (
79+ self ,
80+ api_key : str ,
81+ api_url : str ,
82+ disable_request_logging : Union [bool , None ] = False ,
83+ ):
84+ super ().__init__ (api_key , api_url , disable_request_logging = disable_request_logging )
85+ self .config = RequestConfig (
86+ api_url = api_url ,
87+ api_key = api_key ,
88+ disable_request_logging = disable_request_logging ,
89+ )
90+
91+ async def image_generation (self , params : ImageGenerationParams ) -> ImageGenerationResponse :
92+ path = "/ai/image_generation"
93+ resp = await AsyncRequest (
94+ config = self .config ,
95+ path = path ,
96+ params = cast (Dict [Any , Any ], params ), # type: ignore
97+ verb = "post" ,
98+ ).perform ()
99+ return resp
100+
101+
102+
103+
0 commit comments