Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,29 +3,35 @@ import gradio as gr
|
|
| 3 |
import os
|
| 4 |
from diffusers import AutoPipelineForText2Image, DPMSolverMultistepScheduler
|
| 5 |
|
| 6 |
-
|
| 7 |
-
MODEL_ID = "Tongyi-MAI/Z-Image-Turbo"
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
pipe = AutoPipelineForText2Image.from_pretrained(
|
| 12 |
-
MODEL_ID,
|
| 13 |
-
torch_dtype=torch.float32,
|
| 14 |
-
low_cpu_mem_usage=True
|
| 15 |
-
)
|
| 16 |
pipe.to("cpu")
|
| 17 |
-
|
| 18 |
-
# มาตรการ Never OOM สำหรับเครื่องฟรี
|
| 19 |
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 20 |
pipe.enable_attention_slicing("max")
|
| 21 |
pipe.enable_vae_tiling()
|
| 22 |
torch.set_num_threads(os.cpu_count())
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
if not prompt: return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
with torch.no_grad():
|
| 27 |
image = pipe(
|
| 28 |
-
prompt=
|
|
|
|
| 29 |
num_inference_steps=int(steps),
|
| 30 |
guidance_scale=float(cfg),
|
| 31 |
width=int(width),
|
|
@@ -33,20 +39,26 @@ def gen(prompt, steps, cfg, width, height):
|
|
| 33 |
).images[0]
|
| 34 |
return image
|
| 35 |
|
| 36 |
-
# UI แบบประหยัด RAM
|
| 37 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 38 |
-
gr.Markdown(
|
|
|
|
| 39 |
with gr.Row():
|
| 40 |
with gr.Column():
|
| 41 |
-
prompt = gr.Textbox(label="
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
with gr.Column():
|
| 48 |
output_img = gr.Image(label="Result")
|
| 49 |
|
| 50 |
-
btn.click(fn=gen, inputs=[prompt, steps, cfg, width, height], outputs=[output_img])
|
| 51 |
|
| 52 |
demo.launch()
|
|
|
|
| 3 |
import os
|
| 4 |
from diffusers import AutoPipelineForText2Image, DPMSolverMultistepScheduler
|
| 5 |
|
| 6 |
+
MODEL_ID = "Lykon/dreamshaper-xl-turbo"
|
|
|
|
| 7 |
|
| 8 |
+
# โหลดโมเดลและตั้งค่า Never-OOM
|
| 9 |
+
pipe = AutoPipelineForText2Image.from_pretrained(MODEL_ID, torch_dtype=torch.float32, low_cpu_mem_usage=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
pipe.to("cpu")
|
|
|
|
|
|
|
| 11 |
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 12 |
pipe.enable_attention_slicing("max")
|
| 13 |
pipe.enable_vae_tiling()
|
| 14 |
torch.set_num_threads(os.cpu_count())
|
| 15 |
|
| 16 |
+
# ดิกชันนารีเก็บสไตล์ (Prompt สำเร็จรูป)
|
| 17 |
+
STYLE_MAP = {
|
| 18 |
+
"สมจริง (Cinematic)": "highly detailed film still, cinematic lighting, masterpiece, 8k, realistic skin, professional photography",
|
| 19 |
+
"อนิเมะ (Anime)": "anime style, high quality, vibrant colors, clean lines, masterpiece, aesthetic",
|
| 20 |
+
"วาดเขียน (Digital Art)": "detailed digital illustration, sharp focus, artistic, vibrant, concept art",
|
| 21 |
+
"ไม่เน้นสไตล์": ""
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
def gen(prompt, style_name, negative_prompt, steps, cfg, width, height):
|
| 25 |
if not prompt: return None
|
| 26 |
+
|
| 27 |
+
# รวม Prompt จากปุ่มเลือกสไตล์
|
| 28 |
+
style_prompt = STYLE_MAP.get(style_name, "")
|
| 29 |
+
full_prompt = f"{prompt}, {style_prompt}"
|
| 30 |
+
|
| 31 |
with torch.no_grad():
|
| 32 |
image = pipe(
|
| 33 |
+
prompt=full_prompt,
|
| 34 |
+
negative_prompt=negative_prompt,
|
| 35 |
num_inference_steps=int(steps),
|
| 36 |
guidance_scale=float(cfg),
|
| 37 |
width=int(width),
|
|
|
|
| 39 |
).images[0]
|
| 40 |
return image
|
| 41 |
|
|
|
|
| 42 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 43 |
+
gr.Markdown("### 🚀 Easy One-Click CPU Turbo")
|
| 44 |
+
|
| 45 |
with gr.Row():
|
| 46 |
with gr.Column():
|
| 47 |
+
prompt = gr.Textbox(label="1. พิมพ์คำสั้นๆ (เช่น woman, city)", lines=2)
|
| 48 |
+
style_name = gr.Radio(choices=list(STYLE_MAP.keys()), value="สมจริง (Cinematic)", label="2. เลือกสไตล์ที่ต้องการ")
|
| 49 |
+
|
| 50 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 51 |
+
negative = gr.Textbox(label="Negative", value="low quality, blurry, deformed, messy")
|
| 52 |
+
steps = gr.Slider(1, 10, 4, step=1, label="Steps (แนะนำ 4-6)")
|
| 53 |
+
cfg = gr.Slider(0.0, 3.0, 1.2, step=0.1, label="CFG Scale")
|
| 54 |
+
width = gr.Slider(256, 512, 384, step=64, label="Width")
|
| 55 |
+
height = gr.Slider(256, 512, 512, step=64, label="Height")
|
| 56 |
+
|
| 57 |
+
btn = gr.Button("Generate Now", variant="primary")
|
| 58 |
+
|
| 59 |
with gr.Column():
|
| 60 |
output_img = gr.Image(label="Result")
|
| 61 |
|
| 62 |
+
btn.click(fn=gen, inputs=[prompt, style_name, negative, steps, cfg, width, height], outputs=[output_img])
|
| 63 |
|
| 64 |
demo.launch()
|