Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai
|
3 |
+
import os
|
4 |
+
|
5 |
+
# List of popular styles
|
6 |
+
STYLES = [
|
7 |
+
"Photorealistic", "Oil Painting", "Watercolor", "Anime",
|
8 |
+
"Studio Ghibli", "Black and White", "Polaroid", "Sketch",
|
9 |
+
"3D Render", "Pixel Art", "Cyberpunk", "Steampunk",
|
10 |
+
"Art Nouveau", "Pop Art", "Minimalist"
|
11 |
+
]
|
12 |
+
|
13 |
+
def enhance_prompt(prompt, style):
|
14 |
+
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
15 |
+
|
16 |
+
enhanced_prompt_request = f"Enhance the following prompt for DALL-E 3 image generation in the style of {style}. Make it more detailed and vivid, while keeping the original intent: '{prompt}'"
|
17 |
+
|
18 |
+
response = openai.ChatCompletion.create(
|
19 |
+
model="gpt-4",
|
20 |
+
messages=[
|
21 |
+
{"role": "system", "content": "You are an expert at creating detailed, vivid prompts for image generation."},
|
22 |
+
{"role": "user", "content": enhanced_prompt_request}
|
23 |
+
]
|
24 |
+
)
|
25 |
+
|
26 |
+
return response.choices[0].message.content.strip()
|
27 |
+
|
28 |
+
def generate_image(api_key, prompt, style):
|
29 |
+
openai.api_key = api_key
|
30 |
+
|
31 |
+
enhanced_prompt = enhance_prompt(prompt, style)
|
32 |
+
|
33 |
+
response = openai.Image.create(
|
34 |
+
model="dall-e-3",
|
35 |
+
prompt=enhanced_prompt,
|
36 |
+
size="1024x1024",
|
37 |
+
quality="standard",
|
38 |
+
n=1
|
39 |
+
)
|
40 |
+
|
41 |
+
return response.data[0].url, enhanced_prompt
|
42 |
+
|
43 |
+
with gr.Blocks() as demo:
|
44 |
+
gr.Markdown("# DALL-E 3 Image Generator")
|
45 |
+
|
46 |
+
with gr.Row():
|
47 |
+
with gr.Column(scale=1):
|
48 |
+
api_key = gr.Textbox(label="OpenAI API Key", type="password")
|
49 |
+
prompt = gr.Textbox(label="Prompt")
|
50 |
+
style = gr.Dropdown(label="Style", choices=STYLES)
|
51 |
+
generate_btn = gr.Button("Generate Image")
|
52 |
+
|
53 |
+
with gr.Column(scale=1):
|
54 |
+
image_output = gr.Image(label="Generated Image")
|
55 |
+
enhanced_prompt_output = gr.Textbox(label="Enhanced Prompt")
|
56 |
+
|
57 |
+
generate_btn.click(
|
58 |
+
generate_image,
|
59 |
+
inputs=[api_key, prompt, style],
|
60 |
+
outputs=[image_output, enhanced_prompt_output]
|
61 |
+
)
|
62 |
+
|
63 |
+
demo.launch()
|