Spaces:
Running
Running
Sebastiankay
commited on
Commit
•
f0c4505
1
Parent(s):
d94013d
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,7 @@ from exif import Image
|
|
7 |
from PIL import Image as PILImage, ExifTags as PILExifTags
|
8 |
import colorsys
|
9 |
|
|
|
10 |
MAX_SEED = 9999
|
11 |
MAX_IMAGE_SIZE = 2048
|
12 |
|
@@ -35,6 +36,7 @@ theme = gr.themes.Soft(
|
|
35 |
)
|
36 |
|
37 |
|
|
|
38 |
def read_exif(image_path):
|
39 |
with open(image_path, "rb") as src:
|
40 |
img = Image(src)
|
@@ -53,6 +55,7 @@ def read_image_exfi_data(image_path):
|
|
53 |
return None, image_path, img_exif_comment
|
54 |
|
55 |
|
|
|
56 |
def groq_enhance_process(Prompt=""):
|
57 |
client = Groq(api_key=GROQ_APIKEY_PROMPTENHANCE)
|
58 |
Prompt = "random prompt" if Prompt == "" else Prompt
|
@@ -84,6 +87,7 @@ def image_get_size(image_path):
|
|
84 |
return width, height
|
85 |
|
86 |
|
|
|
87 |
def image_get_dominant_color(image_path):
|
88 |
img = PILImage.open(image_path)
|
89 |
img = img.convert("RGB")
|
@@ -111,54 +115,37 @@ def image_get_dominant_color(image_path):
|
|
111 |
return dominant_color_rgb
|
112 |
|
113 |
|
|
|
114 |
def process(Prompt, image_width, image_height, image_seed, randomize_seed):
|
|
|
|
|
|
|
|
|
|
|
115 |
used_seed = random.randint(0, MAX_SEED) if image_seed == 0 or randomize_seed else image_seed
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
|
|
129 |
|
130 |
img_exif_comment = read_exif(file_path)
|
131 |
img_dominant_color = image_get_dominant_color(file_path)
|
132 |
img_width, img_height = image_get_size(file_path)
|
133 |
-
print("ImageSize: " + str(img_width) + "x" + str(img_height) + "px")
|
134 |
-
|
135 |
-
time.sleep(1)
|
136 |
|
137 |
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, used_seed)
|
138 |
-
else:
|
139 |
-
used_model = "turbo" if int(image_width) > 1024 or int(image_height) > 1024 else "flux" # turbo, flux
|
140 |
-
|
141 |
-
timestamp = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
|
142 |
-
filename_prompt = re.sub(r"[^\w\s-]", "", Prompt).strip().replace(" ", "_")
|
143 |
-
filename = timestamp + "_" + filename_prompt[:100] + ".png"
|
144 |
-
# file_path = os.path.join(filename_dir, filename)
|
145 |
-
file_path = os.path.join(IMAGE_DIR, filename)
|
146 |
-
|
147 |
-
encode_prompt = urllib.parse.quote(Prompt)
|
148 |
-
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}"
|
149 |
-
print(request_url)
|
150 |
-
response = requests.get(request_url)
|
151 |
-
if response.status_code == 200:
|
152 |
-
with open(file_path, "wb") as f:
|
153 |
-
f.write(response.content)
|
154 |
-
|
155 |
-
img_exif_comment = read_exif(file_path)
|
156 |
-
img_dominant_color = image_get_dominant_color(file_path)
|
157 |
-
img_width, img_height = image_get_size(file_path)
|
158 |
-
|
159 |
-
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, used_seed)
|
160 |
|
161 |
|
|
|
162 |
with gr.Blocks(theme=theme, css=custom_css, js=custom_js, title=title) as demo:
|
163 |
with gr.Tab("Image Generator"):
|
164 |
with gr.Row():
|
@@ -209,7 +196,7 @@ with gr.Blocks(theme=theme, css=custom_css, js=custom_js, title=title) as demo:
|
|
209 |
|
210 |
def switch_image_ratio_buttons(ratio_value):
|
211 |
ratio_value = ratio_value.split(":")
|
212 |
-
ratio_value_new = f"{ratio_value[1]}:{ratio_value[0]}"
|
213 |
|
214 |
if ratio_value[1] > ratio_value[0]:
|
215 |
# Querformat
|
@@ -222,18 +209,33 @@ with gr.Blocks(theme=theme, css=custom_css, js=custom_js, title=title) as demo:
|
|
222 |
|
223 |
return {"choices": new_choises, "value": ratio_value_new, "label": new_label, "__type__": "update"}
|
224 |
|
225 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
226 |
|
227 |
switch_width_height.click(fn=switch_image_size_values, inputs=[image_width, image_height], outputs=[image_width, image_height], show_progress="hidden")
|
228 |
-
|
229 |
switch_width_height.click(fn=switch_image_ratio_buttons, inputs=[image_ratio_buttons], outputs=[image_ratio_buttons], show_progress="hidden")
|
230 |
|
231 |
-
|
232 |
|
233 |
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, image_seed])
|
234 |
|
235 |
enhance_prompt_button.click(fn=groq_enhance_process, inputs=[text_prompt], outputs=[text_prompt])
|
236 |
random_prompt_button.click(fn=groq_enhance_process, inputs=None, outputs=[text_prompt])
|
237 |
|
238 |
-
|
239 |
demo.launch()
|
|
|
7 |
from PIL import Image as PILImage, ExifTags as PILExifTags
|
8 |
import colorsys
|
9 |
|
10 |
+
# MARK: INIT
|
11 |
MAX_SEED = 9999
|
12 |
MAX_IMAGE_SIZE = 2048
|
13 |
|
|
|
36 |
)
|
37 |
|
38 |
|
39 |
+
# MARK: READ EXIF
|
40 |
def read_exif(image_path):
|
41 |
with open(image_path, "rb") as src:
|
42 |
img = Image(src)
|
|
|
55 |
return None, image_path, img_exif_comment
|
56 |
|
57 |
|
58 |
+
# MARK: GROQ PROMPT ENHANCE
|
59 |
def groq_enhance_process(Prompt=""):
|
60 |
client = Groq(api_key=GROQ_APIKEY_PROMPTENHANCE)
|
61 |
Prompt = "random prompt" if Prompt == "" else Prompt
|
|
|
87 |
return width, height
|
88 |
|
89 |
|
90 |
+
# MARK: DOMINANT COLOR
|
91 |
def image_get_dominant_color(image_path):
|
92 |
img = PILImage.open(image_path)
|
93 |
img = img.convert("RGB")
|
|
|
115 |
return dominant_color_rgb
|
116 |
|
117 |
|
118 |
+
# MARK: PROCESS IMAGE
|
119 |
def process(Prompt, image_width, image_height, image_seed, randomize_seed):
|
120 |
+
|
121 |
+
if Prompt == "":
|
122 |
+
gr.Info("Kein Prompt angegeben, es wird ein zufälliger Prompt generiert.", duration=15)
|
123 |
+
Prompt = groq_enhance_process("random prompt")
|
124 |
+
|
125 |
used_seed = random.randint(0, MAX_SEED) if image_seed == 0 or randomize_seed else image_seed
|
126 |
+
used_model = "turbo" if int(image_width) > 1024 or int(image_height) > 1024 else "flux" # turbo, flux
|
127 |
+
|
128 |
+
timestamp = datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
|
129 |
+
filename_prompt = re.sub(r"[^\w\s-]", "", Prompt).strip().replace(" ", "_")
|
130 |
+
filename = timestamp + "_" + filename_prompt[:100] + ".png"
|
131 |
+
file_path = os.path.join(IMAGE_DIR, filename)
|
132 |
+
|
133 |
+
encode_prompt = urllib.parse.quote(Prompt)
|
134 |
+
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}"
|
135 |
+
print(request_url)
|
136 |
+
response = requests.get(request_url)
|
137 |
+
if response.status_code == 200:
|
138 |
+
with open(file_path, "wb") as f:
|
139 |
+
f.write(response.content)
|
140 |
|
141 |
img_exif_comment = read_exif(file_path)
|
142 |
img_dominant_color = image_get_dominant_color(file_path)
|
143 |
img_width, img_height = image_get_size(file_path)
|
|
|
|
|
|
|
144 |
|
145 |
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, used_seed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
|
147 |
|
148 |
+
# MARK: Gradio BLOCKS UI
|
149 |
with gr.Blocks(theme=theme, css=custom_css, js=custom_js, title=title) as demo:
|
150 |
with gr.Tab("Image Generator"):
|
151 |
with gr.Row():
|
|
|
196 |
|
197 |
def switch_image_ratio_buttons(ratio_value):
|
198 |
ratio_value = ratio_value.split(":")
|
199 |
+
ratio_value_new = f"{int(ratio_value[1])}:{int(ratio_value[0])}"
|
200 |
|
201 |
if ratio_value[1] > ratio_value[0]:
|
202 |
# Querformat
|
|
|
209 |
|
210 |
return {"choices": new_choises, "value": ratio_value_new, "label": new_label, "__type__": "update"}
|
211 |
|
212 |
+
def calculate_ratio_values(image_ratio_buttons, image_width, image_height):
|
213 |
+
ratio_value = image_ratio_buttons.split(":")
|
214 |
+
if int(ratio_value[0]) > int(ratio_value[1]):
|
215 |
+
a = 1024
|
216 |
+
b = int(a * int(ratio_value[1]) / int(ratio_value[0]))
|
217 |
+
new_width = a
|
218 |
+
new_height = b
|
219 |
+
elif int(ratio_value[0]) < int(ratio_value[1]):
|
220 |
+
b = 1024
|
221 |
+
a = int(b * int(ratio_value[0]) / int(ratio_value[1]))
|
222 |
+
new_width = a
|
223 |
+
new_height = b
|
224 |
+
else:
|
225 |
+
new_width = 1024
|
226 |
+
new_height = 1024
|
227 |
+
|
228 |
+
return new_width, new_height
|
229 |
|
230 |
switch_width_height.click(fn=switch_image_size_values, inputs=[image_width, image_height], outputs=[image_width, image_height], show_progress="hidden")
|
|
|
231 |
switch_width_height.click(fn=switch_image_ratio_buttons, inputs=[image_ratio_buttons], outputs=[image_ratio_buttons], show_progress="hidden")
|
232 |
|
233 |
+
image_ratio_buttons.input(fn=calculate_ratio_values, inputs=[image_ratio_buttons, image_width, image_height], outputs=[image_width, image_height], show_progress="hidden")
|
234 |
|
235 |
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, image_seed])
|
236 |
|
237 |
enhance_prompt_button.click(fn=groq_enhance_process, inputs=[text_prompt], outputs=[text_prompt])
|
238 |
random_prompt_button.click(fn=groq_enhance_process, inputs=None, outputs=[text_prompt])
|
239 |
|
240 |
+
# MARK: Gradio LAUNCH
|
241 |
demo.launch()
|