Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,11 +4,11 @@ import sys
|
|
| 4 |
import importlib.util
|
| 5 |
import time
|
| 6 |
|
| 7 |
-
#
|
| 8 |
os.environ["TRANSFORMERS_NO_ADVISORY_WARNINGS"] = "1"
|
| 9 |
os.environ["TRANSFORMERS_COMPILER_DISABLED"] = "1"
|
| 10 |
|
| 11 |
-
#
|
| 12 |
def install_required_packages():
|
| 13 |
required_packages = [
|
| 14 |
"warmup_scheduler",
|
|
@@ -129,10 +129,6 @@ import gradio as gr
|
|
| 129 |
import spaces
|
| 130 |
from huggingface_hub import hf_hub_url
|
| 131 |
from huggingface_hub import hf_hub_download
|
| 132 |
-
from transformers import pipeline
|
| 133 |
-
|
| 134 |
-
# Initialize the translation pipeline
|
| 135 |
-
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
|
| 136 |
|
| 137 |
def parse_args():
|
| 138 |
parser = argparse.ArgumentParser()
|
|
@@ -164,26 +160,6 @@ def load_message(height, width, seed, prompt, args, stage_a_tiled):
|
|
| 164 |
args.stage_a_tiled = stage_a_tiled
|
| 165 |
return args
|
| 166 |
|
| 167 |
-
def is_korean(text):
|
| 168 |
-
return any('\uac00' <= char <= '\ud7a3' for char in text)
|
| 169 |
-
|
| 170 |
-
def translate_if_korean(text, translation_display=None):
|
| 171 |
-
if is_korean(text):
|
| 172 |
-
translated = translator(text, max_length=512)[0]['translation_text']
|
| 173 |
-
print(f"Translated from Korean: {text} -> {translated}")
|
| 174 |
-
|
| 175 |
-
# If translation_display is provided, update it to show the translation
|
| 176 |
-
if translation_display is not None:
|
| 177 |
-
translation_display.update(value=f"Original (Korean): {text}\nTranslation (English): {translated}", visible=True)
|
| 178 |
-
|
| 179 |
-
return translated
|
| 180 |
-
|
| 181 |
-
# If not Korean, hide the translation display if it exists
|
| 182 |
-
if translation_display is not None:
|
| 183 |
-
translation_display.update(visible=False)
|
| 184 |
-
|
| 185 |
-
return text
|
| 186 |
-
|
| 187 |
@spaces.GPU(duration=120)
|
| 188 |
def get_image(height, width, seed, prompt, cfg, timesteps, stage_a_tiled):
|
| 189 |
global args
|
|
@@ -242,13 +218,6 @@ def get_image(height, width, seed, prompt, cfg, timesteps, stage_a_tiled):
|
|
| 242 |
|
| 243 |
return imgs[0]
|
| 244 |
|
| 245 |
-
def process_and_generate(height, width, seed, prompt, cfg, timesteps, stage_a_tiled, translation_display):
|
| 246 |
-
# First translate if needed and show the translation
|
| 247 |
-
translated_prompt = translate_if_korean(prompt, translation_display)
|
| 248 |
-
|
| 249 |
-
# Then generate the image
|
| 250 |
-
return get_image(height, width, seed, translated_prompt, cfg, timesteps, stage_a_tiled)
|
| 251 |
-
|
| 252 |
css = """
|
| 253 |
footer {
|
| 254 |
visibility: hidden;
|
|
@@ -347,17 +316,6 @@ input[type="text"]:focus {
|
|
| 347 |
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
|
| 348 |
}
|
| 349 |
|
| 350 |
-
/* Language indicator */
|
| 351 |
-
.language-indicator {
|
| 352 |
-
background-color: #e6f7ff;
|
| 353 |
-
color: #0072b5;
|
| 354 |
-
border-radius: 20px;
|
| 355 |
-
padding: 2px 10px;
|
| 356 |
-
font-size: 0.8rem;
|
| 357 |
-
margin-left: 10px;
|
| 358 |
-
display: inline-block;
|
| 359 |
-
}
|
| 360 |
-
|
| 361 |
/* Loading animation */
|
| 362 |
@keyframes pulse {
|
| 363 |
0% { opacity: 0.6; }
|
|
@@ -384,7 +342,7 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
|
|
| 384 |
prompt = gr.Textbox(
|
| 385 |
label="Text Prompt",
|
| 386 |
max_lines=2,
|
| 387 |
-
placeholder="Enter your prompt
|
| 388 |
elem_id="prompt-input"
|
| 389 |
)
|
| 390 |
|
|
@@ -392,17 +350,6 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
|
|
| 392 |
generate_button = gr.Button("Generate Image", variant="primary", elem_id="generate-btn")
|
| 393 |
clear_button = gr.Button("Clear", elem_id="clear-btn")
|
| 394 |
|
| 395 |
-
# Language detection indicator
|
| 396 |
-
with gr.Row(visible=False) as language_indicator:
|
| 397 |
-
gr.Markdown('<div class="language-indicator">Korean detected - will translate to English</div>')
|
| 398 |
-
|
| 399 |
-
# Translation display component
|
| 400 |
-
with gr.Row(visible=False) as translation_display:
|
| 401 |
-
translation_text = gr.Textbox(
|
| 402 |
-
label="Translation Information",
|
| 403 |
-
interactive=False
|
| 404 |
-
)
|
| 405 |
-
|
| 406 |
# Loading indicator
|
| 407 |
with gr.Row(visible=False) as loading_indicator:
|
| 408 |
gr.Markdown('<div class="loading">Generating your ultra high resolution image... This may take a minute...</div>')
|
|
@@ -476,15 +423,15 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
|
|
| 476 |
gr.TabItem("Nature", gr.Examples(
|
| 477 |
examples=[
|
| 478 |
"A detailed view of a blooming magnolia tree, with large, white flowers and dark green leaves, set against a clear blue sky.",
|
| 479 |
-
"
|
| 480 |
],
|
| 481 |
inputs=[prompt],
|
| 482 |
outputs=[output_img],
|
| 483 |
)),
|
| 484 |
gr.TabItem("Animals", gr.Examples(
|
| 485 |
examples=[
|
| 486 |
-
"
|
| 487 |
-
"
|
| 488 |
],
|
| 489 |
inputs=[prompt],
|
| 490 |
outputs=[output_img],
|
|
@@ -524,20 +471,13 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css) as demo:
|
|
| 524 |
preset_square.click(set_square, outputs=[width, height])
|
| 525 |
preset_ultrawide.click(set_ultrawide, outputs=[width, height])
|
| 526 |
|
| 527 |
-
# Function to show Korean detection indicator
|
| 528 |
-
def check_korean(text):
|
| 529 |
-
if any('\uac00' <= char <= '\ud7a3' for char in text):
|
| 530 |
-
return gr.update(visible=True)
|
| 531 |
-
return gr.update(visible=False)
|
| 532 |
-
|
| 533 |
# Connect events
|
| 534 |
-
prompt.change(check_korean, inputs=[prompt], outputs=[language_indicator])
|
| 535 |
generate_button.click(
|
| 536 |
lambda: gr.update(visible=True),
|
| 537 |
outputs=[loading_indicator]
|
| 538 |
).then(
|
| 539 |
-
|
| 540 |
-
inputs=[height, width, seed, prompt, cfg, timesteps, stage_a_tiled
|
| 541 |
outputs=[output_img]
|
| 542 |
).then(
|
| 543 |
lambda: gr.update(visible=False),
|
|
|
|
| 4 |
import importlib.util
|
| 5 |
import time
|
| 6 |
|
| 7 |
+
# Set environment variables
|
| 8 |
os.environ["TRANSFORMERS_NO_ADVISORY_WARNINGS"] = "1"
|
| 9 |
os.environ["TRANSFORMERS_COMPILER_DISABLED"] = "1"
|
| 10 |
|
| 11 |
+
# Function to install required packages
|
| 12 |
def install_required_packages():
|
| 13 |
required_packages = [
|
| 14 |
"warmup_scheduler",
|
|
|
|
| 129 |
import spaces
|
| 130 |
from huggingface_hub import hf_hub_url
|
| 131 |
from huggingface_hub import hf_hub_download
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
def parse_args():
|
| 134 |
parser = argparse.ArgumentParser()
|
|
|
|
| 160 |
args.stage_a_tiled = stage_a_tiled
|
| 161 |
return args
|
| 162 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
@spaces.GPU(duration=120)
|
| 164 |
def get_image(height, width, seed, prompt, cfg, timesteps, stage_a_tiled):
|
| 165 |
global args
|
|
|
|
| 218 |
|
| 219 |
return imgs[0]
|
| 220 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
css = """
|
| 222 |
footer {
|
| 223 |
visibility: hidden;
|
|
|
|
| 316 |
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
|
| 317 |
}
|
| 318 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 319 |
/* Loading animation */
|
| 320 |
@keyframes pulse {
|
| 321 |
0% { opacity: 0.6; }
|
|
|
|
| 342 |
prompt = gr.Textbox(
|
| 343 |
label="Text Prompt",
|
| 344 |
max_lines=2,
|
| 345 |
+
placeholder="Enter your prompt (e.g., 'A majestic mountain landscape with snow')",
|
| 346 |
elem_id="prompt-input"
|
| 347 |
)
|
| 348 |
|
|
|
|
| 350 |
generate_button = gr.Button("Generate Image", variant="primary", elem_id="generate-btn")
|
| 351 |
clear_button = gr.Button("Clear", elem_id="clear-btn")
|
| 352 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
# Loading indicator
|
| 354 |
with gr.Row(visible=False) as loading_indicator:
|
| 355 |
gr.Markdown('<div class="loading">Generating your ultra high resolution image... This may take a minute...</div>')
|
|
|
|
| 423 |
gr.TabItem("Nature", gr.Examples(
|
| 424 |
examples=[
|
| 425 |
"A detailed view of a blooming magnolia tree, with large, white flowers and dark green leaves, set against a clear blue sky.",
|
| 426 |
+
"A majestic view of snow-covered mountains with a calm lake against a blue sky background",
|
| 427 |
],
|
| 428 |
inputs=[prompt],
|
| 429 |
outputs=[output_img],
|
| 430 |
)),
|
| 431 |
gr.TabItem("Animals", gr.Examples(
|
| 432 |
examples=[
|
| 433 |
+
"A crocodile wearing a sweater",
|
| 434 |
+
"A cute golden retriever puppy chasing a red ball on a green lawn",
|
| 435 |
],
|
| 436 |
inputs=[prompt],
|
| 437 |
outputs=[output_img],
|
|
|
|
| 471 |
preset_square.click(set_square, outputs=[width, height])
|
| 472 |
preset_ultrawide.click(set_ultrawide, outputs=[width, height])
|
| 473 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 474 |
# Connect events
|
|
|
|
| 475 |
generate_button.click(
|
| 476 |
lambda: gr.update(visible=True),
|
| 477 |
outputs=[loading_indicator]
|
| 478 |
).then(
|
| 479 |
+
get_image,
|
| 480 |
+
inputs=[height, width, seed, prompt, cfg, timesteps, stage_a_tiled],
|
| 481 |
outputs=[output_img]
|
| 482 |
).then(
|
| 483 |
lambda: gr.update(visible=False),
|