Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,86 @@
|
|
| 1 |
import torch
|
| 2 |
from diffusers import AutoPipelineForText2Image
|
| 3 |
-
|
| 4 |
-
import gradio as gr # Import Gradio
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
device =
|
| 8 |
|
| 9 |
-
|
| 10 |
-
print("기본 FLUX 모델 로드 중...")
|
| 11 |
pipe = AutoPipelineForText2Image.from_pretrained(
|
| 12 |
"black-forest-labs/FLUX.1-dev",
|
| 13 |
-
torch_dtype=torch.float16 #
|
| 14 |
)
|
| 15 |
-
pipe.to(device)
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
pipe.load_lora_weights(
|
| 20 |
'Heartsync/Flux-NSFW-uncensored',
|
| 21 |
weight_name='lora.safetensors',
|
| 22 |
adapter_name="uncensored"
|
| 23 |
)
|
| 24 |
|
| 25 |
-
#
|
| 26 |
def generate_image(prompt, negative_prompt, guidance_scale, num_inference_steps, width, height, seed):
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
-
|
|
|
|
| 30 |
prompt=prompt,
|
| 31 |
negative_prompt=negative_prompt,
|
| 32 |
-
guidance_scale=guidance_scale,
|
| 33 |
-
num_inference_steps=num_inference_steps,
|
| 34 |
-
width=width,
|
| 35 |
-
height=height,
|
| 36 |
generator=generator,
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
return image
|
| 42 |
|
| 43 |
-
# Gradio
|
| 44 |
iface = gr.Interface(
|
| 45 |
fn=generate_image,
|
| 46 |
inputs=[
|
| 47 |
-
gr.Textbox(label="Prompt", value="A woman in a sheer white dress standing on a beach at sunset
|
| 48 |
gr.Textbox(label="Negative Prompt", value="text, watermark, signature, cartoon, anime, illustration, painting, drawing, low quality, blurry"),
|
| 49 |
gr.Slider(minimum=1.0, maximum=20.0, step=0.1, value=7.0, label="Guidance Scale"),
|
| 50 |
gr.Slider(minimum=10, maximum=100, step=1, value=28, label="Number of Inference Steps"),
|
|
@@ -53,9 +89,8 @@ iface = gr.Interface(
|
|
| 53 |
gr.Slider(minimum=0, maximum=99999, step=1, value=42, label="Seed")
|
| 54 |
],
|
| 55 |
outputs="image",
|
| 56 |
-
title="FLUX.1-dev with Uncensored LoRA",
|
| 57 |
-
description="Generate images using FLUX.1-dev with
|
| 58 |
)
|
| 59 |
|
| 60 |
-
# 인터페이스 실행 (share=True)
|
| 61 |
iface.launch(share=True)
|
|
|
|
| 1 |
import torch
|
| 2 |
from diffusers import AutoPipelineForText2Image
|
| 3 |
+
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
+
# เลือก device เป็น cuda ถ้ามี GPU เพื่อให้ offload ทำงานร่วมกับ GPU
|
| 6 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
|
| 8 |
+
print("โหลด FLUX model ด้วยการตั้งค่าสำหรับ offload...")
|
|
|
|
| 9 |
pipe = AutoPipelineForText2Image.from_pretrained(
|
| 10 |
"black-forest-labs/FLUX.1-dev",
|
| 11 |
+
torch_dtype=torch.float16 # ลด precision เพื่อประหยัดหน่วยความจำ
|
| 12 |
)
|
|
|
|
| 13 |
|
| 14 |
+
# ถ้ามี GPU ให้ย้าย pipeline ไปยัง GPU ก่อนเปิด offload (diffusers จะจัดการย้ายพารามิเตอร์)
|
| 15 |
+
if device == "cuda":
|
| 16 |
+
pipe.to("cuda")
|
| 17 |
+
|
| 18 |
+
# เปิดการประหยัดหน่วยความจำ
|
| 19 |
+
# 1) attention slicing ลด peak memory ขณะ attention
|
| 20 |
+
pipe.enable_attention_slicing()
|
| 21 |
+
# 2) VAE slicing ลด memory ตอน decode
|
| 22 |
+
try:
|
| 23 |
+
pipe.enable_vae_slicing()
|
| 24 |
+
except Exception:
|
| 25 |
+
pass
|
| 26 |
+
|
| 27 |
+
# 3) เปิด CPU offload: ย้ายพารามิเตอร์โมเดลไปเก็บใน CPU RAM แล้วโหลดไป GPU เฉพาะตอนใช้งาน
|
| 28 |
+
# ถ้าต้องการให้ใช้โฟลเดอร์สำหรับ offload ชั่วคราว ให้กำหนด offload_folder
|
| 29 |
+
try:
|
| 30 |
+
pipe.enable_model_cpu_offload(
|
| 31 |
+
gpu_id=0, # หมายเลข GPU ที่จะใช้ (ถ้ามีหลายตัว ปรับตามจริง)
|
| 32 |
+
offload_folder="./offload", # ถ้าต้องการใช้ disk เป็นสำรอง
|
| 33 |
+
pin_memory=True # pin memory ช่วยให้ย้ายข้อมูลเร็วขึ้น
|
| 34 |
+
)
|
| 35 |
+
except Exception:
|
| 36 |
+
# ถ้าเวอร์ชัน diffusers ไม่มีฟังก์ชันนี้ ให้ลอง enable_sequential_cpu_offload
|
| 37 |
+
try:
|
| 38 |
+
pipe.enable_sequential_cpu_offload()
|
| 39 |
+
except Exception:
|
| 40 |
+
pass
|
| 41 |
+
|
| 42 |
+
# เพิ่มการตั้งค่าอื่น ๆ ที่ช่วยลด memory
|
| 43 |
+
torch.backends.cudnn.benchmark = False
|
| 44 |
+
|
| 45 |
+
# โหลด LoRA (ถ้าจำเป็น) — ควรโหลดหลังจาก pipeline ถูกตั้งค่า offload แล้ว
|
| 46 |
+
print("โหลด Uncensored LoRA...")
|
| 47 |
pipe.load_lora_weights(
|
| 48 |
'Heartsync/Flux-NSFW-uncensored',
|
| 49 |
weight_name='lora.safetensors',
|
| 50 |
adapter_name="uncensored"
|
| 51 |
)
|
| 52 |
|
| 53 |
+
# ฟังก์ชันสร้างภาพ
|
| 54 |
def generate_image(prompt, negative_prompt, guidance_scale, num_inference_steps, width, height, seed):
|
| 55 |
+
# แนะนำ: ถ้าต้องการจำกัด VRAM ให้ลด width/height หรือใช้ smaller batch
|
| 56 |
+
generator = torch.Generator(device="cpu").manual_seed(int(seed))
|
| 57 |
|
| 58 |
+
# ถ้า GPU มี ให้ใช้ device="cuda" ในการเรียก แต่ pipeline จะจัดการ offload ให้
|
| 59 |
+
out = pipe(
|
| 60 |
prompt=prompt,
|
| 61 |
negative_prompt=negative_prompt,
|
| 62 |
+
guidance_scale=float(guidance_scale),
|
| 63 |
+
num_inference_steps=int(num_inference_steps),
|
| 64 |
+
width=int(width),
|
| 65 |
+
height=int(height),
|
| 66 |
generator=generator,
|
| 67 |
+
)
|
| 68 |
+
image = out.images[0]
|
| 69 |
+
|
| 70 |
+
# เคลียร์ cache ของ CUDA ถ้ามี
|
| 71 |
+
if torch.cuda.is_available():
|
| 72 |
+
try:
|
| 73 |
+
torch.cuda.empty_cache()
|
| 74 |
+
except Exception:
|
| 75 |
+
pass
|
| 76 |
+
|
| 77 |
return image
|
| 78 |
|
| 79 |
+
# Gradio interface (เหมือนเดิม)
|
| 80 |
iface = gr.Interface(
|
| 81 |
fn=generate_image,
|
| 82 |
inputs=[
|
| 83 |
+
gr.Textbox(label="Prompt", value="A woman in a sheer white dress standing on a beach at sunset..."),
|
| 84 |
gr.Textbox(label="Negative Prompt", value="text, watermark, signature, cartoon, anime, illustration, painting, drawing, low quality, blurry"),
|
| 85 |
gr.Slider(minimum=1.0, maximum=20.0, step=0.1, value=7.0, label="Guidance Scale"),
|
| 86 |
gr.Slider(minimum=10, maximum=100, step=1, value=28, label="Number of Inference Steps"),
|
|
|
|
| 89 |
gr.Slider(minimum=0, maximum=99999, step=1, value=42, label="Seed")
|
| 90 |
],
|
| 91 |
outputs="image",
|
| 92 |
+
title="FLUX.1-dev with Uncensored LoRA (Offload to RAM)",
|
| 93 |
+
description="Generate images using FLUX.1-dev with CPU offload to keep GPU VRAM low."
|
| 94 |
)
|
| 95 |
|
|
|
|
| 96 |
iface.launch(share=True)
|