Spaces:
Sleeping
Sleeping
Tobias Geisler commited on
Commit ·
7f24ec8
1
Parent(s): 71261da
protect generation with app password
Browse files
app.py
CHANGED
|
@@ -2,24 +2,47 @@ 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
global last_generation_time
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
current_time = time.time()
|
| 21 |
-
if current_time - last_generation_time[user_id] <
|
| 22 |
-
return
|
| 23 |
|
| 24 |
if use_magic_prompt:
|
| 25 |
prompt = enhance_prompt(prompt)
|
|
@@ -49,57 +72,20 @@ def generate_image(prompt, use_magic_prompt, style, user_id):
|
|
| 49 |
print("Ein Fehler ist aufgetreten:", e)
|
| 50 |
return f"Ein Fehler ist beim Generieren des Bildes aufgetreten: {error_message}", None
|
| 51 |
|
| 52 |
-
magic_prompts = [
|
| 53 |
-
"""Enhance and refine anything the user sends you as an image generation prompt for DALL-E 3:
|
| 54 |
-
|
| 55 |
-
Provide an improved version of the user message by following these guidelines:
|
| 56 |
-
Adds more specific details about the scene, subjects, and atmosphere
|
| 57 |
-
Incorporates precise descriptors for colors, textures, and lighting
|
| 58 |
-
Specifies the artistic style or medium (e.g., oil painting, digital art, photography)
|
| 59 |
-
Includes relevant compositional elements (foreground, background, perspective)
|
| 60 |
-
Adds any missing context or setting information
|
| 61 |
-
Removes any redundant or vague language
|
| 62 |
-
Ensures the prompt is coherent and follows a logical structure
|
| 63 |
-
Incorporates relevant technical terms or jargon related to art or photography
|
| 64 |
-
Suggests any additional elements that could enhance the overall image
|
| 65 |
-
Optimizes the prompt length for DALL-E 3's capabilities (aim for 40-60 words)
|
| 66 |
-
Only return the improved prompt without any additional comments or messages""",
|
| 67 |
-
"Verbessere den folgenden Bildgenerierungsprompt, um ihn detaillierter und kreativer zu machen."
|
| 68 |
-
]
|
| 69 |
-
|
| 70 |
-
def enhance_prompt(original_prompt):
|
| 71 |
-
try:
|
| 72 |
-
response = client.chat.completions.create(
|
| 73 |
-
model="gpt-3.5-turbo",
|
| 74 |
-
messages=[
|
| 75 |
-
{"role": "system", "content": magic_prompts[0]},
|
| 76 |
-
{"role": "user", "content": original_prompt}
|
| 77 |
-
],
|
| 78 |
-
max_tokens=250
|
| 79 |
-
)
|
| 80 |
-
enhanced_prompt = response.choices[0].message.content.strip()
|
| 81 |
-
return enhanced_prompt
|
| 82 |
-
except Exception as e:
|
| 83 |
-
print("Ein Fehler ist beim Verbessern des Prompts aufgetreten:", e)
|
| 84 |
-
return original_prompt
|
| 85 |
-
|
| 86 |
examples = [
|
| 87 |
"Astronaut im Dschungel, kalte Farbpalette, gedämpfte Farben, detailliert, 8k",
|
| 88 |
"Ein Astronaut reitet auf einem grünen Pferd",
|
| 89 |
"Ein köstliches Ceviche-Käsekuchenstück",
|
| 90 |
-
"Ein
|
| 91 |
"Ein futuristisches Stadtbild bei Nacht mit Neonlichtern",
|
| 92 |
"Ein Roboter, der in einem antiken Tempel tanzt",
|
| 93 |
"Ein magisches Einhorn, das über einen Regenbogen springt",
|
| 94 |
-
"Ein mittelalterlicher Ritter, der gegen einen Drachen kämpft"
|
| 95 |
-
"cube cutout of an isometric programmer bedroom, 3d art, muted colors, soft lighting, high detail, concept art, behance, ray tracing, 4k",
|
| 96 |
-
"astronaut riding a llama on Mars",
|
| 97 |
-
"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"
|
| 98 |
]
|
| 99 |
|
| 100 |
with gr.Blocks() as demo:
|
| 101 |
gr.Markdown("# codora DALL-E 3 Bildgenerator")
|
| 102 |
-
gr.Markdown(
|
| 103 |
|
| 104 |
with gr.Row():
|
| 105 |
with gr.Column(scale=1):
|
|
@@ -111,21 +97,22 @@ with gr.Blocks() as demo:
|
|
| 111 |
value="vivid"
|
| 112 |
)
|
| 113 |
user_id = gr.Textbox(label="Benutzer-ID", visible=False)
|
|
|
|
| 114 |
generate_button = gr.Button("Bild generieren")
|
| 115 |
|
| 116 |
with gr.Column(scale=1):
|
| 117 |
final_prompt = gr.Textbox(label="Finaler Prompt")
|
| 118 |
-
generated_image = gr.Image(label="Generiertes Bild")
|
| 119 |
|
| 120 |
# Place examples below the generation button
|
| 121 |
gr.Examples(
|
| 122 |
examples=examples,
|
| 123 |
-
inputs=prompt
|
| 124 |
)
|
| 125 |
|
| 126 |
generate_button.click(
|
| 127 |
fn=generate_image,
|
| 128 |
-
inputs=[prompt, use_magic_prompt, style, user_id],
|
| 129 |
outputs=[final_prompt, generated_image]
|
| 130 |
)
|
| 131 |
|
|
|
|
| 2 |
import time
|
| 3 |
from collections import defaultdict
|
| 4 |
from dotenv import load_dotenv
|
|
|
|
| 5 |
import os
|
| 6 |
+
from openai import OpenAI
|
| 7 |
+
import random
|
| 8 |
|
| 9 |
load_dotenv()
|
| 10 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
|
|
|
| 11 |
|
| 12 |
if client.api_key is None:
|
| 13 |
raise ValueError("Die Umgebungsvariable OPENAI_API_KEY ist nicht gesetzt.")
|
| 14 |
|
| 15 |
+
APP_PASSWORD = os.getenv("APP_PASSWORD")
|
| 16 |
+
if APP_PASSWORD is None:
|
| 17 |
+
raise ValueError("Die Umgebungsvariable APP_PASSWORD ist nicht gesetzt.")
|
| 18 |
+
|
| 19 |
last_generation_time = defaultdict(float)
|
| 20 |
|
| 21 |
+
def enhance_prompt(original_prompt):
|
| 22 |
+
try:
|
| 23 |
+
response = client.chat.completions.create(
|
| 24 |
+
model="gpt-3.5-turbo",
|
| 25 |
+
messages=[
|
| 26 |
+
{"role": "system", "content": "Verbessere den folgenden Bildgenerierungsprompt, um ihn detaillierter und kreativer zu machen."},
|
| 27 |
+
{"role": "user", "content": original_prompt}
|
| 28 |
+
],
|
| 29 |
+
max_tokens=100
|
| 30 |
+
)
|
| 31 |
+
enhanced_prompt = response.choices[0].message.content.strip()
|
| 32 |
+
return enhanced_prompt
|
| 33 |
+
except Exception as e:
|
| 34 |
+
print("Ein Fehler ist beim Verbessern des Prompts aufgetreten:", e)
|
| 35 |
+
return original_prompt
|
| 36 |
+
|
| 37 |
+
def generate_image(prompt, use_magic_prompt, style, user_id, password):
|
| 38 |
global last_generation_time
|
| 39 |
|
| 40 |
+
if password != APP_PASSWORD:
|
| 41 |
+
return "Falsches Passwort. Bitte versuche es erneut.", None
|
| 42 |
+
|
| 43 |
current_time = time.time()
|
| 44 |
+
if current_time - last_generation_time[user_id] < 30:
|
| 45 |
+
return "Bitte warte 30 Sekunden zwischen den Bildgenerierungen.", None
|
| 46 |
|
| 47 |
if use_magic_prompt:
|
| 48 |
prompt = enhance_prompt(prompt)
|
|
|
|
| 72 |
print("Ein Fehler ist aufgetreten:", e)
|
| 73 |
return f"Ein Fehler ist beim Generieren des Bildes aufgetreten: {error_message}", None
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
examples = [
|
| 76 |
"Astronaut im Dschungel, kalte Farbpalette, gedämpfte Farben, detailliert, 8k",
|
| 77 |
"Ein Astronaut reitet auf einem grünen Pferd",
|
| 78 |
"Ein köstliches Ceviche-Käsekuchenstück",
|
| 79 |
+
"Ein wunderschönes Glumanda in einer blühenden Wiese",
|
| 80 |
"Ein futuristisches Stadtbild bei Nacht mit Neonlichtern",
|
| 81 |
"Ein Roboter, der in einem antiken Tempel tanzt",
|
| 82 |
"Ein magisches Einhorn, das über einen Regenbogen springt",
|
| 83 |
+
"Ein mittelalterlicher Ritter, der gegen einen Drachen kämpft"
|
|
|
|
|
|
|
|
|
|
| 84 |
]
|
| 85 |
|
| 86 |
with gr.Blocks() as demo:
|
| 87 |
gr.Markdown("# codora DALL-E 3 Bildgenerator")
|
| 88 |
+
gr.Markdown("Gib einen Bildprompt ein und verwende optional die magische Prompt-Funktion, um ihn zu verbessern. Du kannst nur ein Bild alle 30 Sekunden generieren.")
|
| 89 |
|
| 90 |
with gr.Row():
|
| 91 |
with gr.Column(scale=1):
|
|
|
|
| 97 |
value="vivid"
|
| 98 |
)
|
| 99 |
user_id = gr.Textbox(label="Benutzer-ID", visible=False)
|
| 100 |
+
password = gr.Textbox(label="App Passwort", type="password")
|
| 101 |
generate_button = gr.Button("Bild generieren")
|
| 102 |
|
| 103 |
with gr.Column(scale=1):
|
| 104 |
final_prompt = gr.Textbox(label="Finaler Prompt")
|
| 105 |
+
generated_image = gr.Image(label="Generiertes Bild", scale=0.5) # Set scale to 0.5 for 50% width
|
| 106 |
|
| 107 |
# Place examples below the generation button
|
| 108 |
gr.Examples(
|
| 109 |
examples=examples,
|
| 110 |
+
inputs=[prompt]
|
| 111 |
)
|
| 112 |
|
| 113 |
generate_button.click(
|
| 114 |
fn=generate_image,
|
| 115 |
+
inputs=[prompt, use_magic_prompt, style, user_id, password],
|
| 116 |
outputs=[final_prompt, generated_image]
|
| 117 |
)
|
| 118 |
|