|
import gradio as gr |
|
import time |
|
from collections import defaultdict |
|
from dotenv import load_dotenv |
|
from openai import OpenAI |
|
import os |
|
|
|
load_dotenv() |
|
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) |
|
generation_timeout = float(os.getenv("GENERATION_TIMEOUT")) |
|
|
|
if client.api_key is None: |
|
raise ValueError("Die Umgebungsvariable OPENAI_API_KEY ist nicht gesetzt.") |
|
|
|
last_generation_time = defaultdict(float) |
|
|
|
def generate_image(prompt, use_magic_prompt, style, user_id): |
|
global last_generation_time |
|
|
|
current_time = time.time() |
|
if current_time - last_generation_time[user_id] < generation_timeout: |
|
return f"Bitte warte {generation_timeout} Sekunden zwischen den Bildgenerierungen.", None |
|
|
|
if use_magic_prompt: |
|
prompt = enhance_prompt(prompt) |
|
|
|
print("Finaler Prompt:", prompt) |
|
|
|
try: |
|
response = client.images.generate( |
|
model="dall-e-3", |
|
prompt=prompt, |
|
size="1024x1024", |
|
quality="standard", |
|
style=style, |
|
n=1 |
|
) |
|
|
|
image_url = response.data[0].url |
|
|
|
|
|
last_generation_time[user_id] = current_time |
|
|
|
return prompt, image_url |
|
except Exception as e: |
|
print("Ein Fehler ist aufgetreten:", e) |
|
return f"Ein Fehler ist beim Generieren des Bildes aufgetreten: {str(e)}", None |
|
|
|
magic_prompts = [ |
|
"""Enhance and refine anything the user sends you as an image generation prompt for DALL-E 3: |
|
|
|
Provide an improved version of the user message by following these guidelines: |
|
Adds more specific details about the scene, subjects, and atmosphere |
|
Incorporates precise descriptors for colors, textures, and lighting |
|
Specifies the artistic style or medium (e.g., oil painting, digital art, photography) |
|
Includes relevant compositional elements (foreground, background, perspective) |
|
Adds any missing context or setting information |
|
Removes any redundant or vague language |
|
Ensures the prompt is coherent and follows a logical structure |
|
Incorporates relevant technical terms or jargon related to art or photography |
|
Suggests any additional elements that could enhance the overall image |
|
Optimizes the prompt length for DALL-E 3's capabilities (aim for 40-60 words) |
|
Only return the improved prompt without any additional comments or messages""", |
|
"Verbessere den folgenden Bildgenerierungsprompt, um ihn detaillierter und kreativer zu machen." |
|
] |
|
|
|
def enhance_prompt(original_prompt): |
|
try: |
|
response = client.chat.completions.create( |
|
model="gpt-3.5-turbo", |
|
messages=[ |
|
{"role": "system", "content": magic_prompts[0]}, |
|
{"role": "user", "content": original_prompt} |
|
], |
|
max_tokens=250 |
|
) |
|
enhanced_prompt = response.choices[0].message.content.strip() |
|
return enhanced_prompt |
|
except Exception as e: |
|
print("Ein Fehler ist beim Verbessern des Prompts aufgetreten:", e) |
|
return original_prompt |
|
|
|
examples = [ |
|
"Astronaut im Dschungel, kalte Farbpalette, gedämpfte Farben, detailliert, 8k", |
|
"Ein Astronaut reitet auf einem grünen Pferd", |
|
"Ein köstliches Ceviche-Käsekuchenstück", |
|
"Ein spielendes Glumanda in einer märchenhaften Waldlichtung bei Sonnenuntergang", |
|
"Ein futuristisches Stadtbild bei Nacht mit Neonlichtern", |
|
"Ein Roboter, der in einem antiken Tempel tanzt", |
|
"Ein magisches Einhorn, das über einen Regenbogen springt", |
|
"Ein mittelalterlicher Ritter, der gegen einen Drachen kämpft", |
|
"cube cutout of an isometric programmer bedroom, 3d art, muted colors, soft lighting, high detail, concept art, behance, ray tracing, 4k", |
|
"astronaut riding a llama on Mars", |
|
"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" |
|
] |
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("# codora DALL-E 3 Bildgenerator") |
|
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.") |
|
|
|
with gr.Row(): |
|
with gr.Column(scale=1): |
|
prompt = gr.Textbox(label="Bildprompt") |
|
use_magic_prompt = gr.Checkbox(label="Magischen Prompt verwenden") |
|
style = gr.Radio( |
|
label="Stil", |
|
choices=["vivid", "natural"], |
|
value="vivid" |
|
) |
|
user_id = gr.Textbox(label="Benutzer-ID", visible=False) |
|
generate_button = gr.Button("Bild generieren") |
|
|
|
with gr.Column(scale=1): |
|
final_prompt = gr.Textbox(label="Finaler Prompt") |
|
generated_image = gr.Image(label="Generiertes Bild") |
|
|
|
|
|
gr.Examples( |
|
examples=examples, |
|
inputs=prompt |
|
) |
|
|
|
generate_button.click( |
|
fn=generate_image, |
|
inputs=[prompt, use_magic_prompt, style, user_id], |
|
outputs=[final_prompt, generated_image] |
|
) |
|
|
|
demo.launch() |