Spaces:
Sleeping
Sleeping
Sebastiankay
commited on
Commit
•
767fb92
1
Parent(s):
70fc24f
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
-
import os, sys, json, re, base64, random, requests, shutil
|
2 |
import gradio as gr
|
3 |
from datetime import datetime
|
4 |
import urllib.parse
|
5 |
from groq import Groq
|
6 |
from exif import Image
|
7 |
-
from PIL import Image as PILImage
|
8 |
import colorsys
|
9 |
|
10 |
MAX_SEED = 9999
|
@@ -23,16 +23,15 @@ os.makedirs(IMAGE_DIR)
|
|
23 |
|
24 |
RES = os.path.join(os.path.dirname(__file__), "_res")
|
25 |
|
|
|
|
|
26 |
custom_css = RES + "/_custom.css"
|
27 |
custom_js = RES + "/_custom.js"
|
28 |
|
29 |
-
|
30 |
-
title = "Pollinations Image Generator"
|
31 |
-
description = "Pollinations API + Randomizer"
|
32 |
-
|
33 |
|
34 |
theme = gr.themes.Soft(
|
35 |
-
primary_hue="
|
36 |
radius_size="sm",
|
37 |
)
|
38 |
|
@@ -55,9 +54,9 @@ def read_image_exfi_data(image_path):
|
|
55 |
return None, image_path, img_exif_comment
|
56 |
|
57 |
|
58 |
-
def groq_enhance_process(Prompt):
|
59 |
client = Groq(api_key=GROQ_APIKEY_PROMPTENHANCE)
|
60 |
-
|
61 |
SYSTEMPROMPT = os.path.join(RES, "groq_systemmessage_prompt_enhance.json")
|
62 |
with open(SYSTEMPROMPT, "r") as f:
|
63 |
SYSTEMPROMPT = json.load(f)
|
@@ -74,30 +73,22 @@ def groq_enhance_process(Prompt):
|
|
74 |
|
75 |
if completion.choices[0].message.content != "":
|
76 |
enhanced_prompt = completion.choices[0].message.content
|
|
|
77 |
|
78 |
return enhanced_prompt
|
79 |
|
80 |
|
81 |
-
def
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
if response.status_code == 200:
|
87 |
-
filename = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + ".png"
|
88 |
-
file_path = os.path.join(CACHE_DIR, filename)
|
89 |
-
with open(file_path, "wb") as f:
|
90 |
-
f.write(response.content)
|
91 |
-
img_exif_comment = read_exif(file_path)
|
92 |
-
prompt_value = img_exif_comment.get("prompt", "")
|
93 |
-
prompt_value = prompt_value.replace(Prompt, "").strip()
|
94 |
-
print(prompt_value)
|
95 |
-
return prompt_value
|
96 |
|
97 |
|
98 |
-
def
|
99 |
img = PILImage.open(image_path)
|
100 |
img = img.convert("RGB")
|
|
|
101 |
pixels = list(img.getdata())
|
102 |
|
103 |
# Erzeuge eine Liste mit den Häufigkeiten der Farben
|
@@ -115,82 +106,118 @@ def image_get_colors(image_path):
|
|
115 |
dominant_color = max(colors, key=lambda x: x[2])
|
116 |
dominant_color_rgb = colorsys.hsv_to_rgb(dominant_color[0], dominant_color[1], dominant_color[2])
|
117 |
dominant_color_rgb = [int(c * 255) for c in dominant_color_rgb]
|
|
|
118 |
print(dominant_color_rgb)
|
119 |
|
120 |
return dominant_color_rgb
|
121 |
|
122 |
|
123 |
def process(Prompt, image_width, image_height, image_seed, randomize_seed):
|
124 |
-
|
125 |
used_seed = random.randint(0, MAX_SEED) if image_seed == 0 or randomize_seed else image_seed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
|
132 |
-
|
133 |
-
request_url = f"https://image.pollinations.ai/prompt/{encode_prompt}?model=flux&width={image_width}&height={image_height}&nologo=true&enhance=false&nofeed=true&seed={used_seed}"
|
134 |
-
print(request_url)
|
135 |
-
response = requests.get(request_url)
|
136 |
-
if response.status_code == 200:
|
137 |
-
with open(file_path, "wb") as f:
|
138 |
-
f.write(response.content)
|
139 |
|
140 |
-
img_exif_comment
|
141 |
-
|
|
|
|
|
142 |
|
143 |
-
|
|
|
|
|
|
|
|
|
144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
|
146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
with gr.Tab("Image Generator"):
|
148 |
with gr.Row():
|
149 |
with gr.Column(scale=1):
|
150 |
with gr.Row():
|
151 |
-
|
152 |
-
|
153 |
-
with gr.Column(scale=2, elem_classes="image_size_selctor_wrapper"):
|
154 |
-
image_width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, value=1024, step=128, elem_classes="image-width-selector")
|
155 |
-
image_height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, value=683, step=128, elem_classes="image-height-selector")
|
156 |
-
# with gr.Row():
|
157 |
-
# image_ratio_buttons = gr.Radio(["16:9", "4:3", "3:2", "1:1"], value="3:2", label="Bild Größe", interactive=True, elem_classes="image-ratio-buttons", container=True, show_label=False)
|
158 |
-
# switch_width_height = gr.Button("🔁", size="sm", elem_id="switchRatioBtn", elem_classes="switch-ratio-btn", variant="primary")
|
159 |
with gr.Row():
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
with gr.Row():
|
164 |
-
|
165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
166 |
|
167 |
with gr.Column(scale=3):
|
168 |
with gr.Row():
|
169 |
-
with gr.Column(scale=3):
|
170 |
-
output_image = gr.Image(show_label=False, height=720)
|
171 |
with gr.Column(scale=1):
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
outpu_image_comment = gr.Json(visible=False)
|
180 |
-
|
181 |
-
|
182 |
-
# testimage = gr.Image(value="images/2024-08-22/2024-08-22_00-45-31.png", type="pil")
|
183 |
-
# gr.HTML("<div>")
|
184 |
-
# for image_path in os.listdir(images_dir):
|
185 |
-
# gr.HTML(f'<img src="images/2024-08-21/{image_path}" />')
|
186 |
-
# gr.HTML("</div>")
|
187 |
|
188 |
def switch_image_size_values(image_width, image_height):
|
189 |
return image_height, image_width
|
190 |
|
191 |
# switch_width_height.click(fn=switch_image_size_values, inputs=[image_width, image_height], outputs=[image_width, image_height])
|
192 |
|
193 |
-
run_button.click(fn=process, inputs=[text_prompt, image_width, image_height, image_seed, randomize_seed], outputs=[output_image, output_url,
|
194 |
-
|
|
|
|
|
|
|
195 |
|
196 |
-
demo.launch()
|
|
|
1 |
+
import os, sys, json, re, time, base64, random, requests, shutil
|
2 |
import gradio as gr
|
3 |
from datetime import datetime
|
4 |
import urllib.parse
|
5 |
from groq import Groq
|
6 |
from exif import Image
|
7 |
+
from PIL import Image as PILImage, ExifTags as PILExifTags
|
8 |
import colorsys
|
9 |
|
10 |
MAX_SEED = 9999
|
|
|
23 |
|
24 |
RES = os.path.join(os.path.dirname(__file__), "_res")
|
25 |
|
26 |
+
gr.set_static_paths(paths=["_res/assets/"])
|
27 |
+
|
28 |
custom_css = RES + "/_custom.css"
|
29 |
custom_js = RES + "/_custom.js"
|
30 |
|
31 |
+
title = "Bilder Builder"
|
|
|
|
|
|
|
32 |
|
33 |
theme = gr.themes.Soft(
|
34 |
+
primary_hue="cyan",
|
35 |
radius_size="sm",
|
36 |
)
|
37 |
|
|
|
54 |
return None, image_path, img_exif_comment
|
55 |
|
56 |
|
57 |
+
def groq_enhance_process(Prompt=""):
|
58 |
client = Groq(api_key=GROQ_APIKEY_PROMPTENHANCE)
|
59 |
+
Prompt = "random prompt" if Prompt == "" else Prompt
|
60 |
SYSTEMPROMPT = os.path.join(RES, "groq_systemmessage_prompt_enhance.json")
|
61 |
with open(SYSTEMPROMPT, "r") as f:
|
62 |
SYSTEMPROMPT = json.load(f)
|
|
|
73 |
|
74 |
if completion.choices[0].message.content != "":
|
75 |
enhanced_prompt = completion.choices[0].message.content
|
76 |
+
enhanced_prompt = re.sub(r"[\.\"]+", "", enhanced_prompt)
|
77 |
|
78 |
return enhanced_prompt
|
79 |
|
80 |
|
81 |
+
def image_get_size(image_path):
|
82 |
+
img = PILImage.open(image_path)
|
83 |
+
print("Image size:", img.size)
|
84 |
+
width, height = img.size
|
85 |
+
return width, height
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
|
88 |
+
def image_get_dominant_color(image_path):
|
89 |
img = PILImage.open(image_path)
|
90 |
img = img.convert("RGB")
|
91 |
+
img = img.resize((100, 100), resample=0)
|
92 |
pixels = list(img.getdata())
|
93 |
|
94 |
# Erzeuge eine Liste mit den Häufigkeiten der Farben
|
|
|
106 |
dominant_color = max(colors, key=lambda x: x[2])
|
107 |
dominant_color_rgb = colorsys.hsv_to_rgb(dominant_color[0], dominant_color[1], dominant_color[2])
|
108 |
dominant_color_rgb = [int(c * 255) for c in dominant_color_rgb]
|
109 |
+
dominant_color_rgb = f"rgb({dominant_color_rgb[0]}, {dominant_color_rgb[1]}, {dominant_color_rgb[2]})"
|
110 |
print(dominant_color_rgb)
|
111 |
|
112 |
return dominant_color_rgb
|
113 |
|
114 |
|
115 |
def process(Prompt, image_width, image_height, image_seed, randomize_seed):
|
|
|
116 |
used_seed = random.randint(0, MAX_SEED) if image_seed == 0 or randomize_seed else image_seed
|
117 |
+
if "test image" in Prompt.lower() or Prompt == "":
|
118 |
+
print("Prompt:", Prompt)
|
119 |
+
TESTIMAGES = os.path.join(RES, "testimages")
|
120 |
+
random_num = random.randint(1, 10)
|
121 |
+
match = re.match(r"test image (\d+)", Prompt)
|
122 |
+
if match and 1 <= int(match.group(1)) <= 10:
|
123 |
+
random_num = match.group(1)
|
124 |
+
print("\n\nrandom: ", random_num)
|
125 |
+
|
126 |
+
Prompt = "test image " + str(random_num) if Prompt == "" else Prompt
|
127 |
+
filename = str(random_num) + ".png"
|
128 |
+
print("Filename:", filename)
|
129 |
+
file_path = os.path.join(TESTIMAGES, filename)
|
130 |
|
131 |
+
img_exif_comment = read_exif(file_path)
|
132 |
+
img_dominant_color = image_get_dominant_color(file_path)
|
133 |
+
img_width, img_height = image_get_size(file_path)
|
134 |
+
print("ImageSize: " + str(img_width) + "x" + str(img_height) + "px")
|
135 |
|
136 |
+
time.sleep(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
|
138 |
+
return ({"value": file_path, "__type__": "update"}, {"value": None, "visible": False, "__type__": "update"}, img_exif_comment, {"visible": True, "__type__": "update"}, {"value": Prompt, "visible": True, "__type__": "update"}, img_width, img_height, used_seed, {"value": file_path, "visible": True, "__type__": "update"}, img_dominant_color)
|
139 |
+
else:
|
140 |
+
used_seed = random.randint(0, MAX_SEED) if image_seed == 0 or randomize_seed else image_seed
|
141 |
+
used_model = "turbo" if int(image_width) > 1024 or int(image_height) > 1024 else "flux" # turbo, flux
|
142 |
|
143 |
+
timestamp = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
|
144 |
+
filename_prompt = re.sub(r"[^\w\s-]", "", Prompt).strip().replace(" ", "_")
|
145 |
+
filename = timestamp + "_" + filename_prompt[:100] + ".png"
|
146 |
+
# file_path = os.path.join(filename_dir, filename)
|
147 |
+
file_path = os.path.join(IMAGE_DIR, filename)
|
148 |
|
149 |
+
encode_prompt = urllib.parse.quote(Prompt)
|
150 |
+
request_url = f"https://image.pollinations.ai/prompt/{encode_prompt}?model={used_model}&width={image_width}&height={image_height}&nologo=true&enhance=false&nofeed=true&seed={used_seed}"
|
151 |
+
print(request_url)
|
152 |
+
response = requests.get(request_url)
|
153 |
+
if response.status_code == 200:
|
154 |
+
with open(file_path, "wb") as f:
|
155 |
+
f.write(response.content)
|
156 |
|
157 |
+
img_exif_comment = read_exif(file_path)
|
158 |
+
img_dominant_color = image_get_dominant_color(file_path)
|
159 |
+
img_width, img_height = image_get_size(file_path)
|
160 |
+
|
161 |
+
return ({"value": file_path, "__type__": "update"}, {"value": None, "visible": False, "__type__": "update"}, img_exif_comment, {"visible": True, "__type__": "update"}, {"value": Prompt, "visible": True, "__type__": "update"}, img_width, img_height, used_seed, {"value": file_path, "visible": True, "__type__": "update"}, img_dominant_color)
|
162 |
+
|
163 |
+
|
164 |
+
with gr.Blocks(theme=theme, css=custom_css, js=custom_js, title=title) as demo:
|
165 |
with gr.Tab("Image Generator"):
|
166 |
with gr.Row():
|
167 |
with gr.Column(scale=1):
|
168 |
with gr.Row():
|
169 |
+
placeholder_text = "[???] Generiert dir einen zufälligen Prompt.\n[STERN] optimiert deinen eignen Prompt.\n[RUN] generiert dein Bild."
|
170 |
+
text_prompt = gr.Textbox(label="Prompt", show_label=False, lines=12, max_lines=18, placeholder=placeholder_text, elem_classes="prompt-input", autofocus=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
with gr.Row():
|
172 |
+
random_prompt_button = gr.Button("", variant="secondary", elem_id="random_prompt_btn", elem_classes="random-prompt-btn", icon="_res/assets/star_light_48.png")
|
173 |
+
enhance_prompt_button = gr.Button("", variant="secondary", elem_id="enhance_prompt_btn", elem_classes="enhance-prompt-btn", icon="_res/assets/star_light_48.png")
|
174 |
+
run_button = gr.Button("Erstellen", variant="primary", elem_id="run_btn", elem_classes="run-btn")
|
175 |
+
with gr.Row(elem_classes="image_size_selctor_wrapper"):
|
176 |
+
with gr.Column(scale=1):
|
177 |
+
with gr.Row():
|
178 |
+
# image_width = gr.Textbox(label="Breite", minimum=256, maximum=MAX_IMAGE_SIZE, value=1024, step=128, elem_id="image_width_selector", elem_classes="image-width-selector", scale=1)
|
179 |
+
image_width = gr.Textbox(label="Breite", value=1024, lines=1, max_lines=1, elem_id="image_width_selector", elem_classes="image-width-selector", scale=1)
|
180 |
+
# image_height = gr.Number(label="Höhe", minimum=256, maximum=MAX_IMAGE_SIZE, value=683, step=128, elem_id="image_height_selector", elem_classes="image-height-selector", scale=1)
|
181 |
+
image_height = gr.Textbox(label="Höhe", value=683, lines=1, max_lines=1, elem_id="image_height_selector", elem_classes="image-height-selector", scale=1)
|
182 |
+
with gr.Row():
|
183 |
+
image_ratio_buttons = gr.Radio(["16:9", "4:3", "3:2", "1:1"], value="3:2", label="Querformat", show_label=True, info="", interactive=True, elem_id="image_ratio_buttons", elem_classes="image-ratio-buttons", container=True, scale=2)
|
184 |
+
switch_width_height = gr.Button("", size="sm", elem_id="switch_width_height", elem_classes="switch-ratio-btn", variant="primary", scale=1)
|
185 |
+
with gr.Column():
|
186 |
+
image_seed = gr.Slider(label="Seed", info="Jeder Seed generiert ein anderes Bild mit dem selben Prompt", minimum=0, step=1, value=42, maximum=MAX_SEED)
|
187 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=False)
|
188 |
|
189 |
with gr.Column(scale=3):
|
190 |
with gr.Row():
|
|
|
|
|
191 |
with gr.Column(scale=1):
|
192 |
+
with gr.Row():
|
193 |
+
output_image = gr.Image(show_label=False, height=720, min_width=320, scale=3, elem_id="output_image", type="filepath")
|
194 |
+
with gr.Column(scale=1, visible=False, elem_classes="image-info-wrapper") as image_info_wrapper:
|
195 |
+
# with gr.Group(visible=False) as image_info_wrapper:
|
196 |
+
with gr.Group():
|
197 |
+
image_informations = gr.Markdown("""## Bildinformationen""", visible=True)
|
198 |
+
# textbox_your_prompt = gr.Textbox(label="Dein Prmopt", lines=2, max_lines=4, interactive=False, show_copy_button=True)
|
199 |
+
with gr.Row(elem_classes="img-size-wrapper"):
|
200 |
+
image_info_tb_width = gr.Textbox(label="Breite", lines=1, max_lines=1, interactive=False, show_copy_button=True, elem_classes="image-info-tb-width")
|
201 |
+
image_info_tb_height = gr.Textbox(label="Höhe", lines=1, max_lines=1, interactive=False, show_copy_button=True, elem_classes="image-info-tb-height")
|
202 |
+
with gr.Row(elem_classes="img-seed-wrapper"):
|
203 |
+
image_info_tb_seed = gr.Textbox(label="Seed", lines=1, max_lines=1, interactive=False, show_copy_button=True, elem_classes="image-info-tb-seed")
|
204 |
+
image_info_tb_prompt = gr.Textbox("Bild Prompt", lines=4, max_lines=8, interactive=False, show_copy_button=True, visible=True)
|
205 |
+
image_download_button = gr.DownloadButton("Bild herunterladen", value=None, elem_classes="download-button", variant="primary", visible=False)
|
206 |
+
|
207 |
+
output_url = gr.Textbox(label="Output URL", show_label=True, interactive=False, visible=False)
|
208 |
outpu_image_comment = gr.Json(visible=False)
|
209 |
+
output_dominant_image_color = gr.Textbox(show_label=False, elem_id="dominant_image_color", visible=True, elem_classes="output-dominant-image-color")
|
210 |
+
|
|
|
|
|
|
|
|
|
|
|
211 |
|
212 |
def switch_image_size_values(image_width, image_height):
|
213 |
return image_height, image_width
|
214 |
|
215 |
# switch_width_height.click(fn=switch_image_size_values, inputs=[image_width, image_height], outputs=[image_width, image_height])
|
216 |
|
217 |
+
run_button.click(fn=process, inputs=[text_prompt, image_width, image_height, image_seed, randomize_seed], outputs=[output_image, output_url, outpu_image_comment, image_informations, image_info_tb_prompt, image_info_tb_width, image_info_tb_height, image_info_tb_seed, image_download_button, output_dominant_image_color])
|
218 |
+
|
219 |
+
enhance_prompt_button.click(fn=groq_enhance_process, inputs=[text_prompt], outputs=[text_prompt])
|
220 |
+
random_prompt_button.click(fn=groq_enhance_process, inputs=None, outputs=[text_prompt])
|
221 |
+
|
222 |
|
223 |
+
demo.launch()
|