File size: 2,025 Bytes
2733832
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import openai
import os

# List of popular styles
STYLES = [
    "Photorealistic", "Oil Painting", "Watercolor", "Anime", 
    "Studio Ghibli", "Black and White", "Polaroid", "Sketch", 
    "3D Render", "Pixel Art", "Cyberpunk", "Steampunk", 
    "Art Nouveau", "Pop Art", "Minimalist"
]

def enhance_prompt(prompt, style):
    openai.api_key = os.environ.get("OPENAI_API_KEY")
    
    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}'"
    
    response = openai.ChatCompletion.create(
        model="gpt-4",
        messages=[
            {"role": "system", "content": "You are an expert at creating detailed, vivid prompts for image generation."},
            {"role": "user", "content": enhanced_prompt_request}
        ]
    )
    
    return response.choices[0].message.content.strip()

def generate_image(api_key, prompt, style):
    openai.api_key = api_key
    
    enhanced_prompt = enhance_prompt(prompt, style)
    
    response = openai.Image.create(
        model="dall-e-3",
        prompt=enhanced_prompt,
        size="1024x1024",
        quality="standard",
        n=1
    )
    
    return response.data[0].url, enhanced_prompt

with gr.Blocks() as demo:
    gr.Markdown("# DALL-E 3 Image Generator")
    
    with gr.Row():
        with gr.Column(scale=1):
            api_key = gr.Textbox(label="OpenAI API Key", type="password")
            prompt = gr.Textbox(label="Prompt")
            style = gr.Dropdown(label="Style", choices=STYLES)
            generate_btn = gr.Button("Generate Image")
        
        with gr.Column(scale=1):
            image_output = gr.Image(label="Generated Image")
            enhanced_prompt_output = gr.Textbox(label="Enhanced Prompt")
    
    generate_btn.click(
        generate_image,
        inputs=[api_key, prompt, style],
        outputs=[image_output, enhanced_prompt_output]
    )

demo.launch()