zhiweili commited on
Commit Β·
4b80f72
1
Parent(s): 7a58dc4
change to image to imagw
Browse files49 unmodified lines5051525354555657585960616263646566676869707172737475767778798081828384858687885 unmodified lines94959697989910010110210310410510610710849 unmodified linesbase_pipe.unet = compiledModelinit_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img.png")generated_image = base_pipe(image=init_image,prompt="A white cat",num_inference_steps=5,).images[0]generated_image.save("/tmp/gradio/generated_image.png")def create_demo() -> gr.Blocks:@spaces.GPU(duration=30)def text_to_image(prompt:str,steps:int,):import torch_tensorrtprint('Compiling model...')compiledModel = torch.compile(base_pipe.unet,backend=backend,options={"truncate_long_and_double": True,"enabled_precisions": {torch.float32, torch.float16},},dynamic=False,)print('Model compiled!')print('Saving compiled model...')torch_tensorrt.save(compiledModel, "compiled_pipe.ep")print('Compiled model saved!')with gr.Blocks() as demo:with gr.Row():5 unmodified lineswith gr.Row():with gr.Column():generated_image = gr.Image(label="Generated Image", type="pil", interactive=False)with gr.Column():time_cost = gr.Textbox(label="Time Cost", lines=1, interactive=False)g_btn.click(fn=text_to_image,inputs=[prompt, steps],# outputs=[generated_image, time_cost],outputs=[],)return demo49 unmodified lines50515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991005 unmodified lines10610710810911011111211311411511611711811912049 unmodified linesbase_pipe.unet = compiledModelimport torch._dynamotorch._dynamo.config.suppress_errors = Truetry:init_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img.png")generated_image = base_pipe(image=init_image,prompt="A white cat",num_inference_steps=5,).images[0]generated_image.save("/tmp/gradio/generated_image.png")except Exception as e:print(f"Error: {e}")def create_demo() -> gr.Blocks:@spaces.GPU(duration=30)def image_to_image(image: gr.Image,prompt:str,steps:int,):run_task_time = 0time_cost_str = ''run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)generated_image = base_pipe(image=image,prompt=prompt,num_inference_steps=steps,).images[0]run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)return generated_imagedef get_time_cost(run_task_time, time_cost_str):now_time = int(time.time()*1000)if run_task_time == 0:time_cost_str = 'start'else:if time_cost_str != '':time_cost_str += f'-->'time_cost_str += f'{now_time - run_task_time}'run_task_time = now_timereturn run_task_time, time_cost_strwith gr.Blocks() as demo:with gr.Row():5 unmodified lineswith gr.Row():with gr.Column():input_image = gr.Image(label="Input Image", type="pil", interactive=True)with gr.Column():generated_image = gr.Image(label="Generated Image", type="pil", interactive=False)time_cost = gr.Textbox(label="Time Cost", lines=1, interactive=False)g_btn.click(fn=text_to_image,inputs=[input_image, prompt, steps],outputs=[generated_image, time_cost],)return demo
app_tensorrt.py+40-28