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