Spaces:
Runtime error
Runtime error
import gradio as gr | |
import openai | |
import urllib.request | |
from PIL import Image | |
import os | |
import nltk | |
#nltk.download('punkt') | |
def generate_image(api_key, prompt): | |
try: | |
if not api_key: | |
raise ValueError("API Key is required.") | |
except ValueError as e: | |
return e | |
openai.api_key = api_key | |
response = openai.Image.create( | |
prompt=prompt, | |
n=1, | |
size="256x256" | |
) | |
image_url = response['data'][0]['url'] | |
# Open the URL image, resize it to 512x512 and return it | |
with urllib.request.urlopen(image_url) as url: | |
with open('temp.jpg', 'wb') as f: | |
f.write(url.read()) | |
img = Image.open('temp.jpg') | |
return img | |
iface = gr.Interface( | |
fn=generate_image, | |
inputs=[ | |
gr.inputs.Textbox(lines=1, label="API Key", type="password"), | |
gr.inputs.Textbox(lines=1, label="Prompt") | |
], | |
outputs=gr.outputs.Image(type="pil"), | |
title="DALL-E Image Generator", | |
description="Enter your API key and a prompt to generate an image from DALL-E." | |
) | |
iface.launch() | |