Spaces:
Running
Running
add mac mps
Browse files
app.py
CHANGED
@@ -1,4 +1,6 @@
|
|
1 |
import os
|
|
|
|
|
2 |
|
3 |
def is_running_in_huggingface_spaces():
|
4 |
return "SPACE_ID" in os.environ
|
@@ -24,7 +26,13 @@ try:
|
|
24 |
lang=config.lang
|
25 |
except ImportError:
|
26 |
is_config_ui_installed=False
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
lang='EN'
|
29 |
|
30 |
try:
|
@@ -42,6 +50,9 @@ max_target_length = 256
|
|
42 |
prefix = "enhance prompt"
|
43 |
|
44 |
def enhance_prompt(prompt, system_prompt, temperature=0.5, repetition_penalty=1.2, seed=-1, is_rnd_seed=True):
|
|
|
|
|
|
|
45 |
|
46 |
if is_rnd_seed or seed==-1:
|
47 |
seed = torch.randint(0, 2**32 - 1, (1,)).item()
|
@@ -73,8 +84,12 @@ def enhance_prompt(prompt, system_prompt, temperature=0.5, repetition_penalty=1.
|
|
73 |
result_output_ru = translator.translate_eng2ru(generated_text_en)
|
74 |
else:
|
75 |
result_output_ru=generated_text_en
|
|
|
|
|
|
|
|
|
76 |
|
77 |
-
return seed, generated_text_en, result_output_ru
|
78 |
|
79 |
def random_prompt():
|
80 |
rnd_prompt_str=generate_random_portrait_prompt()
|
@@ -176,8 +191,8 @@ def set_initial():
|
|
176 |
device_name = f"GPU: {device_name}"
|
177 |
dev="cuda"
|
178 |
else:
|
179 |
-
device_name = "
|
180 |
-
return gr.update(value=lang), gr.update(value=dev), f'{device_name}
|
181 |
|
182 |
|
183 |
# Настройка интерфейса Gradio
|
@@ -196,8 +211,14 @@ with gr.Blocks(title="Flux Prompt Enhance",
|
|
196 |
radio_lang = gr.Radio(choices = ["RU", "EN"], show_label = False, container = False, type = "value",
|
197 |
visible = True if is_google_translate_installed else False)
|
198 |
|
199 |
-
|
200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
201 |
save_button = gr.Button(LABELS["save_button"], visible= True if is_config_ui_installed else False)
|
202 |
|
203 |
with gr.Row(variant="default"):
|
@@ -227,28 +248,44 @@ with gr.Blocks(title="Flux Prompt Enhance",
|
|
227 |
# Кнопка генерации
|
228 |
with gr.Row(variant="default"):
|
229 |
generate_button = gr.Button(LABELS["generate_button"], variant="primary", size="lg")
|
230 |
-
generate_button.click(fn=enhance_prompt, inputs=[prompt_input,system_prompt,temperature,repetition_penalty,seed_output,is_rnd_seed],
|
231 |
-
outputs=[seed_output, result_output, result_output_ru])
|
232 |
|
233 |
# Кнопка копирования в буфер обмена
|
234 |
copy_button = gr.Button(LABELS["copy_button"], variant="secondary")
|
235 |
copy_button.click(fn=copy_to_clipboard, inputs=result_output, outputs=[],js="(text) => navigator.clipboard.writeText(text)")
|
|
|
236 |
with gr.Row(variant="default"):
|
237 |
-
log_text = gr.Textbox(label="")
|
238 |
|
239 |
if is_config_ui_installed:
|
240 |
save_button.click(fn=save_config, inputs=[], outputs=log_text)
|
241 |
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
if is_google_translate_installed:
|
246 |
radio_lang.change(process_lang, inputs=radio_lang,
|
247 |
outputs=[log_text,generate_button, copy_button, save_button, prompt_input, seed_output, is_rnd_seed,
|
248 |
result_output, result_output_ru,AccordionAdvanced,system_prompt, temperature, repetition_penalty])
|
249 |
|
250 |
radio_gpu.change(process_gpu, inputs=radio_gpu, outputs=log_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
251 |
|
252 |
|
253 |
-
|
254 |
-
demo.launch()
|
|
|
1 |
import os
|
2 |
+
import time
|
3 |
+
import platform
|
4 |
|
5 |
def is_running_in_huggingface_spaces():
|
6 |
return "SPACE_ID" in os.environ
|
|
|
26 |
lang=config.lang
|
27 |
except ImportError:
|
28 |
is_config_ui_installed=False
|
29 |
+
if platform.system() == "Darwin" and platform.machine().startswith("arm"):
|
30 |
+
print("run on mac with Apple Silicon")
|
31 |
+
if torch.backends.mps.is_available():
|
32 |
+
device = torch.device("mps") # MPS = Metal Performance Shaders
|
33 |
+
else:
|
34 |
+
#TODO parse env var to assign cuda device
|
35 |
+
device = 0 if torch.cuda.is_available() else "cpu"
|
36 |
lang='EN'
|
37 |
|
38 |
try:
|
|
|
50 |
prefix = "enhance prompt"
|
51 |
|
52 |
def enhance_prompt(prompt, system_prompt, temperature=0.5, repetition_penalty=1.2, seed=-1, is_rnd_seed=True):
|
53 |
+
global lang
|
54 |
+
start_time = time.time() # Начало замера времени
|
55 |
+
|
56 |
|
57 |
if is_rnd_seed or seed==-1:
|
58 |
seed = torch.randint(0, 2**32 - 1, (1,)).item()
|
|
|
84 |
result_output_ru = translator.translate_eng2ru(generated_text_en)
|
85 |
else:
|
86 |
result_output_ru=generated_text_en
|
87 |
+
|
88 |
+
end_time = time.time() # Конец замера времени
|
89 |
+
execution_time = end_time - start_time
|
90 |
+
time_str=f"execution time: {execution_time:.2f} s." if lang=="EN" else f"время выполнения: {execution_time:.2f} с."
|
91 |
|
92 |
+
return seed, generated_text_en, result_output_ru, time_str
|
93 |
|
94 |
def random_prompt():
|
95 |
rnd_prompt_str=generate_random_portrait_prompt()
|
|
|
191 |
device_name = f"GPU: {device_name}"
|
192 |
dev="cuda"
|
193 |
else:
|
194 |
+
device_name = "running on CPU"
|
195 |
+
return gr.update(value=lang), gr.update(value=dev), f'{device_name}, set to "{lang}" language'
|
196 |
|
197 |
|
198 |
# Настройка интерфейса Gradio
|
|
|
211 |
radio_lang = gr.Radio(choices = ["RU", "EN"], show_label = False, container = False, type = "value",
|
212 |
visible = True if is_google_translate_installed else False)
|
213 |
|
214 |
+
if platform.system() == "Darwin" and platform.machine().startswith("arm") and torch.backends.mps.is_available():
|
215 |
+
print("radio_gpu mac")
|
216 |
+
radio_gpu = gr.Radio(choices = ["mps","cpu"], show_label = False, container = False, type = "value",
|
217 |
+
visible = True)
|
218 |
+
else:
|
219 |
+
radio_gpu = gr.Radio(choices = ["cuda","cpu"], show_label = False, container = False, type = "value",
|
220 |
+
visible = True if torch.cuda.is_available() else False)
|
221 |
+
|
222 |
save_button = gr.Button(LABELS["save_button"], visible= True if is_config_ui_installed else False)
|
223 |
|
224 |
with gr.Row(variant="default"):
|
|
|
248 |
# Кнопка генерации
|
249 |
with gr.Row(variant="default"):
|
250 |
generate_button = gr.Button(LABELS["generate_button"], variant="primary", size="lg")
|
|
|
|
|
251 |
|
252 |
# Кнопка копирования в буфер обмена
|
253 |
copy_button = gr.Button(LABELS["copy_button"], variant="secondary")
|
254 |
copy_button.click(fn=copy_to_clipboard, inputs=result_output, outputs=[],js="(text) => navigator.clipboard.writeText(text)")
|
255 |
+
|
256 |
with gr.Row(variant="default"):
|
257 |
+
log_text = gr.Textbox(label="", container=False)
|
258 |
|
259 |
if is_config_ui_installed:
|
260 |
save_button.click(fn=save_config, inputs=[], outputs=log_text)
|
261 |
|
262 |
+
generate_button.click(fn=enhance_prompt, inputs=[prompt_input,system_prompt,temperature,repetition_penalty,seed_output,is_rnd_seed],
|
263 |
+
outputs=[seed_output, result_output, result_output_ru,log_text])
|
264 |
+
|
265 |
if is_google_translate_installed:
|
266 |
radio_lang.change(process_lang, inputs=radio_lang,
|
267 |
outputs=[log_text,generate_button, copy_button, save_button, prompt_input, seed_output, is_rnd_seed,
|
268 |
result_output, result_output_ru,AccordionAdvanced,system_prompt, temperature, repetition_penalty])
|
269 |
|
270 |
radio_gpu.change(process_gpu, inputs=radio_gpu, outputs=log_text)
|
271 |
+
|
272 |
+
#preload values for lang
|
273 |
+
demo.load(set_initial, outputs=[radio_lang, radio_gpu, log_text])
|
274 |
+
|
275 |
+
|
276 |
+
launch_args={}
|
277 |
+
|
278 |
+
if not is_running_in_huggingface_spaces():
|
279 |
+
launch_args["share"]=False
|
280 |
+
launch_args["server_name"]="0.0.0.0"
|
281 |
+
launch_args["inbrowser"] = True
|
282 |
+
launch_args["favicon_path"] = "./static/favicon_aicave.png"
|
283 |
+
launch_args["show_api"]=True
|
284 |
+
|
285 |
+
if os.path.exists("cert.pem") and os.path.exists("key.pem"):
|
286 |
+
launch_args["ssl_certfile"]="cert.pem"
|
287 |
+
launch_args["ssl_keyfile"]="key.pem"
|
288 |
+
launch_args["ssl_verify"]=False
|
289 |
|
290 |
|
291 |
+
demo.launch(**launch_args)
|
|