panfayu commited on
Commit
0aba3ea
·
1 Parent(s): 95ca57d

first commit

Browse files
Files changed (5) hide show
  1. app.py +62 -0
  2. imgs/1.jpg +0 -0
  3. imgs/2.jpg +0 -0
  4. imgs/3.jpg +0 -0
  5. util.py +25 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import requests
4
+ import gradio as gr
5
+ from util import base64_to_img, img_to_base64, resize_image
6
+
7
+ url = os.getenv("REQUEST_URL")
8
+ headers = {'Content-Type': 'application/json',
9
+ 'Validation-Key': os.getenv("VALIDATION_KEY")}
10
+ names = ["input_image", "prompt", "neg_prompt", "maxlen", "step", "cfg", "seed", "up", "down", "left", "right"]
11
+ def run(*params):
12
+ params = {k:v for k, v in zip(names, params)}
13
+ image = params.pop("input_image")
14
+ image = resize_image(image)
15
+ params["image_base64"] = img_to_base64(image)
16
+ try:
17
+ response = requests.post(url, headers=headers, data=json.dumps(params), timeout=30)
18
+ if response.status_code != 200:
19
+ raise ValueError()
20
+ data = response.json()
21
+ except:
22
+ raise gr.Error("Fail to generate")
23
+ if data["code"] != 0:
24
+ raise gr.Error(data["message"])
25
+ result = base64_to_img(data["content"])
26
+ return result
27
+
28
+ with gr.Blocks() as demo:
29
+ gr.Markdown("# SDXL inpainting for Clothes2Human")
30
+ with gr.Row().style(equal_height=True):
31
+ with gr.Column():
32
+ input_image = gr.Image(type="pil", height=300)
33
+ with gr.Column():
34
+ output_image = gr.Image(type="pil", height=300)
35
+
36
+ with gr.Row():
37
+ with gr.Column():
38
+ prompt = gr.Textbox(label="Prompt")
39
+ neg_prompt = gr.Textbox(label="Negative Prompt")
40
+
41
+ maxlen = gr.Slider(label="Max Edge Length", step=32, minimum=768, maximum=1536, value=1024)
42
+ step = gr.Slider(label="Step", minimum=20, maximum=70, value=50, step=1)
43
+
44
+ with gr.Column():
45
+ up = gr.Slider(label="Scale Up Image", minimum=-0.3, maximum=0.5, value=0, step=0.1)
46
+ down = gr.Slider(label="Scale Down Image", minimum=-0.3, maximum=0.5, value=0, step=0.1)
47
+ left = gr.Slider(label="Scale Left Image", minimum=-0.3, maximum=0.5, value=0, step=0.1)
48
+ right = gr.Slider(label="Scale Right Image", minimum=-0.3, maximum=0.5, value=0, step=0.1)
49
+ with gr.Column():
50
+ cfg = gr.Slider(label="CFG Scale", minimum=1.0, maximum=9.0, value=5.0, step=0.5)
51
+ seed = gr.Slider(label="Seed", minimum=-1, maximum=1000000, value=-1, step=1)
52
+ inpaint_button = gr.Button()
53
+
54
+ run_in = [input_image, prompt, neg_prompt, maxlen, step, cfg, seed, up, down, left, right]
55
+ inpaint_button.click(run, inputs=run_in, outputs=[output_image])
56
+
57
+ gr.Examples([["imgs/1.jpg","A man wearing a white T-shirt stands on the beach","", 1024, 50, 5.0, 333866, 0.3, 0.3, 0.1, 0.1],
58
+ ["imgs/2.jpg"," woman wearing a blue dress stands in a park, asian race","", 1280, 50, 5.0, 443652, 0.3, 0.3, 0.2, 0.2],
59
+ ["imgs/3.jpg","A woman wearing a white dress stands","", 1280, 50, 5.0, 306728, -0.1, -0.2, 0, 0]],
60
+ inputs=run_in, outputs=[output_image], fn=run, cache_examples=True)
61
+
62
+ demo.queue(concurrency_count=2).launch()
imgs/1.jpg ADDED
imgs/2.jpg ADDED
imgs/3.jpg ADDED
util.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ import base64
3
+ from io import BytesIO
4
+ from pydantic import BaseModel, validator
5
+
6
+ def img_to_base64(img):
7
+ buffer = BytesIO()
8
+ img.save(buffer, "jpeg")
9
+ content = base64.b64encode(buffer.getvalue())
10
+ return str(content, 'utf-8')
11
+
12
+ def base64_to_img(content):
13
+ decoded_image = base64.b64decode(content)
14
+ image_buffer = BytesIO(decoded_image)
15
+ image = Image.open(image_buffer)
16
+ return image
17
+
18
+ def resize_image(img, maxlen=2048):
19
+ if max(img.size)<maxlen:
20
+ return img
21
+ if img.width > img.height:
22
+ img = img.resize((maxlen, int(img.height*maxlen/img.width)//8*8))
23
+ else:
24
+ img = img.resize((int(img.width*maxlen/img.height)//8*8, maxlen))
25
+ return img