Spaces:
Sleeping
Sleeping
Delete app_upscale.py
Browse files- app_upscale.py +0 -95
app_upscale.py
DELETED
|
@@ -1,95 +0,0 @@
|
|
| 1 |
-
import requests
|
| 2 |
-
from PIL import Image
|
| 3 |
-
from io import BytesIO
|
| 4 |
-
from diffusers import StableDiffusionUpscalePipeline
|
| 5 |
-
import torch
|
| 6 |
-
import gradio as gr
|
| 7 |
-
import time
|
| 8 |
-
import spaces
|
| 9 |
-
|
| 10 |
-
from segment_utils import(
|
| 11 |
-
segment_image,
|
| 12 |
-
restore_result,
|
| 13 |
-
)
|
| 14 |
-
|
| 15 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 16 |
-
|
| 17 |
-
print(f'{device} is available')
|
| 18 |
-
|
| 19 |
-
model_id = "stabilityai/stable-diffusion-x4-upscaler"
|
| 20 |
-
upscale_pipe = StableDiffusionUpscalePipeline.from_pretrained(model_id, torch_dtype=torch.float16)
|
| 21 |
-
upscale_pipe = upscale_pipe.to(device)
|
| 22 |
-
|
| 23 |
-
DEFAULT_SRC_PROMPT = "a person with pefect face"
|
| 24 |
-
DEFAULT_CATEGORY = "face"
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
def create_demo() -> gr.Blocks:
|
| 28 |
-
|
| 29 |
-
@spaces.GPU(duration=30)
|
| 30 |
-
def upscale_image(
|
| 31 |
-
input_image: Image,
|
| 32 |
-
prompt: str,
|
| 33 |
-
num_inference_steps: int = 10,
|
| 34 |
-
):
|
| 35 |
-
time_cost_str = ''
|
| 36 |
-
run_task_time = 0
|
| 37 |
-
run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
|
| 38 |
-
upscaled_image = upscale_pipe(
|
| 39 |
-
prompt=prompt,
|
| 40 |
-
image=input_image,
|
| 41 |
-
num_inference_steps=num_inference_steps,
|
| 42 |
-
).images[0]
|
| 43 |
-
run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
|
| 44 |
-
|
| 45 |
-
return upscaled_image, time_cost_str
|
| 46 |
-
|
| 47 |
-
def get_time_cost(run_task_time, time_cost_str):
|
| 48 |
-
now_time = int(time.time()*1000)
|
| 49 |
-
if run_task_time == 0:
|
| 50 |
-
time_cost_str = 'start'
|
| 51 |
-
else:
|
| 52 |
-
if time_cost_str != '':
|
| 53 |
-
time_cost_str += f'-->'
|
| 54 |
-
time_cost_str += f'{now_time - run_task_time}'
|
| 55 |
-
run_task_time = now_time
|
| 56 |
-
return run_task_time, time_cost_str
|
| 57 |
-
|
| 58 |
-
with gr.Blocks() as demo:
|
| 59 |
-
croper = gr.State()
|
| 60 |
-
with gr.Row():
|
| 61 |
-
with gr.Column():
|
| 62 |
-
input_image_prompt = gr.Textbox(lines=1, label="Input Image Prompt", value=DEFAULT_SRC_PROMPT)
|
| 63 |
-
with gr.Column():
|
| 64 |
-
num_inference_steps = gr.Number(label="Num Inference Steps", value=5)
|
| 65 |
-
generate_size = gr.Number(label="Generate Size", value=512)
|
| 66 |
-
g_btn = gr.Button("Upscale Image")
|
| 67 |
-
|
| 68 |
-
with gr.Row():
|
| 69 |
-
with gr.Column():
|
| 70 |
-
input_image = gr.Image(label="Input Image", type="pil")
|
| 71 |
-
with gr.Column():
|
| 72 |
-
restored_image = gr.Image(label="Restored Image", format="png", type="pil", interactive=False)
|
| 73 |
-
origin_area_image = gr.Image(label="Origin Area Image", format="png", type="pil", interactive=False, visible=False)
|
| 74 |
-
upscaled_image = gr.Image(label="Upscaled Image", format="png", type="pil", interactive=False)
|
| 75 |
-
download_path = gr.File(label="Download the output image", interactive=False)
|
| 76 |
-
generated_cost = gr.Textbox(label="Time cost by step (ms):", visible=True, interactive=False)
|
| 77 |
-
category = gr.Textbox(label="Category", value=DEFAULT_CATEGORY, visible=False)
|
| 78 |
-
mask_expansion = gr.Number(label="Mask Expansion", value=20, visible=False)
|
| 79 |
-
mask_dilation = gr.Slider(minimum=0, maximum=10, value=2, step=1, label="Mask Dilation", visible=False)
|
| 80 |
-
|
| 81 |
-
g_btn.click(
|
| 82 |
-
fn=segment_image,
|
| 83 |
-
inputs=[input_image, category, generate_size, mask_expansion, mask_dilation],
|
| 84 |
-
outputs=[origin_area_image, croper],
|
| 85 |
-
).success(
|
| 86 |
-
fn=upscale_image,
|
| 87 |
-
inputs=[origin_area_image, input_image_prompt, num_inference_steps],
|
| 88 |
-
outputs=[upscaled_image, generated_cost],
|
| 89 |
-
).success(
|
| 90 |
-
fn=restore_result,
|
| 91 |
-
inputs=[croper, category, upscaled_image],
|
| 92 |
-
outputs=[restored_image, download_path],
|
| 93 |
-
)
|
| 94 |
-
|
| 95 |
-
return demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|