mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-18 14:34:23 +01:00
feat(ImageGen): add DALL-E support
This commit is contained in:
@@ -4,6 +4,8 @@ import os.path
|
|||||||
from PIL import Image
|
from PIL import Image
|
||||||
from config import Config
|
from config import Config
|
||||||
import uuid
|
import uuid
|
||||||
|
import openai
|
||||||
|
from base64 import b64decode
|
||||||
|
|
||||||
cfg = Config()
|
cfg = Config()
|
||||||
|
|
||||||
@@ -13,16 +15,36 @@ API_URL = "https://api-inference.huggingface.co/models/CompVis/stable-diffusion-
|
|||||||
headers = {"Authorization": "Bearer " + cfg.huggingface_api_token}
|
headers = {"Authorization": "Bearer " + cfg.huggingface_api_token}
|
||||||
|
|
||||||
def generate_image(prompt):
|
def generate_image(prompt):
|
||||||
|
|
||||||
|
filename = str(uuid.uuid4()) + ".jpg"
|
||||||
|
|
||||||
|
# DALL-E
|
||||||
|
openai.api_key = cfg.openai_api_key
|
||||||
|
|
||||||
|
response = openai.Image.create(
|
||||||
|
prompt=prompt,
|
||||||
|
n=1,
|
||||||
|
size="256x256",
|
||||||
|
response_format="b64_json",
|
||||||
|
)
|
||||||
|
|
||||||
|
print("Image Generated for prompt:" + prompt)
|
||||||
|
print(response["data"][0]["b64_json"][:50])
|
||||||
|
|
||||||
|
image_data = b64decode(response["data"][0]["b64_json"])
|
||||||
|
with open(working_directory + "/" + filename, mode="wb") as png:
|
||||||
|
png.write(image_data)
|
||||||
|
|
||||||
|
return "Saved to disk:" + filename
|
||||||
|
|
||||||
|
# STABLE DIFFUSION
|
||||||
response = requests.post(API_URL, headers=headers, json={
|
response = requests.post(API_URL, headers=headers, json={
|
||||||
"inputs": prompt,
|
"inputs": prompt,
|
||||||
})
|
})
|
||||||
image = Image.open(io.BytesIO(response.content))
|
image = Image.open(io.BytesIO(response.content))
|
||||||
print("Image Generated for prompt:" + prompt)
|
print("Image Generated for prompt:" + prompt)
|
||||||
|
|
||||||
filename = str(uuid.uuid4()) + ".jpg"
|
|
||||||
|
|
||||||
image.save(os.path.join(working_directory, filename))
|
image.save(os.path.join(working_directory, filename))
|
||||||
|
|
||||||
print("Saved to disk:" + filename)
|
print("Saved to disk:" + filename)
|
||||||
|
|
||||||
return str("Image " + filename + " saved to disk for prompt: " + prompt)
|
return str("Image " + filename + " saved to disk for prompt: " + prompt)
|
||||||
|
|||||||
Reference in New Issue
Block a user