Skip to content

Commit a41e543

Browse files
authored
Merge pull request #143 from Runware/openai-models
New openai models
2 parents ec6b0e5 + 8990185 commit a41e543

8 files changed

Lines changed: 314 additions & 18 deletions

File tree

README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,82 @@ async def main() -> None:
429429
```
430430
This example demonstrates how to configure and use a ControlNet to enhance the image inference process.
431431

432+
### Generating Images with OpenAI Models (DALL-E 2 & DALL-E 3)
433+
434+
The Runware SDK supports OpenAI's DALL-E 2 and DALL-E 3 models for image generation. These models offer high-quality image generation with various configuration options.
435+
436+
#### DALL-E 2
437+
438+
```python
439+
from runware import Runware, IImageInference, IOpenAIProviderSettings
440+
441+
async def main() -> None:
442+
runware = Runware(api_key=RUNWARE_API_KEY)
443+
await runware.connect()
444+
445+
# DALL-E 2 configuration
446+
provider_settings = IOpenAIProviderSettings(
447+
quality="high",
448+
background="transparent" # Optional: for transparent backgrounds
449+
)
450+
451+
request_image = IImageInference(
452+
positivePrompt="A cute cartoon robot character",
453+
model="openai:1@1", # DALL-E 2 model identifier
454+
width=1024,
455+
height=1024,
456+
numberResults=1,
457+
outputFormat="PNG",
458+
includeCost=True,
459+
providerSettings=provider_settings
460+
)
461+
462+
images = await runware.imageInference(requestImage=request_image)
463+
for image in images:
464+
print(f"Image URL: {image.imageURL}")
465+
```
466+
467+
#### DALL-E 3
468+
469+
```python
470+
from runware import Runware, IImageInference, IOpenAIProviderSettings
471+
472+
async def main() -> None:
473+
runware = Runware(api_key=RUNWARE_API_KEY)
474+
await runware.connect()
475+
476+
# DALL-E 3 with HD quality
477+
provider_settings = IOpenAIProviderSettings(
478+
quality="hd" # Options: "hd" or "standard"
479+
)
480+
481+
request_image = IImageInference(
482+
positivePrompt="A futuristic city with flying cars, highly detailed",
483+
model="openai:2@3", # DALL-E 3 model identifier
484+
width=1024,
485+
height=1024,
486+
numberResults=1,
487+
outputFormat="PNG",
488+
includeCost=True,
489+
providerSettings=provider_settings
490+
)
491+
492+
images = await runware.imageInference(requestImage=request_image)
493+
for image in images:
494+
print(f"Image URL: {image.imageURL}")
495+
```
496+
497+
**OpenAI Provider Settings:**
498+
- `quality`: Image quality setting
499+
- DALL-E 2: `"high"` (recommended)
500+
- DALL-E 3: `"hd"` or `"standard"`
501+
- `background`: (DALL-E 2 only) Set to `"transparent"` for transparent backgrounds
502+
- `style`: (Optional) Additional style parameters
503+
504+
**Model Identifiers:**
505+
- DALL-E 2: `"openai:1@1"`
506+
- DALL-E 3: `"openai:2@3"`
507+
432508

433509
### Inferencing Video Models
434510

examples/openai/dall-e2.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import asyncio
2+
import os
3+
from dotenv import load_dotenv
4+
from runware import Runware, IImageInference, IOpenAIProviderSettings, RunwareAPIError
5+
6+
load_dotenv()
7+
8+
RUNWARE_API_KEY = os.environ.get("RUNWARE_API_KEY")
9+
10+
11+
async def main() -> None:
12+
runware = Runware(api_key=RUNWARE_API_KEY)
13+
await runware.connect()
14+
15+
# DALL-E 2 with standard settings
16+
provider_settings = IOpenAIProviderSettings(
17+
quality="high"
18+
)
19+
20+
request_image = IImageInference(
21+
positivePrompt="A serene landscape with mountains and a crystal clear lake at sunset",
22+
model="openai:1@1",
23+
width=1024,
24+
height=1024,
25+
numberResults=1,
26+
outputFormat="PNG",
27+
outputQuality=95,
28+
includeCost=True,
29+
providerSettings=provider_settings
30+
)
31+
32+
try:
33+
images = await runware.imageInference(requestImage=request_image)
34+
for image in images:
35+
print(f"Image URL: {image.imageURL}")
36+
if image.cost:
37+
print(f"Cost: ${image.cost}")
38+
except RunwareAPIError as e:
39+
print(f"API Error: {e}")
40+
print(f"Error Code: {e.code}")
41+
except Exception as e:
42+
print(f"Unexpected Error: {e}")
43+
44+
# DALL-E 2 with transparent background
45+
provider_settings_transparent = IOpenAIProviderSettings(
46+
quality="high",
47+
background="transparent"
48+
)
49+
50+
request_image_transparent = IImageInference(
51+
positivePrompt="A cute cartoon robot character with big eyes, isolated on transparent background",
52+
model="openai:1@1",
53+
width=1024,
54+
height=1024,
55+
numberResults=1,
56+
outputFormat="PNG",
57+
outputQuality=95,
58+
includeCost=True,
59+
providerSettings=provider_settings_transparent
60+
)
61+
62+
try:
63+
images = await runware.imageInference(requestImage=request_image_transparent)
64+
for image in images:
65+
print(f"Image URL (transparent): {image.imageURL}")
66+
if image.cost:
67+
print(f"Cost: ${image.cost}")
68+
except RunwareAPIError as e:
69+
print(f"API Error: {e}")
70+
print(f"Error Code: {e.code}")
71+
except Exception as e:
72+
print(f"Unexpected Error: {e}")
73+
74+
75+
if __name__ == "__main__":
76+
asyncio.run(main())

examples/openai/dall-e3.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import asyncio
2+
import os
3+
4+
from dotenv import load_dotenv
5+
from runware import Runware, IImageInference, IOpenAIProviderSettings, RunwareAPIError
6+
7+
load_dotenv()
8+
9+
RUNWARE_API_KEY = os.environ.get("RUNWARE_API_KEY")
10+
11+
12+
async def main() -> None:
13+
runware = Runware(api_key=RUNWARE_API_KEY)
14+
await runware.connect()
15+
16+
# DALL-E 3 with HD quality
17+
provider_settings_hd = IOpenAIProviderSettings(
18+
quality="hd"
19+
)
20+
21+
request_image_hd = IImageInference(
22+
positivePrompt="A futuristic city with flying cars and neon lights, cyberpunk aesthetic, highly detailed",
23+
model="openai:2@3",
24+
width=1024,
25+
height=1024,
26+
numberResults=1,
27+
outputFormat="PNG",
28+
outputQuality=95,
29+
includeCost=True,
30+
providerSettings=provider_settings_hd
31+
)
32+
33+
try:
34+
images = await runware.imageInference(requestImage=request_image_hd)
35+
for image in images:
36+
print(f"Image URL (HD): {image.imageURL}")
37+
if image.cost:
38+
print(f"Cost: ${image.cost}")
39+
except RunwareAPIError as e:
40+
print(f"API Error: {e}")
41+
print(f"Error Code: {e.code}")
42+
except Exception as e:
43+
print(f"Unexpected Error: {e}")
44+
45+
# DALL-E 3 with standard quality
46+
provider_settings_standard = IOpenAIProviderSettings(
47+
quality="standard",
48+
style="ghibli style"
49+
)
50+
51+
request_image_standard = IImageInference(
52+
positivePrompt="A whimsical tea party in a magical forest with talking animals",
53+
model="openai:2@3",
54+
width=1024,
55+
height=1024,
56+
numberResults=1,
57+
outputFormat="PNG",
58+
outputQuality=95,
59+
includeCost=True,
60+
providerSettings=provider_settings_standard
61+
)
62+
63+
try:
64+
images = await runware.imageInference(requestImage=request_image_standard)
65+
for image in images:
66+
print(f"Image URL (Standard): {image.imageURL}")
67+
if image.cost:
68+
print(f"Cost: ${image.cost}")
69+
except RunwareAPIError as e:
70+
print(f"API Error: {e}")
71+
print(f"Error Code: {e.code}")
72+
except Exception as e:
73+
print(f"Unexpected Error: {e}")
74+
75+
76+
if __name__ == "__main__":
77+
asyncio.run(main())

examples/openai/image_1.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import asyncio
2+
import os
3+
4+
from dotenv import load_dotenv
5+
from runware import Runware, IImageInference, IOpenAIProviderSettings, RunwareAPIError
6+
7+
load_dotenv()
8+
9+
RUNWARE_API_KEY = os.environ.get("RUNWARE_API_KEY")
10+
11+
12+
async def main() -> None:
13+
runware = Runware(api_key=RUNWARE_API_KEY)
14+
await runware.connect()
15+
16+
provider_settings_hd = IOpenAIProviderSettings(
17+
quality="high",
18+
style="cyberpunk"
19+
)
20+
21+
request_image_hd = IImageInference(
22+
positivePrompt="A futuristic city with flying cars and neon lights, cyberpunk aesthetic, highly detailed",
23+
model="openai:1@1",
24+
width=1024,
25+
height=1024,
26+
numberResults=1,
27+
outputFormat="PNG",
28+
outputQuality=95,
29+
includeCost=True,
30+
providerSettings=provider_settings_hd
31+
)
32+
33+
try:
34+
images = await runware.imageInference(requestImage=request_image_hd)
35+
for image in images:
36+
print(f"Image URL (high): {image.imageURL}")
37+
if image.cost:
38+
print(f"Cost: ${image.cost}")
39+
except RunwareAPIError as e:
40+
print(f"API Error: {e}")
41+
print(f"Error Code: {e.code}")
42+
except Exception as e:
43+
print(f"Unexpected Error: {e}")
44+
45+
if __name__ == "__main__":
46+
asyncio.run(main())

runware/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
from .async_retry import *
77

88
__all__ = ["Runware"]
9-
__version__ = "0.4.17"
9+
__version__ = "0.4.18"

runware/base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,11 @@ async def imageInference(
459459

460460
if requestImage.outputQuality:
461461
request_object["outputQuality"] = requestImage.outputQuality
462+
463+
if requestImage.providerSettings:
464+
provider_data = requestImage.providerSettings.to_request_dict()
465+
request_object.update(provider_data)
466+
462467
return await asyncRetry(
463468
lambda: self._requestImages(
464469
request_object=request_object,

runware/types.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,37 @@ class IAdvancedFeatures:
397397
fluxKontext: Optional[IFluxKontext] = None
398398

399399

400+
class SerializableMixin:
401+
def serialize(self) -> Dict[str, Any]:
402+
return {k: v for k, v in asdict(self).items()
403+
if v is not None and not k.startswith('_')}
404+
405+
@dataclass
406+
class BaseProviderSettings(SerializableMixin, ABC):
407+
@property
408+
@abstractmethod
409+
def provider_key(self) -> str:
410+
pass
411+
412+
def to_request_dict(self) -> Dict[str, Any]:
413+
data = self.serialize()
414+
if data:
415+
return {self.provider_key: data}
416+
return {}
417+
418+
@dataclass
419+
class IOpenAIProviderSettings(BaseProviderSettings):
420+
quality: Optional[str] = None
421+
background: Optional[str] = None
422+
style: Optional[str] = None
423+
424+
@property
425+
def provider_key(self) -> str:
426+
return "openai"
427+
428+
ImageProviderSettings = IOpenAIProviderSettings
429+
430+
400431
@dataclass
401432
class IImageInference:
402433
positivePrompt: str
@@ -437,6 +468,7 @@ class IImageInference:
437468
ipAdapters: Optional[List[IIpAdapter]] = field(default_factory=list)
438469
referenceImages: Optional[List[Union[str, File]]] = field(default_factory=list)
439470
acePlusPlus: Optional[IAcePlusPlus] = None
471+
providerSettings: Optional[ImageProviderSettings] = None
440472
extraArgs: Optional[Dict[str, Any]] = field(default_factory=dict)
441473

442474

@@ -589,24 +621,8 @@ class IFrameImage:
589621
frame: Optional[Union[Literal["first", "last"], int]] = None
590622

591623

592-
class SerializableMixin:
593-
def serialize(self) -> Dict[str, Any]:
594-
return {k: v for k, v in asdict(self).items()
595-
if v is not None and not k.startswith('_')}
596624

597625

598-
@dataclass
599-
class BaseProviderSettings(SerializableMixin, ABC):
600-
@property
601-
@abstractmethod
602-
def provider_key(self) -> str:
603-
pass
604-
605-
def to_request_dict(self) -> Dict[str, Any]:
606-
data = self.serialize()
607-
if data:
608-
return {self.provider_key: data}
609-
return {}
610626

611627

612628
@dataclass

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
setup(
77
name="runware",
88
license="MIT",
9-
version="0.4.17",
9+
version="0.4.18",
1010
author="Runware Inc.",
1111
author_email="python.sdk@runware.ai",
1212
description="The Python Runware SDK is used to run image inference with the Runware API, powered by the Runware inference platform. It can be used to generate images with text-to-image and image-to-image. It also allows the use of an existing gallery of models or selecting any model or LoRA from the CivitAI gallery. The API also supports upscaling, background removal, inpainting and outpainting, and a series of other ControlNet models.",

0 commit comments

Comments
 (0)