Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,36 @@
|
|
| 1 |
-
# app.py - نسخه
|
| 2 |
-
from diffusers import StableDiffusionXLPipeline
|
| 3 |
-
from accelerate import cpu_offload # ← این خط حیاتیه!
|
| 4 |
import torch
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
-
import os
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
from accelerate
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
# لود مدل
|
| 12 |
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 13 |
"stabilityai/stable-diffusion-xl-base-1.0",
|
| 14 |
-
torch_dtype=torch.float32,
|
| 15 |
variant="fp16",
|
| 16 |
use_safetensors=True,
|
| 17 |
-
|
| 18 |
)
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
pipe.
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
-
# اگر
|
| 27 |
-
#
|
| 28 |
-
# os.makedirs("offload_temp", exist_ok=True)
|
| 29 |
-
# disk_offload(pipe, offload_dir="offload_temp")
|
| 30 |
|
| 31 |
def generate_image(prompt, negative_prompt="", height=1024, width=1024, steps=28, guidance=6.0, seed=-1):
|
| 32 |
-
if seed != -1
|
| 33 |
-
|
| 34 |
-
|
| 35 |
image = pipe(
|
| 36 |
prompt=prompt,
|
| 37 |
negative_prompt=negative_prompt,
|
|
@@ -39,19 +38,20 @@ def generate_image(prompt, negative_prompt="", height=1024, width=1024, steps=28
|
|
| 39 |
width=width,
|
| 40 |
num_inference_steps=steps,
|
| 41 |
guidance_scale=guidance,
|
|
|
|
| 42 |
).images[0]
|
| 43 |
-
|
| 44 |
return image
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
with gr.Blocks(title="SDXL
|
| 48 |
-
gr.Markdown("# Stable Diffusion XL -
|
| 49 |
-
gr.Markdown("زمان
|
| 50 |
|
| 51 |
with gr.Row():
|
| 52 |
with gr.Column(scale=2):
|
| 53 |
-
prompt = gr.Textbox(label="پرامپت", lines=3, placeholder="
|
| 54 |
-
negative = gr.Textbox(label="نگاتیو پرامپت", lines=2,
|
| 55 |
|
| 56 |
with gr.Row():
|
| 57 |
height = gr.Slider(512, 1280, value=1024, step=64, label="ارتفاع")
|
|
@@ -65,16 +65,13 @@ with gr.Blocks(title="SDXL روی CPU") as demo:
|
|
| 65 |
btn = gr.Button("تولید تصویر 🚀", variant="primary")
|
| 66 |
|
| 67 |
with gr.Column(scale=1):
|
| 68 |
-
output = gr.Image(label="
|
| 69 |
|
| 70 |
-
gr.Examples(
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
],
|
| 76 |
-
inputs=[prompt, negative]
|
| 77 |
-
)
|
| 78 |
|
| 79 |
btn.click(fn=generate_image, inputs=[prompt, negative, height, width, steps, guidance, seed], outputs=output)
|
| 80 |
|
|
|
|
| 1 |
+
# app.py - نسخه نهایی و تضمینی برای CPU در سال ۲۰۲۵
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
+
from diffusers import StableDiffusionXLPipeline
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
|
| 6 |
+
# ۱. اول accelerate رو initialize کن (حتماً قبل از لود مدل!)
|
| 7 |
+
from accelerate import Accelerator
|
| 8 |
+
accelerator = Accelerator(
|
| 9 |
+
cpu=True, # اجبار به CPU
|
| 10 |
+
mixed_precision="no" # روی CPU فقط float32
|
| 11 |
+
)
|
| 12 |
|
| 13 |
+
# ۲. لود مدل (از dtype استفاده کن، نه torch_dtype)
|
| 14 |
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 15 |
"stabilityai/stable-diffusion-xl-base-1.0",
|
|
|
|
| 16 |
variant="fp16",
|
| 17 |
use_safetensors=True,
|
| 18 |
+
dtype=torch.float32, # جدید و درست
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# ۳. فقط این یک خط کافی است (جایگزین همه cpu_offload و enable_... قدیمی)
|
| 22 |
+
pipe = accelerator.prepare(pipe)
|
| 23 |
|
| 24 |
+
# ۴. بهینهسازیهای اضافی برای سرعت بیشتر روی CPU
|
| 25 |
+
pipe.unet.to(memory_format=torch.channels_last) # معمولاً ۱۰–۲۰٪ سرعت بیشتر میده
|
| 26 |
+
pipe.enable_attention_slicing() # حافظه خیلی کمتری مصرف میکنه (روی CPU حیاتیه)
|
| 27 |
|
| 28 |
+
# اگر هنوز حافظه کم بود، این خط رو هم بزن:
|
| 29 |
+
# pipe.enable_vae_tiling()
|
|
|
|
|
|
|
| 30 |
|
| 31 |
def generate_image(prompt, negative_prompt="", height=1024, width=1024, steps=28, guidance=6.0, seed=-1):
|
| 32 |
+
generator = torch.Generator().manual_seed(int(seed)) if seed != -1 and seed != "" else None
|
| 33 |
+
|
|
|
|
| 34 |
image = pipe(
|
| 35 |
prompt=prompt,
|
| 36 |
negative_prompt=negative_prompt,
|
|
|
|
| 38 |
width=width,
|
| 39 |
num_inference_steps=steps,
|
| 40 |
guidance_scale=guidance,
|
| 41 |
+
generator=generator,
|
| 42 |
).images[0]
|
| 43 |
+
|
| 44 |
return image
|
| 45 |
|
| 46 |
+
# رابط گرادیو (همون قبلی)
|
| 47 |
+
with gr.Blocks(title="SDXL CPU - 100% کار میکنه") as demo:
|
| 48 |
+
gr.Markdown("# Stable Diffusion XL - فقط CPU 🚀")
|
| 49 |
+
gr.Markdown("زمان تولید: ۴۰–۱۲۰ ثانیه روی CPU معمولی")
|
| 50 |
|
| 51 |
with gr.Row():
|
| 52 |
with gr.Column(scale=2):
|
| 53 |
+
prompt = gr.Textbox(label="پرامپت", lines=3, placeholder="یک منظره کوهستانی در غروب...")
|
| 54 |
+
negative = gr.Textbox(label="نگاتیو پرامپت", lines=2, value="تار، بدشکل، متن")
|
| 55 |
|
| 56 |
with gr.Row():
|
| 57 |
height = gr.Slider(512, 1280, value=1024, step=64, label="ارتفاع")
|
|
|
|
| 65 |
btn = gr.Button("تولید تصویر 🚀", variant="primary")
|
| 66 |
|
| 67 |
with gr.Column(scale=1):
|
| 68 |
+
output = gr.Image(label="نتیجه", type="pil")
|
| 69 |
|
| 70 |
+
gr.Examples([
|
| 71 |
+
["یک شهر سایبرپانک در شب با باران و نورهای نئونی", "تار، بدشکل"],
|
| 72 |
+
["گربه فضانورد در حال قدم زدن روی ماه", ""],
|
| 73 |
+
["پرتره یک دختر انیمه با موهای صورتی", "تار، زشت"]
|
| 74 |
+
], inputs=[prompt, negative])
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
btn.click(fn=generate_image, inputs=[prompt, negative, height, width, steps, guidance, seed], outputs=output)
|
| 77 |
|