Spaces:
Build error
Build error
Support changing base model
Browse files48 unmodified lines495051525354553 unmodified lines5960616263646540 unmodified lines10610710810948 unmodified linesIf you are interested in trying out other base models, check out [this Space](https://huggingface.co/spaces/hysts/ControlNet-with-other-models) as well.'''if (SPACE_ID := os.getenv('SPACE_ID')) is not None:DESCRIPTION += f'''<p>For faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings.<br/><a href="https://huggingface.co/spaces/{SPACE_ID}?duplicate=true"><img style="margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>3 unmodified linesMAX_IMAGES = int(os.getenv('MAX_IMAGES', '3'))DEFAULT_NUM_IMAGES = min(MAX_IMAGES, int(os.getenv('DEFAULT_NUM_IMAGES', '1')))model = Model()with gr.Blocks(css='style.css') as demo:gr.Markdown(DESCRIPTION)40 unmodified linesmax_images=MAX_IMAGES,default_num_images=DEFAULT_NUM_IMAGES)demo.queue(api_open=False).launch()48 unmodified lines49505152535455565758596061623 unmodified lines66676869707172737440 unmodified lines11511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414548 unmodified linesIf you are interested in trying out other base models, check out [this Space](https://huggingface.co/spaces/hysts/ControlNet-with-other-models) as well.'''SPACE_ID = os.getenv('SPACE_ID')ALLOW_CHANGING_BASE_MODEL = SPACE_ID != 'hysts/ControlNet'if not ALLOW_CHANGING_BASE_MODEL:DESCRIPTION += 'In this Space, the base model is not allowed to be changed so as not to slow down the demo, but it can be changed if you duplicate the Space.'if SPACE_ID is not None:DESCRIPTION += f'''<p>For faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings.<br/><a href="https://huggingface.co/spaces/{SPACE_ID}?duplicate=true"><img style="margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>3 unmodified linesMAX_IMAGES = int(os.getenv('MAX_IMAGES', '3'))DEFAULT_NUM_IMAGES = min(MAX_IMAGES, int(os.getenv('DEFAULT_NUM_IMAGES', '1')))DEFAULT_MODEL_ID = os.getenv('DEFAULT_MODEL_ID','runwayml/stable-diffusion-v1-5')model = Model(base_model_id=DEFAULT_MODEL_ID, task_name='canny')with gr.Blocks(css='style.css') as demo:gr.Markdown(DESCRIPTION)40 unmodified linesmax_images=MAX_IMAGES,default_num_images=DEFAULT_NUM_IMAGES)with gr.Accordion(label='Base model', open=False):with gr.Row():with gr.Column():current_base_model = gr.Text(label='Current base model')with gr.Column(scale=0.3):check_base_model_button = gr.Button('Check current base model')with gr.Row():with gr.Column():base_model_id = gr.Text(label='Base model repo',max_lines=1,placeholder='runwayml/stable-diffusion-v1-5',info='The base model must be compatible with Stable Diffusion v1.5.',interactive=ALLOW_CHANGING_BASE_MODEL)with gr.Column(scale=0.3):change_base_model_button = gr.Button('Change base model')check_base_model_button.click(fn=lambda: model.base_model_id,outputs=current_base_model)base_model_id.submit(fn=model.set_base_model,inputs=base_model_id,outputs=current_base_model)change_base_model_button.click(fn=model.set_base_model,inputs=base_model_id,outputs=current_base_model)demo.queue(api_open=False).launch()
38 unmodified lines3940414243444546474849505152535455561 unmodified line585960616263646538 unmodified linesclass Model:def __init__(self):# FIXMEself.base_model_id = 'andite/anything-v4.0'self.task_name = 'pose'self.pipe = self.load_pipe()def load_pipe(self) -> DiffusionPipeline:model_id = CONTROLNET_MODEL_IDS[self.task_name]controlnet = ControlNetModel.from_pretrained(model_id,torch_dtype=torch.float16)pipe = StableDiffusionControlNetPipeline.from_pretrained(self.base_model_id,safety_checker=None,controlnet=controlnet,torch_dtype=torch.float16)1 unmodified linepipe.scheduler.config)pipe.enable_xformers_memory_efficient_attention()pipe.enable_model_cpu_offload()return pipedef load_controlnet_weight(self, task_name: str) -> None:if task_name == self.task_name:return38 unmodified lines3940414243444546474849505152535455565758591 unmodified line616263646566676869707172737438 unmodified linesclass Model:def __init__(self,base_model_id: str = 'runwayml/stable-diffusion-v1-5',task_name: str = 'canny'):self.base_model_id = ''self.task_name = ''self.pipe = self.load_pipe(base_model_id, task_name)def load_pipe(self, base_model_id: str, task_name) -> DiffusionPipeline:if base_model_id == self.base_model_id and task_name == self.task_name:return self.pipemodel_id = CONTROLNET_MODEL_IDS[task_name]controlnet = ControlNetModel.from_pretrained(model_id,torch_dtype=torch.float16)pipe = StableDiffusionControlNetPipeline.from_pretrained(base_model_id,safety_checker=None,controlnet=controlnet,torch_dtype=torch.float16)1 unmodified linepipe.scheduler.config)pipe.enable_xformers_memory_efficient_attention()pipe.enable_model_cpu_offload()self.base_model_id = base_model_idself.task_name = task_namereturn pipedef set_base_model(self, base_model_id: str) -> str:self.pipe = self.load_pipe(base_model_id, self.task_name)return self.base_model_iddef load_controlnet_weight(self, task_name: str) -> None:if task_name == self.task_name:return
app.py+38-2
model.py+18-9