Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,62 +3,104 @@ import gradio as gr
|
|
| 3 |
import os
|
| 4 |
from diffusers import AutoPipelineForText2Image, DPMSolverMultistepScheduler
|
| 5 |
|
|
|
|
| 6 |
MODEL_ID = "Lykon/dreamshaper-xl-turbo"
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
#
|
| 17 |
-
|
| 18 |
-
"
|
| 19 |
-
"
|
| 20 |
-
"
|
| 21 |
-
"
|
|
|
|
|
|
|
| 22 |
}
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
if not prompt: return None
|
| 26 |
|
| 27 |
-
# รวม Prompt
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
|
| 31 |
with torch.no_grad():
|
| 32 |
image = pipe(
|
| 33 |
-
prompt=
|
| 34 |
negative_prompt=negative_prompt,
|
| 35 |
num_inference_steps=int(steps),
|
| 36 |
guidance_scale=float(cfg),
|
| 37 |
-
width=
|
| 38 |
-
height=
|
| 39 |
).images[0]
|
| 40 |
return image
|
| 41 |
|
|
|
|
| 42 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 43 |
-
gr.Markdown("### 🚀
|
| 44 |
|
| 45 |
with gr.Row():
|
| 46 |
with gr.Column():
|
| 47 |
-
prompt = gr.Textbox(label="
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
with gr.Accordion("Advanced Settings", open=False):
|
| 51 |
-
|
| 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 |
-
|
| 55 |
-
|
| 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(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
| 3 |
import os
|
| 4 |
from diffusers import AutoPipelineForText2Image, DPMSolverMultistepScheduler
|
| 5 |
|
| 6 |
+
# ใช้โมเดลเดิม
|
| 7 |
MODEL_ID = "Lykon/dreamshaper-xl-turbo"
|
| 8 |
|
| 9 |
+
print("Loading for Pure CPU Environment (No GPU)...")
|
| 10 |
+
|
| 11 |
+
# โหลดโมเดลแบบ CPU
|
| 12 |
+
pipe = AutoPipelineForText2Image.from_pretrained(
|
| 13 |
+
MODEL_ID,
|
| 14 |
+
torch_dtype=torch.float32,
|
| 15 |
+
low_cpu_mem_usage=True,
|
| 16 |
+
safety_checker=None,
|
| 17 |
+
requires_safety_checker=False
|
| 18 |
+
)
|
| 19 |
pipe.to("cpu")
|
| 20 |
+
|
| 21 |
+
# [SAFE CPU OPTIMIZATIONS]
|
| 22 |
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 23 |
pipe.enable_attention_slicing("max")
|
| 24 |
+
pipe.enable_vae_tiling()
|
| 25 |
+
torch.set_num_threads(os.cpu_count())
|
| 26 |
|
| 27 |
+
# พจนานุกรมสำหรับ Style
|
| 28 |
+
STYLE_TEMPLATES = {
|
| 29 |
+
"None": "{prompt}",
|
| 30 |
+
"Cinematic": "cinematic photo, {prompt}, high resolution, detailed, 8k, masterpiece",
|
| 31 |
+
"Digital Art": "digital art, {prompt}, vibrant colors, detailed, trending on artstation",
|
| 32 |
+
"Anime": "anime style, {prompt}, vivid, expressive, high quality art",
|
| 33 |
+
"Oil Painting": "oil painting, {prompt}, brush strokes, classical style, textured canvas",
|
| 34 |
+
"Photography": "professional photography, {prompt}, realistic, 8k uhd, dslr, high detail"
|
| 35 |
}
|
| 36 |
|
| 37 |
+
# ฟังก์ชันคำนวณสัดส่วนภาพ
|
| 38 |
+
def get_dimensions(ratio_str):
|
| 39 |
+
ratios = {
|
| 40 |
+
"1:1 (Square)": (512, 512),
|
| 41 |
+
"4:3 (Classic)": (512, 384),
|
| 42 |
+
"3:4 (Portrait)": (384, 512),
|
| 43 |
+
"16:9 (Widescreen)": (512, 288),
|
| 44 |
+
"9:16 (Vertical)": (288, 512)
|
| 45 |
+
}
|
| 46 |
+
return ratios.get(ratio_str, (512, 512))
|
| 47 |
+
|
| 48 |
+
def gen(prompt, negative_prompt, style, aspect_ratio, steps, cfg):
|
| 49 |
if not prompt: return None
|
| 50 |
|
| 51 |
+
# รวม Prompt กับ Style
|
| 52 |
+
final_prompt = STYLE_TEMPLATES[style].format(prompt=prompt)
|
| 53 |
+
|
| 54 |
+
# กำหนดขนาดภาพจาก Aspect Ratio
|
| 55 |
+
width, height = get_dimensions(aspect_ratio)
|
| 56 |
|
| 57 |
with torch.no_grad():
|
| 58 |
image = pipe(
|
| 59 |
+
prompt=final_prompt,
|
| 60 |
negative_prompt=negative_prompt,
|
| 61 |
num_inference_steps=int(steps),
|
| 62 |
guidance_scale=float(cfg),
|
| 63 |
+
width=width,
|
| 64 |
+
height=height
|
| 65 |
).images[0]
|
| 66 |
return image
|
| 67 |
|
| 68 |
+
# UI
|
| 69 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 70 |
+
gr.Markdown("### 🚀 Pure CPU Turbo (Style & Aspect Ratio Support)")
|
| 71 |
|
| 72 |
with gr.Row():
|
| 73 |
with gr.Column():
|
| 74 |
+
prompt = gr.Textbox(label="Prompt", lines=3, placeholder="What do you want to create?")
|
| 75 |
+
|
| 76 |
+
with gr.Row():
|
| 77 |
+
style_dropdown = gr.Dropdown(
|
| 78 |
+
choices=list(STYLE_TEMPLATES.keys()),
|
| 79 |
+
value="None",
|
| 80 |
+
label="Select Style"
|
| 81 |
+
)
|
| 82 |
+
ratio_dropdown = gr.Dropdown(
|
| 83 |
+
choices=["1:1 (Square)", "4:3 (Classic)", "3:4 (Portrait)", "16:9 (Widescreen)", "9:16 (Vertical)"],
|
| 84 |
+
value="1:1 (Square)",
|
| 85 |
+
label="Aspect Ratio"
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
negative = gr.Textbox(label="Negative", value="deformed, lowres, blurry, nsfw")
|
| 89 |
|
| 90 |
with gr.Accordion("Advanced Settings", open=False):
|
| 91 |
+
steps = gr.Slider(1, 10, 4, step=1, label="Steps (Turbo recommended 1-4)")
|
|
|
|
| 92 |
cfg = gr.Slider(0.0, 3.0, 1.2, step=0.1, label="CFG Scale")
|
| 93 |
+
|
| 94 |
+
btn = gr.Button("Generate", variant="primary")
|
|
|
|
|
|
|
| 95 |
|
| 96 |
with gr.Column():
|
| 97 |
output_img = gr.Image(label="Result")
|
| 98 |
|
| 99 |
+
btn.click(
|
| 100 |
+
fn=gen,
|
| 101 |
+
inputs=[prompt, negative, style_dropdown, ratio_dropdown, steps, cfg],
|
| 102 |
+
outputs=[output_img]
|
| 103 |
+
)
|
| 104 |
|
| 105 |
+
if __name__ == "__main__":
|
| 106 |
+
demo.launch()
|