Displaying a generated image from openai
I’m having trouble displaying image that I’m generating from openai. I get the URL back and if I print it I can see the image but when I try to pass it to the image component it never displays! I can’t tell what I’m doing wrong. Here’s code snippet:import openai
import gradio as gr
from PIL import Image

output= gr.Image()


def generateImage (prompt):
    response = openai.images.generate(
        model="dall-e-3",
        prompt="People of color doing climate jobs",
        size="1024x1024",
        quality="standard",
        n=1,
    )
    return  response.data[0].url

def handlePrompt(prompt):
    urlString = generateImage(prompt)
    print(urlString)
    output.value = urlString

   
with gr.Blocks() as demo:
  
    gr.HTML( "<H1> Unified Ground Image generator</H1>" )

  
    with gr.Row():
       
        input = gr.Textbox(label="Enter Prompt")
        output= gr.Image("images/ug_logo.jpg", height=200, width=200, type="pil")
    generate_btn = gr.Button("Generate Image")
    generate_btn.click(fn=handlePrompt, inputs=input, outputs=output)

demo.launch(share=True)
2024-02-05T21:43:14Z
Hi@unifiedGround,here is the complete example, you missed the steps of fetching the image and rendering a PIL Imageimport openai
from PIL import Image
import requests
import gradio as gr

OPEN_AI_KEY = "......"
openai = openai.OpenAI(api_key=OPEN_AI_KEY)


def generateImage(prompt):
    response = openai.images.generate(
        model="dall-e-3",
        prompt=prompt,
        size="1024x1024",
        quality="standard",
        n=1,
    )
    return response.data[0].url


def handlePrompt(prompt):
    urlString = generateImage(prompt)
    print(urlString)
    image = Image.open(requests.get(urlString, stream=True).raw)
    return image


with gr.Blocks() as demo:
    gr.HTML("<H1> Unified Ground Image generator</H1>")
    with gr.Row():
        input = gr.Textbox(label="Enter Prompt")
        output = gr.Image()
    generate_btn = gr.Button("Generate Image")
    generate_btn.click(fn=handlePrompt, inputs=input, outputs=output)

demo.launch()
2024-02-05T21:48:52Z
Thanks!!! It works. My Climate works of color image generator is going to become a reality!
2024-02-06T09:49:44Z
This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.