Spaces:
Sleeping
Sleeping
Tobias Geisler
commited on
Commit
·
8a8d86e
1
Parent(s):
4e0988e
app init
Browse files- .gitignore +59 -0
- app.py +129 -0
- requirements.txt +0 -0
.gitignore
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Virtual environment
|
2 |
+
gradio-dalle3-env/
|
3 |
+
|
4 |
+
# Python cache files
|
5 |
+
__pycache__/
|
6 |
+
*.py[cod]
|
7 |
+
*$py.class
|
8 |
+
|
9 |
+
# C extensions
|
10 |
+
*.so
|
11 |
+
|
12 |
+
# Distribution / packaging
|
13 |
+
.Python
|
14 |
+
build/
|
15 |
+
develop-eggs/
|
16 |
+
dist/
|
17 |
+
downloads/
|
18 |
+
eggs/
|
19 |
+
.eggs/
|
20 |
+
lib/
|
21 |
+
lib64/
|
22 |
+
parts/
|
23 |
+
sdist/
|
24 |
+
var/
|
25 |
+
wheels/
|
26 |
+
*.egg-info/
|
27 |
+
.installed.cfg
|
28 |
+
*.egg
|
29 |
+
|
30 |
+
# PyInstaller
|
31 |
+
*.manifest
|
32 |
+
*.spec
|
33 |
+
|
34 |
+
# Jupyter Notebook
|
35 |
+
.ipynb_checkpoints
|
36 |
+
|
37 |
+
# PyCharm
|
38 |
+
.idea/
|
39 |
+
|
40 |
+
# VS Code
|
41 |
+
.vscode/
|
42 |
+
|
43 |
+
# Environment variables
|
44 |
+
.env
|
45 |
+
|
46 |
+
# OS generated files
|
47 |
+
.DS_Store
|
48 |
+
.DS_Store?
|
49 |
+
._*
|
50 |
+
.Spotlight-V100
|
51 |
+
.Trashes
|
52 |
+
ehthumbs.db
|
53 |
+
Thumbs.db
|
54 |
+
|
55 |
+
# Gradio specific
|
56 |
+
flagged/
|
57 |
+
|
58 |
+
# Logs
|
59 |
+
*.log
|
app.py
ADDED
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import time
|
3 |
+
from collections import defaultdict
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
from openai import OpenAI
|
6 |
+
import os
|
7 |
+
|
8 |
+
load_dotenv()
|
9 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
10 |
+
generation_timeout = float(os.getenv("GENERATION_TIMEOUT"))
|
11 |
+
|
12 |
+
if client.api_key is None:
|
13 |
+
raise ValueError("Die Umgebungsvariable OPENAI_API_KEY ist nicht gesetzt.")
|
14 |
+
|
15 |
+
last_generation_time = defaultdict(float)
|
16 |
+
|
17 |
+
def generate_image(prompt, use_magic_prompt, style, user_id):
|
18 |
+
global last_generation_time
|
19 |
+
|
20 |
+
current_time = time.time()
|
21 |
+
if current_time - last_generation_time[user_id] < generation_timeout:
|
22 |
+
return f"Bitte warte {generation_timeout} Sekunden zwischen den Bildgenerierungen.", None
|
23 |
+
|
24 |
+
if use_magic_prompt:
|
25 |
+
prompt = enhance_prompt(prompt)
|
26 |
+
|
27 |
+
print("Finaler Prompt:", prompt)
|
28 |
+
|
29 |
+
try:
|
30 |
+
response = client.images.generate(
|
31 |
+
model="dall-e-3",
|
32 |
+
prompt=prompt,
|
33 |
+
size="1024x1024",
|
34 |
+
quality="standard",
|
35 |
+
style=style,
|
36 |
+
n=1
|
37 |
+
)
|
38 |
+
|
39 |
+
image_url = response.data[0].url
|
40 |
+
|
41 |
+
# Update the last generation time for this user
|
42 |
+
last_generation_time[user_id] = current_time
|
43 |
+
|
44 |
+
return prompt, image_url
|
45 |
+
except Exception as e:
|
46 |
+
print("Ein Fehler ist aufgetreten:", e)
|
47 |
+
return f"Ein Fehler ist beim Generieren des Bildes aufgetreten: {str(e)}", None
|
48 |
+
|
49 |
+
magic_prompts = [
|
50 |
+
"""Enhance and refine anything the user sends you as an image generation prompt for DALL-E 3:
|
51 |
+
|
52 |
+
Provide an improved version of the user message by following these guidelines:
|
53 |
+
Adds more specific details about the scene, subjects, and atmosphere
|
54 |
+
Incorporates precise descriptors for colors, textures, and lighting
|
55 |
+
Specifies the artistic style or medium (e.g., oil painting, digital art, photography)
|
56 |
+
Includes relevant compositional elements (foreground, background, perspective)
|
57 |
+
Adds any missing context or setting information
|
58 |
+
Removes any redundant or vague language
|
59 |
+
Ensures the prompt is coherent and follows a logical structure
|
60 |
+
Incorporates relevant technical terms or jargon related to art or photography
|
61 |
+
Suggests any additional elements that could enhance the overall image
|
62 |
+
Optimizes the prompt length for DALL-E 3's capabilities (aim for 40-60 words)
|
63 |
+
Only return the improved prompt without any additional comments or messages""",
|
64 |
+
"Verbessere den folgenden Bildgenerierungsprompt, um ihn detaillierter und kreativer zu machen."
|
65 |
+
]
|
66 |
+
|
67 |
+
def enhance_prompt(original_prompt):
|
68 |
+
try:
|
69 |
+
response = client.chat.completions.create(
|
70 |
+
model="gpt-3.5-turbo",
|
71 |
+
messages=[
|
72 |
+
{"role": "system", "content": magic_prompts[0]},
|
73 |
+
{"role": "user", "content": original_prompt}
|
74 |
+
],
|
75 |
+
max_tokens=250
|
76 |
+
)
|
77 |
+
enhanced_prompt = response.choices[0].message.content.strip()
|
78 |
+
return enhanced_prompt
|
79 |
+
except Exception as e:
|
80 |
+
print("Ein Fehler ist beim Verbessern des Prompts aufgetreten:", e)
|
81 |
+
return original_prompt
|
82 |
+
|
83 |
+
examples = [
|
84 |
+
"Astronaut im Dschungel, kalte Farbpalette, gedämpfte Farben, detailliert, 8k",
|
85 |
+
"Ein Astronaut reitet auf einem grünen Pferd",
|
86 |
+
"Ein köstliches Ceviche-Käsekuchenstück",
|
87 |
+
"Ein spielendes Glumanda in einer märchenhaften Waldlichtung bei Sonnenuntergang",
|
88 |
+
"Ein futuristisches Stadtbild bei Nacht mit Neonlichtern",
|
89 |
+
"Ein Roboter, der in einem antiken Tempel tanzt",
|
90 |
+
"Ein magisches Einhorn, das über einen Regenbogen springt",
|
91 |
+
"Ein mittelalterlicher Ritter, der gegen einen Drachen kämpft",
|
92 |
+
"cube cutout of an isometric programmer bedroom, 3d art, muted colors, soft lighting, high detail, concept art, behance, ray tracing, 4k",
|
93 |
+
"astronaut riding a llama on Mars",
|
94 |
+
"a close up of a fire breathing pokemon figure, digital art, trending on polycount, real life charmander, sparks flying, photo-realistic unreal engine, pokemon in the wild"
|
95 |
+
]
|
96 |
+
|
97 |
+
with gr.Blocks() as demo:
|
98 |
+
gr.Markdown("# codora DALL-E 3 Bildgenerator")
|
99 |
+
gr.Markdown(f"Gib einen Bildprompt ein und verwende optional die magische Prompt-Funktion, um ihn zu verbessern. Du kannst alle {generation_timeout} Sekunden ein neues Bild generieren.")
|
100 |
+
|
101 |
+
with gr.Row():
|
102 |
+
with gr.Column(scale=1):
|
103 |
+
prompt = gr.Textbox(label="Bildprompt")
|
104 |
+
use_magic_prompt = gr.Checkbox(label="Magischen Prompt verwenden")
|
105 |
+
style = gr.Radio(
|
106 |
+
label="Stil",
|
107 |
+
choices=["vivid", "natural"],
|
108 |
+
value="vivid"
|
109 |
+
)
|
110 |
+
user_id = gr.Textbox(label="Benutzer-ID", visible=False)
|
111 |
+
generate_button = gr.Button("Bild generieren")
|
112 |
+
|
113 |
+
with gr.Column(scale=1):
|
114 |
+
final_prompt = gr.Textbox(label="Finaler Prompt")
|
115 |
+
generated_image = gr.Image(label="Generiertes Bild")
|
116 |
+
|
117 |
+
# Place examples below the generation button
|
118 |
+
gr.Examples(
|
119 |
+
examples=examples,
|
120 |
+
inputs=prompt
|
121 |
+
)
|
122 |
+
|
123 |
+
generate_button.click(
|
124 |
+
fn=generate_image,
|
125 |
+
inputs=[prompt, use_magic_prompt, style, user_id],
|
126 |
+
outputs=[final_prompt, generated_image]
|
127 |
+
)
|
128 |
+
|
129 |
+
demo.launch()
|
requirements.txt
ADDED
File without changes
|