Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,35 @@
|
|
| 1 |
import torch
|
| 2 |
import gradio as gr
|
| 3 |
from diffusers import DiffusionPipeline
|
| 4 |
-
import diffusers
|
| 5 |
import numpy as np
|
| 6 |
import random
|
| 7 |
|
| 8 |
# =========================================================
|
| 9 |
-
# MODEL CONFIGURATION
|
| 10 |
# =========================================================
|
| 11 |
MAX_SEED = np.iinfo(np.int32).max
|
| 12 |
-
# Turbo
|
| 13 |
-
DEFAULT_STEPS =
|
| 14 |
|
| 15 |
# =========================================================
|
| 16 |
-
# LOAD PIPELINE (CPU Optimized)
|
| 17 |
# =========================================================
|
| 18 |
-
print("Loading Z-Image-Turbo pipeline to CPU...")
|
| 19 |
|
| 20 |
-
# CPU ပေါ်တွင်
|
| 21 |
pipe = DiffusionPipeline.from_pretrained(
|
| 22 |
"Tongyi-MAI/Z-Image-Turbo",
|
| 23 |
torch_dtype=torch.float32,
|
| 24 |
low_cpu_mem_usage=True
|
| 25 |
)
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
pipe.enable_attention_slicing()
|
|
|
|
| 29 |
pipe.to("cpu")
|
| 30 |
|
| 31 |
# =========================================================
|
| 32 |
-
# PROMPT EXAMPLES
|
| 33 |
# =========================================================
|
| 34 |
prompt_examples = [
|
| 35 |
"Moody mature anime scene of two lovers kissing under neon rain, sensual atmosphere",
|
|
@@ -41,21 +41,18 @@ def get_random_prompt():
|
|
| 41 |
return random.choice(prompt_examples)
|
| 42 |
|
| 43 |
# =========================================================
|
| 44 |
-
# IMAGE GENERATOR
|
| 45 |
# =========================================================
|
| 46 |
-
def generate_image(prompt, height, width, num_inference_steps, seed, randomize_seed
|
| 47 |
if not prompt:
|
| 48 |
raise gr.Error("Please enter a prompt.")
|
| 49 |
|
| 50 |
if randomize_seed:
|
| 51 |
seed = random.randint(0, MAX_SEED)
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
num_images = min(max(1, int(num_images)), 2)
|
| 55 |
-
|
| 56 |
generator = torch.Generator("cpu").manual_seed(int(seed))
|
| 57 |
|
| 58 |
-
# CPU inference ဖြစ်၍ အချိန်ကြာနိုင်ကြောင်း သတိပြုပါ
|
| 59 |
result = pipe(
|
| 60 |
prompt=prompt,
|
| 61 |
height=int(height),
|
|
@@ -63,18 +60,16 @@ def generate_image(prompt, height, width, num_inference_steps, seed, randomize_s
|
|
| 63 |
num_inference_steps=int(num_inference_steps),
|
| 64 |
guidance_scale=0.0,
|
| 65 |
generator=generator,
|
| 66 |
-
|
| 67 |
-
num_images_per_prompt=num_images,
|
| 68 |
output_type="pil",
|
| 69 |
)
|
| 70 |
-
|
| 71 |
return result.images, seed
|
| 72 |
|
| 73 |
# ============================================
|
| 74 |
-
# 🎨 UI Design
|
| 75 |
# ============================================
|
| 76 |
css = """
|
| 77 |
-
/* User ပေးထားသော CSS ကို ဤနေရာတွင် ထည့်သွင်းထားသည် */
|
| 78 |
@import url('https://fonts.googleapis.com/css2?family=Bangers&family=Comic+Neue:wght@400;700&display=swap');
|
| 79 |
.gradio-container { background-color: #FEF9C3 !important; font-family: 'Comic Neue', cursive !important; }
|
| 80 |
.header-text h1 { font-family: 'Bangers', cursive !important; text-align: center; font-size: 3rem; }
|
|
@@ -82,43 +77,38 @@ css = """
|
|
| 82 |
"""
|
| 83 |
|
| 84 |
with gr.Blocks(css=css) as demo:
|
| 85 |
-
gr.Markdown("#
|
| 86 |
|
| 87 |
with gr.Row():
|
| 88 |
with gr.Column():
|
| 89 |
-
prompt_input = gr.Textbox(label="✏️ Prompt", lines=3)
|
| 90 |
random_button = gr.Button("🎲 RANDOM PROMPT")
|
| 91 |
|
| 92 |
with gr.Row():
|
| 93 |
-
|
| 94 |
-
|
|
|
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
steps_slider = gr.Slider(1, 10, DEFAULT_STEPS, step=1, label="Steps (Keep low for CPU)")
|
| 100 |
seed_input = gr.Number(value=42, label="Seed")
|
| 101 |
randomize_seed_checkbox = gr.Checkbox(label="Randomize Seed", value=True)
|
| 102 |
-
|
| 103 |
-
generate_button = gr.Button("✨ GENERATE", variant="primary")
|
| 104 |
|
| 105 |
with gr.Column():
|
| 106 |
-
output_gallery = gr.Gallery(label="
|
| 107 |
used_seed_output = gr.Number(label="Seed Used")
|
| 108 |
|
| 109 |
random_button.click(fn=get_random_prompt, outputs=[prompt_input])
|
|
|
|
|
|
|
| 110 |
generate_button.click(
|
| 111 |
fn=generate_image,
|
| 112 |
-
inputs=[prompt_input, height_input, width_input, steps_slider, seed_input, randomize_seed_checkbox
|
| 113 |
outputs=[output_gallery, used_seed_output]
|
| 114 |
)
|
| 115 |
|
| 116 |
if __name__ == "__main__":
|
| 117 |
-
|
| 118 |
-
demo.queue(max_size=10).launch(
|
| 119 |
-
debug=False,
|
| 120 |
-
share=False # Hugging Face Space မှာ run ရင် share=True လုပ်စရာမလိုပါ
|
| 121 |
-
)
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
| 1 |
import torch
|
| 2 |
import gradio as gr
|
| 3 |
from diffusers import DiffusionPipeline
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
import random
|
| 6 |
|
| 7 |
# =========================================================
|
| 8 |
+
# MODEL CONFIGURATION (အမြန်ဆုံးနှုန်းအတွက် ပြင်ဆင်ချက်)
|
| 9 |
# =========================================================
|
| 10 |
MAX_SEED = np.iinfo(np.int32).max
|
| 11 |
+
# Turbo မော်ဒယ်ဖြစ်သောကြောင့် အလွန်မြန်ဆန်စေရန် (၂) ဆင့်သာ သုံးပါမည်
|
| 12 |
+
DEFAULT_STEPS = 2
|
| 13 |
|
| 14 |
# =========================================================
|
| 15 |
+
# LOAD PIPELINE (Ultra CPU Optimized for Speed)
|
| 16 |
# =========================================================
|
| 17 |
+
print("Loading Z-Image-Turbo pipeline to CPU for MAXIMUM SPEED...")
|
| 18 |
|
| 19 |
+
# CPU ပေါ်တွင် အမြန်ဆုံး အလုပ်လုပ်ရန် float32 သုံးသည်
|
| 20 |
pipe = DiffusionPipeline.from_pretrained(
|
| 21 |
"Tongyi-MAI/Z-Image-Turbo",
|
| 22 |
torch_dtype=torch.float32,
|
| 23 |
low_cpu_mem_usage=True
|
| 24 |
)
|
| 25 |
|
| 26 |
+
# ⚠️ အရေးကြီးသော ပြင်ဆင်ချက်:
|
| 27 |
+
# pipe.enable_attention_slicing() ကို အမြန်နှုန်းအတွက် တမင်ဖယ်ရှားထားပါသည်။
|
| 28 |
+
# ၎င်းသည် Memory ကို ချွေတာပေးသော်လည်း အချိန်ပိုကြာစေသောကြောင့် ဖြစ်သည်။
|
| 29 |
pipe.to("cpu")
|
| 30 |
|
| 31 |
# =========================================================
|
| 32 |
+
# PROMPT EXAMPLES
|
| 33 |
# =========================================================
|
| 34 |
prompt_examples = [
|
| 35 |
"Moody mature anime scene of two lovers kissing under neon rain, sensual atmosphere",
|
|
|
|
| 41 |
return random.choice(prompt_examples)
|
| 42 |
|
| 43 |
# =========================================================
|
| 44 |
+
# IMAGE GENERATOR (Speed Focused)
|
| 45 |
# =========================================================
|
| 46 |
+
def generate_image(prompt, height, width, num_inference_steps, seed, randomize_seed):
|
| 47 |
if not prompt:
|
| 48 |
raise gr.Error("Please enter a prompt.")
|
| 49 |
|
| 50 |
if randomize_seed:
|
| 51 |
seed = random.randint(0, MAX_SEED)
|
| 52 |
|
| 53 |
+
# အမြန်ဆုံးဖြစ်စေရန် ပုံ (၁) ပုံတည်းကိုသာ အတင်းအကျပ် ဖန်တီးခိုင်းပါမည်
|
|
|
|
|
|
|
| 54 |
generator = torch.Generator("cpu").manual_seed(int(seed))
|
| 55 |
|
|
|
|
| 56 |
result = pipe(
|
| 57 |
prompt=prompt,
|
| 58 |
height=int(height),
|
|
|
|
| 60 |
num_inference_steps=int(num_inference_steps),
|
| 61 |
guidance_scale=0.0,
|
| 62 |
generator=generator,
|
| 63 |
+
num_images_per_prompt=1, # အမြန်နှုန်းအတွက် ၁ ပုံသာ
|
|
|
|
| 64 |
output_type="pil",
|
| 65 |
)
|
| 66 |
+
|
| 67 |
return result.images, seed
|
| 68 |
|
| 69 |
# ============================================
|
| 70 |
+
# 🎨 UI Design
|
| 71 |
# ============================================
|
| 72 |
css = """
|
|
|
|
| 73 |
@import url('https://fonts.googleapis.com/css2?family=Bangers&family=Comic+Neue:wght@400;700&display=swap');
|
| 74 |
.gradio-container { background-color: #FEF9C3 !important; font-family: 'Comic Neue', cursive !important; }
|
| 75 |
.header-text h1 { font-family: 'Bangers', cursive !important; text-align: center; font-size: 3rem; }
|
|
|
|
| 77 |
"""
|
| 78 |
|
| 79 |
with gr.Blocks(css=css) as demo:
|
| 80 |
+
gr.Markdown("# ⚡ AI Image Generator (Ultra Fast CPU)", elem_classes="header-text")
|
| 81 |
|
| 82 |
with gr.Row():
|
| 83 |
with gr.Column():
|
| 84 |
+
prompt_input = gr.Textbox(label="✏️ The Vision (Prompt)", lines=3)
|
| 85 |
random_button = gr.Button("🎲 RANDOM PROMPT")
|
| 86 |
|
| 87 |
with gr.Row():
|
| 88 |
+
# ပုံသေးလေ ပိုမြန်လေဖြစ်၍ Default ကို 512 အစား 384 သို့ လျှော့ချထားပါသည်
|
| 89 |
+
height_input = gr.Slider(256, 1024, 384, step=64, label="Height")
|
| 90 |
+
width_input = gr.Slider(256, 1024, 384, step=64, label="Width")
|
| 91 |
|
| 92 |
+
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 93 |
+
# Turbo မော်ဒယ်အတွက် အမြင့်ဆုံး ၁၀ ဆင့်ထိသာ ပေးထားသည်
|
| 94 |
+
steps_slider = gr.Slider(1, 5, DEFAULT_STEPS, step=1, label="Steps (Lower = Faster)")
|
|
|
|
| 95 |
seed_input = gr.Number(value=42, label="Seed")
|
| 96 |
randomize_seed_checkbox = gr.Checkbox(label="Randomize Seed", value=True)
|
| 97 |
+
|
| 98 |
+
generate_button = gr.Button("✨ အလျင်အမြန် ဖန်တီးမည် (GENERATE FAST)", variant="primary")
|
| 99 |
|
| 100 |
with gr.Column():
|
| 101 |
+
output_gallery = gr.Gallery(label="The Masterpiece", columns=1)
|
| 102 |
used_seed_output = gr.Number(label="Seed Used")
|
| 103 |
|
| 104 |
random_button.click(fn=get_random_prompt, outputs=[prompt_input])
|
| 105 |
+
|
| 106 |
+
# UI တွင် Image Count ကို ဖယ်ရှားပြီး Code ထဲတွင် ၁ ပုံတည်းဟု Fix လုပ်ထားသည်
|
| 107 |
generate_button.click(
|
| 108 |
fn=generate_image,
|
| 109 |
+
inputs=[prompt_input, height_input, width_input, steps_slider, seed_input, randomize_seed_checkbox],
|
| 110 |
outputs=[output_gallery, used_seed_output]
|
| 111 |
)
|
| 112 |
|
| 113 |
if __name__ == "__main__":
|
| 114 |
+
demo.launch(debug=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|