Spaces:
Runtime error
Runtime error
File size: 4,562 Bytes
9451bf8 e4d0fa6 9451bf8 ec03c85 9451bf8 e4d0fa6 ec03c85 e4d0fa6 be820dc 9451bf8 2924d00 9451bf8 2924d00 9451bf8 2924d00 9451bf8 2924d00 9451bf8 2924d00 9451bf8 2924d00 9451bf8 2924d00 be820dc 9451bf8 2924d00 9451bf8 be820dc 2924d00 9451bf8 be820dc 9451bf8 2924d00 9451bf8 be820dc 2924d00 9451bf8 2924d00 9451bf8 2924d00 9451bf8 2924d00 9451bf8 be820dc |
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 |
import os
import math
import gradio as gr
import numpy as np
import torch
import safetensors.torch as sf
from PIL import Image
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
from diffusers import AutoencoderKL, UNet2DConditionModel, DDIMScheduler, EulerAncestralDiscreteScheduler, DPMSolverMultistepScheduler
from diffusers.models.attention_processor import AttnProcessor2_0
from transformers import CLIPTextModel, CLIPTokenizer
from briarmbg import BriaRMBG
from torch.hub import download_url_to_file
from spaces import ZeroGPU
# مقداردهی اولیه ZeroGPU
ZeroGPU.initialize()
# نصب کتابخانههای مورد نیاز در صورت اجرا روی Hugging Face Spaces
os.system("pip install torch torchvision --index-url https://download.pytorch.org/whl/cu121")
# بررسی دسترسی به GPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
dtype = torch.bfloat16 if torch.cuda.is_available() and torch.cuda.get_device_name(0).startswith("A100") else torch.float16
if torch.cuda.is_available():
print(f"✅ CUDA is available! GPU: {torch.cuda.get_device_name(0)}")
else:
print("❌ No CUDA GPUs are available. Switching to CPU.")
# دانلود و بارگذاری مدل
model_path = './models/iclight_sd15_fc.safetensors'
if not os.path.exists(model_path):
download_url_to_file(
url='https://huggingface.co/lllyasviel/ic-light/resolve/main/iclight_sd15_fc.safetensors',
dst=model_path
)
# بارگذاری مدلهای مورد نیاز
sd15_name = 'stablediffusionapi/realistic-vision-v51'
tokenizer = CLIPTokenizer.from_pretrained(sd15_name, subfolder="tokenizer")
text_encoder = CLIPTextModel.from_pretrained(sd15_name, subfolder="text_encoder").to(device=device, dtype=dtype)
vae = AutoencoderKL.from_pretrained(sd15_name, subfolder="vae").to(device=device, dtype=dtype)
unet = UNet2DConditionModel.from_pretrained(sd15_name, subfolder="unet").to(device=device, dtype=dtype)
rmbg = BriaRMBG.from_pretrained("briaai/RMBG-1.4").to(device=device, dtype=torch.float32)
# تنظیم پردازشگر توجه برای بهینهسازی عملکرد
unet.set_attn_processor(AttnProcessor2_0())
vae.set_attn_processor(AttnProcessor2_0())
# تنظیم نمونهگیرها
scheduler = DPMSolverMultistepScheduler(
num_train_timesteps=1000,
beta_start=0.00085,
beta_end=0.012,
algorithm_type="sde-dpmsolver++",
use_karras_sigmas=True,
steps_offset=1
)
# ساخت لولههای پردازش تصویر
t2i_pipe = StableDiffusionPipeline.from_pretrained(
sd15_name,
vae=vae,
text_encoder=text_encoder,
tokenizer=tokenizer,
unet=unet,
scheduler=scheduler,
safety_checker=None,
requires_safety_checker=False,
feature_extractor=None,
image_encoder=None
).to(device)
i2i_pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
sd15_name,
vae=vae,
text_encoder=text_encoder,
tokenizer=tokenizer,
unet=unet,
scheduler=scheduler,
safety_checker=None,
requires_safety_checker=False,
feature_extractor=None,
image_encoder=None
).to(device)
# پاک کردن کش حافظه GPU برای جلوگیری از خطای Out Of Memory
torch.cuda.empty_cache()
# توابع پردازش تصویر
def process_image(prompt: str, width: int, height: int, num_steps: int, guidance: float):
rng = torch.Generator(device=device).manual_seed(42)
result = t2i_pipe(
prompt=prompt,
width=width,
height=height,
num_inference_steps=num_steps,
guidance_scale=guidance,
generator=rng
).images[0]
return result
# راهاندازی رابط کاربری با Gradio
block = gr.Blocks()
with block:
with gr.Row():
gr.Markdown("## IC-Light - Image Processing")
with gr.Row():
with gr.Column():
prompt = gr.Textbox(label="Prompt")
width = gr.Slider(256, 1024, value=512, step=64, label="Width")
height = gr.Slider(256, 1024, value=640, step=64, label="Height")
steps = gr.Slider(10, 100, value=50, step=5, label="Inference Steps")
guidance = gr.Slider(1.0, 15.0, value=7.5, step=0.5, label="Guidance Scale")
submit = gr.Button("Generate Image")
with gr.Column():
result_image = gr.Image(type="pil", label="Generated Image")
submit.click(fn=process_image, inputs=[prompt, width, height, steps, guidance], outputs=result_image)
block.launch(server_name='0.0.0.0')
|