Spaces:
Runtime error
Runtime error
File size: 2,270 Bytes
206af61 e6ff9aa 206af61 |
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 |
import gradio as gr
import torch
from diffusers import StableDiffusionInpaintPipeline, DPMSolverMultistepScheduler
from PIL import Image
# แแ Model Card แแฝแฑแธแแปแแบแแพแฏ
model_id = "Sanster/Realistic_Vision_V1.4-inpainting"
# แแ Pipeline แแญแฏ Load แแฏแแบแแผแฎแธ Optimization แแปแฌแธแแแทแบแแฝแแบแธแแผแแบแธ
pipe = StableDiffusionInpaintPipeline.from_pretrained(
model_id,
torch_dtype=torch.float32 # CPU แกแแฝแแบ แแญแฏแแผแญแแบแแแบ
)
# Speed แกแแฝแแบ Scheduler แแผแฑแฌแแบแธแแผแแบแธ
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
# Safety Checker (NSFW Filter) แแญแฏ แแฏแถแธแ แแญแแบแแผแแบแธ
pipe.safety_checker = lambda images, **kwargs: (images, [False] * len(images))
# CPU Optimization แแปแฌแธ
pipe.enable_attention_slicing()
# pipe.enable_sequential_cpu_offload() # RAM แแญแแบแแแพแญแแปแพแแบ แแฝแแทแบแแแบ
def predict(image_dict, prompt):
# แแฏแถแกแแฝแแบแกแ
แฌแธแแญแฏ 512 แกแฑแฌแแบแแฌแธแแผแแบแธแ แกแแผแแบแแฏแถแธแแผแ
แบแแแบ
init_image = image_dict["background"].convert("RGB").resize((512, 512))
mask_image = image_dict["layers"][0].convert("RGB").resize((512, 512))
# แแ แกแแผแแบแแฏแถแธ แกแแฑแกแแฌแธแแผแแทแบ แแฏแถแแฏแแบแแผแแบแธ (Steps แแญแฏ แแ แแฌแธแแซ)
output = pipe(
prompt=prompt,
image=init_image,
mask_image=mask_image,
num_inference_steps=20, # แกแแผแแบแแพแฏแแบแธแกแแฝแแบ แกแแญแ แกแแปแแบ
guidance_scale=7.5
).images[0]
return output
# แแ Gradio UI แแแบแแฑแฌแแบแแผแแบแธ
with gr.Blocks() as demo:
gr.Markdown("### โก Fast AI Inpainting (CPU Optimized)")
with gr.Row():
img = gr.Image(label="Upload Image & Paint Mask", tool="sketch", type="pil")
prompt = gr.Textbox(label="Prompt (e.g., 'white shirt')")
btn = gr.Button("Generate Fast")
result = gr.Image(label="Result")
btn.click(predict, inputs=[img, prompt], outputs=result)
demo.launch()
|