Spaces:
Runtime error
Runtime error
import os | |
import gradio as gr | |
import openai | |
import requests | |
from PIL import Image | |
import io | |
import numpy as np | |
openai.api_key = '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' # your api key | |
def DALLE(user_input): | |
prompt = user_input | |
response = prompt | |
# Create the DALL-E image using the OpenAI API | |
data = { | |
"model": "image-alpha-001", | |
"prompt": f"{prompt}:::", | |
"n": 1, | |
"size": "1024x1024", | |
"response_format": "url" | |
} | |
headers = { | |
"Content-Type": "application/json", | |
"Authorization": f"Bearer {openai.api_key}" | |
} | |
response = requests.post("https://api.openai.com/v1/images/generations", json=data, headers=headers) | |
response.raise_for_status() | |
# Convert the image to a format that Gradio can display | |
img_bytes = io.BytesIO(requests.get(response.json()["data"][0]["url"]).content) | |
img = Image.open(img_bytes) | |
img_arr = np.array(img) | |
return img_arr | |
iface = gr.Interface(fn=DALLE, inputs="text", outputs="image", title="bhAI") | |
iface.launch() | |