Spaces:
Build error
Build error
File size: 1,712 Bytes
8b98084 3b5d9c4 2d1ecce 100f69c 328e7d1 8b98084 100f69c 8b98084 328e7d1 8b98084 328e7d1 8b98084 328e7d1 100f69c 8b98084 3b5d9c4 100f69c 3b5d9c4 | 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | # app.py
import gradio as gr
import os
from PIL import Image
import requests
from io import BytesIO
from openai import OpenAI
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Set up OpenAI API key
api_key = os.getenv("OPENAI_API_KEY") # Ensure your OpenAI API key is stored in the .env file
client = OpenAI(api_key=api_key)
from io import BytesIO
# Initialize OpenAI client
client = OpenAI()
def generate_image(input_text):
# Call OpenAI's DALL·E 3 model for image generation
response = client.images.generate(
model="dall-e-2",
prompt=input_text,
size="256x256",
quality="standard",
n=1
)
# Extract the image URL from the response
image_url = response.data[0].url
# Fetch the image from the URL
image_response = requests.get(image_url)
img = Image.open(BytesIO(image_response.content))
return img
# Create a Gradio interface
def create_interface():
with gr.Blocks() as demo:
gr.Markdown("# Text to Image Generation App with OpenAI") # Title
with gr.Row():
# Textbox for user input
input_text = gr.Textbox(label="Enter your image description:")
# Button to generate image
generate_btn = gr.Button("Generate Image")
# Image output
image_output = gr.Image(label="Generated Image", type="pil")
# Define the interaction: When the button is clicked, generate the image
generate_btn.click(generate_image, inputs=input_text, outputs=image_output)
return demo
# Launch the Gradio app
if __name__ == "__main__":
demo = create_interface()
demo.launch()
|