zhiweili commited on
Commit
3f85c56
·
1 Parent(s): e84a93e

add app_upscale

Browse files
Files changed (2) hide show
  1. app.py +1 -1
  2. app_upscale.py +63 -0
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
 
3
- from app_enhance import create_demo as create_demo_enhance
4
 
5
  with gr.Blocks(css="style.css") as demo:
6
  with gr.Tabs():
 
1
  import gradio as gr
2
 
3
+ from app_upscale import create_demo as create_demo_enhance
4
 
5
  with gr.Blocks(css="style.css") as demo:
6
  with gr.Tabs():
app_upscale.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ from PIL import Image
3
+ from io import BytesIO
4
+ from diffusers import StableDiffusionUpscalePipeline
5
+ import torch
6
+
7
+ model_id = "stabilityai/stable-diffusion-x4-upscaler"
8
+ upscale_pipe = StableDiffusionUpscalePipeline.from_pretrained(model_id, torch_dtype=torch.float16)
9
+ upscale_pipe = upscale_pipe.to("cuda")
10
+
11
+ DEFAULT_SRC_PROMPT = "a person with pefect face"
12
+
13
+
14
+ def create_demo() -> gr.Blocks:
15
+ from inversion_run_base import run as base_run
16
+
17
+ @spaces.GPU(duration=15)
18
+ def upscale_image(
19
+ input_image: Image,
20
+ prompt: str,
21
+ ):
22
+ upscaled_image = upscale_pipe(prompt=prompt, image=input_image).images[0]
23
+ extension = 'png'
24
+
25
+ path = f"output/{uuid.uuid4()}.{extension}"
26
+ upscaled_image.save(path, quality=100)
27
+
28
+ return upscaled_image, path, time_cost_str
29
+
30
+ def get_time_cost(run_task_time, time_cost_str):
31
+ now_time = int(time.time()*1000)
32
+ if run_task_time == 0:
33
+ time_cost_str = 'start'
34
+ else:
35
+ if time_cost_str != '':
36
+ time_cost_str += f'-->'
37
+ time_cost_str += f'{now_time - run_task_time}'
38
+ run_task_time = now_time
39
+ return run_task_time, time_cost_str
40
+
41
+ with gr.Blocks() as demo:
42
+ croper = gr.State()
43
+ with gr.Row():
44
+ with gr.Column():
45
+ input_image_prompt = gr.Textbox(lines=1, label="Input Image Prompt", value=DEFAULT_SRC_PROMPT)
46
+ with gr.Column():
47
+ g_btn = gr.Button("Upscale Image")
48
+
49
+ with gr.Row():
50
+ with gr.Column():
51
+ input_image = gr.Image(label="Input Image", type="pil")
52
+ with gr.Column():
53
+ upscaled_image = gr.Image(label="Upscaled Image", format="png", type="pil", interactive=False)
54
+ download_path = gr.File(label="Download the output image", interactive=False)
55
+ generated_cost = gr.Textbox(label="Time cost by step (ms):", visible=True, interactive=False)
56
+
57
+ g_btn.click(
58
+ fn=upscale_image,
59
+ inputs=[input_image, input_image_prompt],
60
+ outputs=[upscaled_image, download_path, generated_cost],
61
+ )
62
+
63
+ return demo