Spaces:
Runtime error
Runtime error
Commit ·
dd559e1
1
Parent(s): 46af7ed
WIP
Browse files3 unmodified lines45678922 unmodified lines32333435363738 unmodified lines7677787980818283848586878889909113 unmodified lines1051061071081091104 unmodified lines1151161171181191201211224 unmodified lines12712812913013113213313413 unmodified lines1481491501511521531541551561571581591601611621631641651661671681691703 unmodified lines1741751761771781797 unmodified lines1871881891901911923 unmodified linesimport osimport randomimport torchfrom PIL import Imageimport cv222 unmodified lines`login("<YOUR_HF_TOKEN>")` explicitly."""# --------------------------------------------------# Model & pipeline setup# --------------------------------------------------38 unmodified lines# --------------------------------------------------def _preview_canny(pil_img: Image.Image) -> Image.Image:arr = np.array(pil_img.convert("RGB"))edges = cv2.Canny(arr, 100, 200)edges_rgb = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB)return Image.fromarray(edges_rgb)def _make_preview(control_image: Image.Image, mode: str) -> Image.Image:if mode == "canny":return _preview_canny(control_image)# For other modes you can plug in your own visualiser laterreturn control_image13 unmodified linesrandomize_seed: bool,guidance_scale: float,num_inference_steps: int,):if control_image is None:raise gr.Error("Please upload a control image first.")4 unmodified linesgen = torch.Generator(device).manual_seed(seed)w, h = control_image.sizeresult = pipe(prompt=prompt,control_image=[control_image],control_mode=[MODE_MAPPING[mode]],width=w,4 unmodified linesgenerator=gen,).images[0]preview = _make_preview(control_image, mode)return result, seed, preview# --------------------------------------------------13 unmodified linescontrol_image = gr.Image(label="Upload a processed control image",type="pil",height=512,)result_image = gr.Image(label="Result", height=512)preview_image = gr.Image(label="Pre‑processed Cond", height=512)# ------------ Prompt ------------prompt_txt = gr.Textbox(label="Prompt", value="best quality", lines=1)# ------------ ControlNet settings ------------with gr.Row():with gr.Column():gr.Markdown("### ControlNet")mode_radio = gr.Radio(choices=list(MODE_MAPPING.keys()), value="gray", label="Mode")strength_slider = gr.Slider(0.0, 1.0, value=0.5, step=0.01, label="control strength")with gr.Column():seed_slider = gr.Slider(0, MAX_SEED, step=1, value=42, label="Seed")3 unmodified lines)steps_slider = gr.Slider(1, 50, step=1, value=24, label="Inference steps")submit_btn = gr.Button("Submit")submit_btn.click(7 unmodified linesrandomize_chk,guidance_slider,steps_slider,],outputs=[result_image, seed_slider, preview_image],)3 unmodified lines4567891022 unmodified lines333435363738394038 unmodified lines79808182838485868788899091929394959697989910010113 unmodified lines1151161171181191201211224 unmodified lines1271281291301311321331341351361371384 unmodified lines14314414514614714814913 unmodified lines1631641651661671681691701711721731741751761771781791801811821831841853 unmodified lines1891901911921931941951961971981992002012022037 unmodified lines2112122132142152162172183 unmodified linesimport osimport randomimport subprocessimport torchfrom PIL import Imageimport cv222 unmodified lines`login("<YOUR_HF_TOKEN>")` explicitly."""subprocess.run("rm -rf /data-nvme/zerogpu-offload/*", env={}, shell=True)# --------------------------------------------------# Model & pipeline setup# --------------------------------------------------38 unmodified lines# --------------------------------------------------def _preview_canny(pil_img: Image.Image, canny_threshold_1: int, canny_threshold_2: int) -> Image.Image:arr = np.array(pil_img.convert("RGB"))edges = cv2.Canny(arr, threshold1=canny_threshold_1, threshold2=canny_threshold_2)edges_rgb = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB)return Image.fromarray(edges_rgb)def _make_preview(control_image: Image.Image,mode: str,canny_threshold_1: int,canny_threshold_2: int,) -> Image.Image:if mode == "canny":return _preview_canny(control_image, canny_threshold_1, canny_threshold_2)# For other modes you can plug in your own visualiser laterreturn control_image13 unmodified linesrandomize_seed: bool,guidance_scale: float,num_inference_steps: int,canny_threshold_1: int,canny_threshold_2: int,):if control_image is None:raise gr.Error("Please upload a control image first.")4 unmodified linesgen = torch.Generator(device).manual_seed(seed)w, h = control_image.sizepreprocessed = _make_preview(control_image, mode, canny_threshold_1, canny_threshold_2)result = pipe(prompt=preprocessed,control_image=[control_image],control_mode=[MODE_MAPPING[mode]],width=w,4 unmodified linesgenerator=gen,).images[0]return result, seed, preprocessed# --------------------------------------------------13 unmodified linescontrol_image = gr.Image(label="Upload a processed control image",type="pil",height=512 + 256,)result_image = gr.Image(label="Result", height=512 + 256)preview_image = gr.Image(label="Pre‑processed Cond", height=512 + 256)# ------------ Prompt ------------prompt_txt = gr.Textbox(label="Prompt", value="A beautiful image", lines=1)# ------------ ControlNet settings ------------with gr.Row():with gr.Column():gr.Markdown("### ControlNet")mode_radio = gr.Radio(choices=list(MODE_MAPPING.keys()), value="canny", label="Mode")strength_slider = gr.Slider(0.0, 1.0, value=0.8, step=0.01, label="control strength")with gr.Column():seed_slider = gr.Slider(0, MAX_SEED, step=1, value=42, label="Seed")3 unmodified lines)steps_slider = gr.Slider(1, 50, step=1, value=24, label="Inference steps")with gr.Row():with gr.Column():gr.Markdown("### Preprocess")canny_threshold_1 = gr.Slider(0, 500, step=1, value=100, label="Canny threshold 1")canny_threshold_2 = gr.Slider(0, 500, step=1, value=200, label="Canny threshold 2")submit_btn = gr.Button("Submit")submit_btn.click(7 unmodified linesrandomize_chk,guidance_slider,steps_slider,canny_threshold_1,canny_threshold_2,],outputs=[result_image, seed_slider, preview_image],)
app.py+39-13