File size: 1,083 Bytes
8cb9766
 
 
 
 
3fe9359
20202fe
8cb9766
a92a8ca
b072ef1
 
 
 
 
a92a8ca
8cb9766
 
 
3073b66
8cb9766
 
 
 
 
 
 
 
 
 
 
 
 
 
20202fe
 
 
 
 
 
7db76b3
8cb9766
20202fe
8cb9766
 
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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()