Spaces:
Runtime error
Runtime error
Yuta Hayashi
commited on
Commit
•
3b9151f
1
Parent(s):
41ed45e
Add application file
Browse files- app.py +109 -0
- pyproject.toml +10 -0
- requirements.txt +2 -0
- uv.lock +0 -0
app.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from openai import OpenAI
|
3 |
+
from PIL import Image
|
4 |
+
import requests
|
5 |
+
from io import BytesIO
|
6 |
+
import logging
|
7 |
+
import re
|
8 |
+
import os
|
9 |
+
|
10 |
+
logging.basicConfig(level=logging.INFO)
|
11 |
+
|
12 |
+
|
13 |
+
def sanitize_filename(prompt):
|
14 |
+
sanitized = re.sub(r"[^\w\d-]", "_", prompt)
|
15 |
+
return sanitized[:50]
|
16 |
+
|
17 |
+
|
18 |
+
def generate_image(api_key, prompt, file_format):
|
19 |
+
if not api_key:
|
20 |
+
raise gr.Error("API key is required. Please enter your OpenAI API key.")
|
21 |
+
|
22 |
+
if not prompt or prompt.strip() == "":
|
23 |
+
raise gr.Error(
|
24 |
+
"Prompt cannot be empty. Please enter a description for the image you want to generate."
|
25 |
+
)
|
26 |
+
|
27 |
+
client = OpenAI(api_key=api_key)
|
28 |
+
|
29 |
+
try:
|
30 |
+
logging.info(f"Attempting to generate image with prompt: {prompt}")
|
31 |
+
response = client.images.generate(
|
32 |
+
model="dall-e-3",
|
33 |
+
prompt=prompt,
|
34 |
+
size="1024x1024",
|
35 |
+
quality="standard",
|
36 |
+
n=1,
|
37 |
+
)
|
38 |
+
|
39 |
+
logging.info("API call successful")
|
40 |
+
image_url = response.data[0].url
|
41 |
+
logging.info(f"Image URL received: {image_url}")
|
42 |
+
|
43 |
+
image_response = requests.get(image_url)
|
44 |
+
img = Image.open(BytesIO(image_response.content))
|
45 |
+
|
46 |
+
filename = f"{sanitize_filename(prompt)}.{file_format}"
|
47 |
+
img_path = os.path.join("output", filename)
|
48 |
+
os.makedirs("output", exist_ok=True)
|
49 |
+
img.save(img_path, format=file_format.upper())
|
50 |
+
|
51 |
+
logging.info(f"Image successfully generated and saved as {img_path}")
|
52 |
+
return img_path
|
53 |
+
except Exception as e:
|
54 |
+
logging.error(f"Error occurred: {str(e)}")
|
55 |
+
if "invalid_api_key" in str(e):
|
56 |
+
raise gr.Error(
|
57 |
+
"Invalid API key. Please check your OpenAI API key and try again."
|
58 |
+
)
|
59 |
+
else:
|
60 |
+
raise gr.Error(f"An error occurred: {str(e)}")
|
61 |
+
|
62 |
+
|
63 |
+
with gr.Blocks() as demo:
|
64 |
+
gr.Markdown("""
|
65 |
+
# DALL-E 3 Image Generation
|
66 |
+
|
67 |
+
Generate images using OpenAI's DALL-E 3 model.
|
68 |
+
|
69 |
+
**Important:** You need an OpenAI API key to use this application. You can obtain one from [https://platform.openai.com/api-keys](https://platform.openai.com/api-keys).
|
70 |
+
|
71 |
+
**Warning:** Your API key is sensitive information. This application does not store your key, but please handle it with care and do not share it with others.
|
72 |
+
|
73 |
+
**Please Note:**
|
74 |
+
1. Image generation typically takes around 30 seconds per image.
|
75 |
+
2. Each image generated will be charged at $0.040 per image (1024x1024 resolution, standard quality).
|
76 |
+
3. For the most up-to-date pricing information, please visit the [OpenAI Pricing Page](https://openai.com/api/pricing/).
|
77 |
+
4. We are not responsible for any failures in image generation. Use this application at your own risk.
|
78 |
+
""")
|
79 |
+
|
80 |
+
with gr.Row():
|
81 |
+
with gr.Column(scale=1):
|
82 |
+
api_key_input = gr.Textbox(label="OpenAI API Key", type="password")
|
83 |
+
prompt_input = gr.Textbox(
|
84 |
+
label="Enter your prompt", placeholder="e.g., a white siamese cat"
|
85 |
+
)
|
86 |
+
file_format = gr.Radio(["webp", "png"], label="File Format", value="webp")
|
87 |
+
submit_button = gr.Button("Generate Image")
|
88 |
+
|
89 |
+
with gr.Column(scale=1):
|
90 |
+
image_output = gr.Image(label="Generated Image")
|
91 |
+
|
92 |
+
submit_button.click(
|
93 |
+
generate_image,
|
94 |
+
inputs=[api_key_input, prompt_input, file_format],
|
95 |
+
outputs=image_output,
|
96 |
+
)
|
97 |
+
|
98 |
+
gr.Examples(
|
99 |
+
examples=[
|
100 |
+
"A white siamese cat",
|
101 |
+
"A futuristic cityscape at night",
|
102 |
+
"A serene mountain lake at sunrise",
|
103 |
+
"An abstract painting inspired by jazz music",
|
104 |
+
],
|
105 |
+
inputs=prompt_input,
|
106 |
+
)
|
107 |
+
|
108 |
+
if __name__ == "__main__":
|
109 |
+
demo.launch()
|
pyproject.toml
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[project]
|
2 |
+
name = "playground-dalle"
|
3 |
+
version = "0.1.0"
|
4 |
+
description = "Add your description here"
|
5 |
+
readme = "README.md"
|
6 |
+
requires-python = ">=3.12"
|
7 |
+
dependencies = [
|
8 |
+
"gradio>=4.42.0",
|
9 |
+
"openai>=1.43.0",
|
10 |
+
]
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio>=4.42.0
|
2 |
+
openai>=1.43.0
|
uv.lock
ADDED
The diff for this file is too large to render.
See raw diff
|
|