Spaces:
Runtime error
Runtime error
Vo Minh Vu commited on
Commit ·
4401155
1
Parent(s): e940fe8
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,14 +9,11 @@ from huggingface_hub import hf_hub_download, login
|
|
| 9 |
import os
|
| 10 |
import time
|
| 11 |
|
| 12 |
-
# 🛠 Kiểm tra thiết bị
|
| 13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
|
| 15 |
-
# 🔑 Đăng nhập Hugging Face
|
| 16 |
hf_token = os.getenv("HF_TOKEN")
|
| 17 |
login(token=hf_token)
|
| 18 |
|
| 19 |
-
# 🏗 Tải mô hình SAM từ Hugging Face
|
| 20 |
model_path = hf_hub_download(
|
| 21 |
repo_id="Vuvo11/segment_anything_model",
|
| 22 |
filename="sam_vit_h_4b8939.pth",
|
|
@@ -25,65 +22,56 @@ model_path = hf_hub_download(
|
|
| 25 |
sam = sam_model_registry["vit_h"](checkpoint=model_path)
|
| 26 |
mask_generator = SamAutomaticMaskGenerator(sam)
|
| 27 |
|
| 28 |
-
# 🏗 Tải mô hình Stable Diffusion từ Hugging Face
|
| 29 |
scheduler = DDIMScheduler.from_pretrained("runwayml/stable-diffusion-inpainting", subfolder="scheduler")
|
| 30 |
pipe = StableDiffusionInpaintPipeline.from_pretrained(
|
| 31 |
"runwayml/stable-diffusion-inpainting",
|
| 32 |
scheduler=scheduler,
|
| 33 |
-
torch_dtype=torch.float32,
|
| 34 |
cache_dir="./models",
|
| 35 |
low_cpu_mem_usage=True
|
| 36 |
).to(device)
|
| 37 |
|
|
|
|
|
|
|
|
|
|
| 38 |
pipe.enable_attention_slicing()
|
| 39 |
|
| 40 |
-
|
| 41 |
-
def generate_mask(image, progress=gr.Progress()):
|
| 42 |
-
progress(0, "Generating mask...")
|
| 43 |
masks = mask_generator.generate(image)
|
| 44 |
if len(masks) == 0:
|
| 45 |
return np.zeros_like(image[:, :, 0])
|
| 46 |
|
| 47 |
largest_mask = max(masks, key=lambda x: np.sum(x["segmentation"]))
|
| 48 |
-
progress(50, "Mask generated successfully!")
|
| 49 |
return (largest_mask["segmentation"] * 255).astype(np.uint8)
|
| 50 |
|
| 51 |
-
# 🖌 Hàm xử lý ảnh (có progress)
|
| 52 |
def inpaint(image, prompt, progress=gr.Progress()):
|
| 53 |
progress(0, "Processing image...")
|
| 54 |
|
| 55 |
-
mask = generate_mask(image
|
| 56 |
progress(30, "Generating inpainting...")
|
| 57 |
|
| 58 |
original_image = Image.fromarray(image).convert("RGB")
|
| 59 |
mask_image = Image.fromarray(mask).convert("L")
|
| 60 |
|
| 61 |
-
original_image = original_image.resize((
|
| 62 |
-
mask_image = mask_image.resize((
|
| 63 |
|
| 64 |
-
|
| 65 |
-
time.sleep(0.1)
|
| 66 |
|
| 67 |
-
output = pipe(prompt=prompt, image=original_image, mask_image=mask_image, num_inference_steps=25).images[0]
|
| 68 |
progress(100, "Completed!")
|
| 69 |
return np.array(output)
|
| 70 |
|
| 71 |
-
# 🌐 UI với Gradio (có nút Submit)
|
| 72 |
with gr.Blocks() as interface:
|
| 73 |
-
gr.Markdown("## 🎨 AI Furniture Inpainting")
|
| 74 |
|
| 75 |
with gr.Row():
|
| 76 |
image_input = gr.Image(type="numpy", label="Upload Image")
|
| 77 |
prompt_input = gr.Textbox(label="Prompt (Describe what to add)")
|
| 78 |
|
| 79 |
-
submit = gr.Button("Submit")
|
| 80 |
output_image = gr.Image(label="Generated Image")
|
| 81 |
|
| 82 |
-
submit.click(
|
| 83 |
-
fn=inpaint,
|
| 84 |
-
inputs=[image_input, prompt_input],
|
| 85 |
-
outputs=output_image
|
| 86 |
-
)
|
| 87 |
|
| 88 |
if __name__ == "__main__":
|
| 89 |
interface.launch()
|
|
|
|
| 9 |
import os
|
| 10 |
import time
|
| 11 |
|
|
|
|
| 12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 13 |
|
|
|
|
| 14 |
hf_token = os.getenv("HF_TOKEN")
|
| 15 |
login(token=hf_token)
|
| 16 |
|
|
|
|
| 17 |
model_path = hf_hub_download(
|
| 18 |
repo_id="Vuvo11/segment_anything_model",
|
| 19 |
filename="sam_vit_h_4b8939.pth",
|
|
|
|
| 22 |
sam = sam_model_registry["vit_h"](checkpoint=model_path)
|
| 23 |
mask_generator = SamAutomaticMaskGenerator(sam)
|
| 24 |
|
|
|
|
| 25 |
scheduler = DDIMScheduler.from_pretrained("runwayml/stable-diffusion-inpainting", subfolder="scheduler")
|
| 26 |
pipe = StableDiffusionInpaintPipeline.from_pretrained(
|
| 27 |
"runwayml/stable-diffusion-inpainting",
|
| 28 |
scheduler=scheduler,
|
| 29 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32, # 🔥 FP16 cho GPU
|
| 30 |
cache_dir="./models",
|
| 31 |
low_cpu_mem_usage=True
|
| 32 |
).to(device)
|
| 33 |
|
| 34 |
+
if torch.cuda.is_available():
|
| 35 |
+
pipe.unet = torch.compile(pipe.unet) # 🔥 Tối ưu tốc độ nếu chạy trên GPU
|
| 36 |
+
|
| 37 |
pipe.enable_attention_slicing()
|
| 38 |
|
| 39 |
+
def generate_mask(image):
|
|
|
|
|
|
|
| 40 |
masks = mask_generator.generate(image)
|
| 41 |
if len(masks) == 0:
|
| 42 |
return np.zeros_like(image[:, :, 0])
|
| 43 |
|
| 44 |
largest_mask = max(masks, key=lambda x: np.sum(x["segmentation"]))
|
|
|
|
| 45 |
return (largest_mask["segmentation"] * 255).astype(np.uint8)
|
| 46 |
|
|
|
|
| 47 |
def inpaint(image, prompt, progress=gr.Progress()):
|
| 48 |
progress(0, "Processing image...")
|
| 49 |
|
| 50 |
+
mask = generate_mask(image)
|
| 51 |
progress(30, "Generating inpainting...")
|
| 52 |
|
| 53 |
original_image = Image.fromarray(image).convert("RGB")
|
| 54 |
mask_image = Image.fromarray(mask).convert("L")
|
| 55 |
|
| 56 |
+
original_image = original_image.resize((384, 384)) # 🔥 Resize nhỏ hơn để xử lý nhanh hơn
|
| 57 |
+
mask_image = mask_image.resize((384, 384))
|
| 58 |
|
| 59 |
+
output = pipe(prompt=prompt, image=original_image, mask_image=mask_image, num_inference_steps=15).images[0] # 🔥 Giảm số bước suy luận
|
|
|
|
| 60 |
|
|
|
|
| 61 |
progress(100, "Completed!")
|
| 62 |
return np.array(output)
|
| 63 |
|
|
|
|
| 64 |
with gr.Blocks() as interface:
|
| 65 |
+
gr.Markdown("## 🎨 AI Furniture Inpainting (Optimized)")
|
| 66 |
|
| 67 |
with gr.Row():
|
| 68 |
image_input = gr.Image(type="numpy", label="Upload Image")
|
| 69 |
prompt_input = gr.Textbox(label="Prompt (Describe what to add)")
|
| 70 |
|
| 71 |
+
submit = gr.Button("Submit")
|
| 72 |
output_image = gr.Image(label="Generated Image")
|
| 73 |
|
| 74 |
+
submit.click(fn=inpaint, inputs=[image_input, prompt_input], outputs=output_image)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
if __name__ == "__main__":
|
| 77 |
interface.launch()
|