File size: 918 Bytes
2dd1349
 
 
 
 
3650e5a
 
2dd1349
 
 
39eabbd
2dd1349
39eabbd
2dd1349
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from openai import OpenAI
import requests

class Dalle3():
    @classmethod
    def __init_dalle__(cls, key):
        client = OpenAI(api_key=key)
        return client
        
    @classmethod
    def generate_image(cls, key: str, query: str, save_path: str = "downloaded_image.png"):
        """Generate an image based on a text query, save the image to the save_path"""
        client = cls.__init_dalle__(key)
        response = client.images.generate(
            model="dall-e-3",
            prompt=query,
            size="1024x1024",
            quality="standard",
            n=1,
        )
        image_url = response.data[0].url
        # Send a GET request to the URL
        response = requests.get(image_url)

        # Open a file in binary write mode and write the content of the response
        with open(save_path, "wb") as file:
            file.write(response.content)
        return save_path