import gradio as gr import numpy as np import cv2 from PIL import Image import torch import base64 import requests from io import BytesIO from region_control import MultiDiffusion, get_views, preprocess_mask from sketch_helper import get_high_freq_colors, color_quantization, create_binary_matrix MAX_COLORS = 12 sd = MultiDiffusion("cuda", "2.0") canvas_html = "
" load_js = """ async () => { const url = "https://huggingface.co/datasets/radames/gradio-components/raw/main/sketch-canvas.js" fetch(url) .then(res => res.text()) .then(text => { const script = document.createElement('script'); script.type = "module" script.src = URL.createObjectURL(new Blob([text], { type: 'application/javascript' })); document.head.appendChild(script); }); } """ get_js_colors = """ async (canvasData) => { const canvasEl = document.getElementById("canvas-root"); return [canvasEl._data] } """ set_canvas_size =""" async (aspect) => { if(aspect ==='square'){ _updateCanvas(512,512) } if(aspect ==='horizontal'){ _updateCanvas(768,512) } if(aspect ==='vertical'){ _updateCanvas(512,768) } } """ def process_sketch(canvas_data, binary_matrixes): base64_img = canvas_data['image'] image_data = base64.b64decode(base64_img.split(',')[1]) image = Image.open(BytesIO(image_data)) im2arr = np.array(image) colors = [tuple(int(color.lstrip('#')[i:i+2], 16) for i in (0, 2, 4)) for color in canvas_data['colors']] colors_fixed = [] for color in colors: r, g, b = color if any(c != 255 for c in (r, g, b)): binary_matrix = create_binary_matrix(im2arr, (r,g,b)) binary_matrixes.append(binary_matrix) colors_fixed.append(gr.update(value=f'