Tobias Geisler
commited on
Commit
路
668a53e
1
Parent(s):
c1e51fb
better magic prompts
Browse files
app.py
CHANGED
@@ -17,12 +17,52 @@ if APP_PASSWORD is None:
|
|
17 |
|
18 |
GENERATION_TIMEOUT = float(os.getenv("GENERATION_TIMEOUT", 60))
|
19 |
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
try:
|
22 |
response = client.chat.completions.create(
|
23 |
model="gpt-4o-mini",
|
24 |
messages=[
|
25 |
-
{"role": "system", "content":
|
26 |
{"role": "user", "content": original_prompt}
|
27 |
],
|
28 |
max_tokens=300
|
@@ -33,26 +73,29 @@ def enhance_prompt(original_prompt):
|
|
33 |
print("Ein Fehler ist beim Verbessern des Prompts aufgetreten:", e)
|
34 |
return original_prompt
|
35 |
|
36 |
-
def generate_image(prompt, use_magic_prompt, style, password, user_id, last_generation_time):
|
37 |
-
print(f"\nGenerierungsversuch von User {user_id} mit Prompt:\n{prompt}")
|
|
|
|
|
|
|
38 |
|
39 |
if password != APP_PASSWORD:
|
40 |
print("Generierung abgebrochen: Falsches Passwort.")
|
41 |
return "Falsches Passwort. Bitte versuche es erneut.", None, last_generation_time
|
42 |
|
43 |
current_time = time.time()
|
44 |
-
if current_time - last_generation_time < GENERATION_TIMEOUT:
|
45 |
remaining_time = max(0, int(GENERATION_TIMEOUT - (current_time - last_generation_time)))
|
46 |
print("Generierung abgebrochen: Cooldown noch nicht abgelaufen")
|
47 |
return f"Bitte warte noch {remaining_time} Sekunden bis zur n盲chsten Bildgenerierungen.\n\nNutze die Wartezeit, um deinen Prompt zu verfeinern. 😉", None, last_generation_time
|
48 |
|
49 |
if use_magic_prompt:
|
50 |
-
prompt = enhance_prompt(prompt)
|
51 |
print("\nFinaler Prompt:", prompt)
|
52 |
|
53 |
try:
|
54 |
response = client.images.generate(
|
55 |
-
model=
|
56 |
prompt=prompt,
|
57 |
size="1024x1024",
|
58 |
quality="standard",
|
@@ -63,7 +106,7 @@ def generate_image(prompt, use_magic_prompt, style, password, user_id, last_gene
|
|
63 |
image_url = response.data[0].url
|
64 |
|
65 |
# Update the last generation time for this user
|
66 |
-
last_generation_time = current_time
|
67 |
|
68 |
return prompt, image_url, last_generation_time
|
69 |
except Exception as e:
|
@@ -106,9 +149,9 @@ with gr.Blocks() as demo:
|
|
106 |
user_id = gr.State(generate_user_id)
|
107 |
last_generation_time = gr.State(0)
|
108 |
|
109 |
-
gr.Markdown("# codora DALL-E
|
110 |
welcome_message = gr.Markdown("Willkommen!")
|
111 |
-
gr.Markdown(f"Gib einen Bildprompt ein und verwende optional die magische Prompt-Funktion, um ihn zu verbessern.
|
112 |
|
113 |
with gr.Row():
|
114 |
with gr.Column(scale=1):
|
@@ -119,6 +162,11 @@ with gr.Blocks() as demo:
|
|
119 |
choices=["vivid", "natural"],
|
120 |
value="vivid"
|
121 |
)
|
|
|
|
|
|
|
|
|
|
|
122 |
password = gr.Textbox(label="App Passwort", type="password")
|
123 |
generate_button = gr.Button("Bild generieren")
|
124 |
|
@@ -134,7 +182,7 @@ with gr.Blocks() as demo:
|
|
134 |
|
135 |
generate_button.click(
|
136 |
fn=generate_image,
|
137 |
-
inputs=[prompt, use_magic_prompt, style, password, user_id, last_generation_time],
|
138 |
outputs=[final_prompt, generated_image, last_generation_time]
|
139 |
)
|
140 |
|
|
|
17 |
|
18 |
GENERATION_TIMEOUT = float(os.getenv("GENERATION_TIMEOUT", 60))
|
19 |
|
20 |
+
MAGIC_PROMPTS = [
|
21 |
+
"""You are an expert DALL-E 2 image generation prompt optimizer. Your task is to take the user's initial prompt idea and enhance it to produce the best possible results from DALL-E 2. Follow these steps:
|
22 |
+
Analyze the user's base prompt.
|
23 |
+
Expand the prompt by adding specific details about:
|
24 |
+
Subject/main elements
|
25 |
+
Setting/background
|
26 |
+
Lighting and atmosphere
|
27 |
+
Color palette
|
28 |
+
Artistic style (e.g. photorealistic, oil painting, digital art)
|
29 |
+
Composition and perspective
|
30 |
+
Incorporate descriptive adjectives and evocative language.
|
31 |
+
Add relevant artistic/technical terms (e.g. macro shot, fisheye lens, chiaroscuro).
|
32 |
+
Include quality boosters like "highly detailed", "award-winning", "stunning".
|
33 |
+
Specify image type if relevant (e.g. digital illustration, 35mm photograph).
|
34 |
+
Mention any desired emotions or moods.
|
35 |
+
Avoid negative language - focus on what should be included rather than excluded.
|
36 |
+
Keep text simple and minimal if text is required in the image.
|
37 |
+
Aim for a prompt length of 40-60 words.
|
38 |
+
Format the final prompt clearly, using proper punctuation and capitalization.
|
39 |
+
Only return the improved prompt without any additional comments or messages.""",
|
40 |
+
"""You are an expert DALL-E 3 image generation prompt optimizer. Enhance and refine anything the user sends you as an image generation prompt for DALL-E 3:
|
41 |
+
|
42 |
+
Provide an improved version of the user message by following these guidelines:
|
43 |
+
Adds more specific details about the scene, subjects, and atmosphere
|
44 |
+
Incorporates precise descriptors for colors, textures, and lighting
|
45 |
+
Specifies the artistic style or medium (e.g., oil painting, digital art, photography)
|
46 |
+
Includes relevant compositional elements (foreground, background, perspective)
|
47 |
+
Adds any missing context or setting information
|
48 |
+
Removes any redundant or vague language
|
49 |
+
Ensures the prompt is coherent and follows a logical structure
|
50 |
+
Incorporates relevant technical terms or jargon related to art or photography
|
51 |
+
Suggests any additional elements that could enhance the overall image
|
52 |
+
Optimizes the prompt length for DALL-E 3's capabilities (aim for 40-60 words)
|
53 |
+
Only return the improved prompt without any additional comments or messages.
|
54 |
+
""",
|
55 |
+
"Verbessere den folgenden Bildgenerierungsprompt, um ihn detaillierter und kreativer zu machen."
|
56 |
+
]
|
57 |
+
|
58 |
+
def enhance_prompt(original_prompt, img_model="dall-e-2"):
|
59 |
+
system_message = MAGIC_PROMPTS[0] if img_model == "dall-e-2" else MAGIC_PROMPTS[1]
|
60 |
+
|
61 |
try:
|
62 |
response = client.chat.completions.create(
|
63 |
model="gpt-4o-mini",
|
64 |
messages=[
|
65 |
+
{"role": "system", "content": system_message},
|
66 |
{"role": "user", "content": original_prompt}
|
67 |
],
|
68 |
max_tokens=300
|
|
|
73 |
print("Ein Fehler ist beim Verbessern des Prompts aufgetreten:", e)
|
74 |
return original_prompt
|
75 |
|
76 |
+
def generate_image(prompt, use_magic_prompt, style, password, user_id, last_generation_time, model):
|
77 |
+
print(f"\nGenerierungsversuch von User {user_id} mit Modell {model} mit Prompt:\n{prompt}")
|
78 |
+
|
79 |
+
if prompt=="":
|
80 |
+
return "Bitte gib einen Bildprompt ein.", None, last_generation_time
|
81 |
|
82 |
if password != APP_PASSWORD:
|
83 |
print("Generierung abgebrochen: Falsches Passwort.")
|
84 |
return "Falsches Passwort. Bitte versuche es erneut.", None, last_generation_time
|
85 |
|
86 |
current_time = time.time()
|
87 |
+
if model == "dall-e-3" and current_time - last_generation_time < GENERATION_TIMEOUT:
|
88 |
remaining_time = max(0, int(GENERATION_TIMEOUT - (current_time - last_generation_time)))
|
89 |
print("Generierung abgebrochen: Cooldown noch nicht abgelaufen")
|
90 |
return f"Bitte warte noch {remaining_time} Sekunden bis zur n盲chsten Bildgenerierungen.\n\nNutze die Wartezeit, um deinen Prompt zu verfeinern. 😉", None, last_generation_time
|
91 |
|
92 |
if use_magic_prompt:
|
93 |
+
prompt = enhance_prompt(prompt, model)
|
94 |
print("\nFinaler Prompt:", prompt)
|
95 |
|
96 |
try:
|
97 |
response = client.images.generate(
|
98 |
+
model=model,
|
99 |
prompt=prompt,
|
100 |
size="1024x1024",
|
101 |
quality="standard",
|
|
|
106 |
image_url = response.data[0].url
|
107 |
|
108 |
# Update the last generation time for this user
|
109 |
+
last_generation_time = current_time if model == "dall-e-3" else last_generation_time
|
110 |
|
111 |
return prompt, image_url, last_generation_time
|
112 |
except Exception as e:
|
|
|
149 |
user_id = gr.State(generate_user_id)
|
150 |
last_generation_time = gr.State(0)
|
151 |
|
152 |
+
gr.Markdown("# codora DALL-E Bildgenerator")
|
153 |
welcome_message = gr.Markdown("Willkommen!")
|
154 |
+
gr.Markdown(f"Gib einen Bildprompt ein und verwende optional die magische Prompt-Funktion, um ihn zu verbessern. Mit DALL路E 3 kannst du ein Bild alle {GENERATION_TIMEOUT} Sekunden generieren. Mit DALL路E 2 kannst du so viele Bilder generieren, wie du m枚chtest.")
|
155 |
|
156 |
with gr.Row():
|
157 |
with gr.Column(scale=1):
|
|
|
162 |
choices=["vivid", "natural"],
|
163 |
value="vivid"
|
164 |
)
|
165 |
+
model = gr.Radio(
|
166 |
+
label="Modell",
|
167 |
+
choices=["dall-e-2", "dall-e-3"],
|
168 |
+
value="dall-e-2"
|
169 |
+
)
|
170 |
password = gr.Textbox(label="App Passwort", type="password")
|
171 |
generate_button = gr.Button("Bild generieren")
|
172 |
|
|
|
182 |
|
183 |
generate_button.click(
|
184 |
fn=generate_image,
|
185 |
+
inputs=[prompt, use_magic_prompt, style, password, user_id, last_generation_time, model],
|
186 |
outputs=[final_prompt, generated_image, last_generation_time]
|
187 |
)
|
188 |
|