leonelhs commited on
Commit
1c09667
1 Parent(s): 52b4693

update app

Browse files
Files changed (1) hide show
  1. app.py +56 -60
app.py CHANGED
@@ -1,56 +1,56 @@
1
- import zipfile
2
- from pathlib import Path
3
 
4
  import gradio as gr
5
  from gradio_client import Client
6
- from huggingface_hub import hf_hub_download
7
 
8
- clientGFPGAN = Client("Xintao/GFPGAN")
9
- clientSuperface = Client("leonelhs/superface")
10
- clientZeroScratches = Client("leonelhs/ZeroScratches")
11
- clientDeoldify = Client("leonelhs/deoldify")
12
- clientEnhanceLight = Client("leonelhs/Zero-DCE")
13
- clientZeroBackground = Client("leonelhs/remove-background")
14
- clientFaceParser = Client("leonelhs/faceparser")
15
 
16
  REPO_ID = "leonelhs/faceshine"
17
 
 
 
 
 
 
 
 
18
 
19
- def download(file):
20
- file_zip = hf_hub_download(repo_id=REPO_ID, filename=file + ".zip")
21
- with zipfile.ZipFile(file_zip, "r") as zip_ref:
22
- zip_ref.extractall(file)
23
- return list(Path(file).iterdir())
24
 
25
-
26
- def gfpgan_face(image, version, scale):
27
- return clientGFPGAN.predict(image, version, scale, fn_index=0)[0]
 
 
 
 
28
 
29
 
30
- def enhance_face(image, upsampler, face_enhancer, scale):
31
- return clientSuperface.predict(image, upsampler, face_enhancer, scale, api_name="/predict")
32
 
33
 
34
  def zero_scratches(image):
35
- return clientZeroScratches.predict(image, api_name="/predict")
36
 
37
 
38
  def colorize_photo(image):
39
- return clientDeoldify.predict(image, api_name="/predict")
40
 
41
 
42
  def enhance_light(image):
43
- return clientEnhanceLight.predict(image, api_name="/predict")
44
 
45
 
46
  def zero_background(image, new_bgr=None):
47
- # return clientZeroBackground.predict(image, new_bgr, api_name="/predict")[0]
48
  # Fixme: cant find predict function by name
49
- return clientZeroBackground.predict(image, new_bgr, fn_index=0)[1]
 
 
 
50
 
51
 
52
  def parse_face(image):
53
- return clientFaceParser.predict(image, api_name="/predict")
54
 
55
 
56
  def mirror(x):
@@ -61,6 +61,10 @@ def active_first():
61
  return gr.Tabs.update(selected=0)
62
 
63
 
 
 
 
 
64
  footer = r"""
65
  <center>
66
  <p>This App is running on a CPU, help us to upgrade a GPU or just give us a <a href='https://github.com/leonelhs/face-shine' target='_blank'>Github ⭐</a></p>
@@ -74,68 +78,60 @@ footer = r"""
74
  """
75
 
76
  with gr.Blocks(title="Face Shine") as app:
 
 
 
 
 
77
  with gr.Row():
78
  gr.HTML("<center><h1>Face Shine</h1></center>")
79
 
80
  with gr.Tabs() as tabs:
81
  with gr.TabItem("Photo restorer", id=0):
82
- with gr.Row():
83
  with gr.Column(scale=1):
84
- btn_hires = gr.Button(value="Enhance faces")
85
  btn_eraser = gr.Button(value="Erase scratches")
86
  btn_color = gr.Button(value="Colorize photo")
 
87
  btn_light = gr.Button(value="Enhance light")
88
- with gr.Accordion("New background", open=False):
89
- img_newbgr = gr.Image(label="Left empty for a transparent background", type="filepath")
90
- btn_newbgr = gr.Button(value="Clear background")
91
 
92
- with gr.Column(scale=4):
93
  with gr.Row():
94
  img_input = gr.Image(label="Input", type="filepath")
95
- img_output = gr.Image(label="Result", type="filepath", interactive=False)
96
  with gr.Row():
97
- gr.ClearButton([img_input, img_output, img_newbgr], variant="stop")
98
- btn_swap = gr.Button(value="Swap images", variant="primary")
 
 
 
 
99
 
100
  with gr.TabItem("Examples", id=1):
101
- gr.Examples(examples=download("lowres"), inputs=[img_input], label="Low resolution")
102
- gr.Examples(examples=download("gray"), inputs=[img_input], label="Gray scale")
103
- gr.Examples(examples=download("scratch"), inputs=[img_input], label="Scratched")
104
- gr.Examples(examples=download("backs"), inputs=[img_newbgr], label="Backgrounds")
105
  gr.Button(value="Ok", variant="primary").click(active_first, None, tabs)
106
 
107
  with gr.TabItem("Settings", id=2):
108
  with gr.Accordion("Image restoration settings", open=False):
109
- enhancer = gr.Dropdown([
110
- 'v1.2',
111
- 'v1.3',
112
- 'v1.4',
113
- 'RestoreFormer'],
114
- type="value", value='RestoreFormer', label='GFPGAN face restoration algorithm',
115
- info="version")
116
- restorer = gr.Dropdown([
117
- 'RealESRGAN_x2plus',
118
- 'RealESRGAN_x4plus',
119
- 'RealESRNet_x4plus',
120
- 'AI-Forever_x2plus',
121
- 'AI-Forever_x4plus',
122
- 'RealESRGAN_x4plus_anime_6B',
123
- 'realesr-animevideov3',
124
- 'realesr-general-x4v3'],
125
- type="value", value='RealESRGAN_x4plus',
126
- label='Real-ESRGAN image restoration algorithm', info="version", visible=False)
127
- rescale = gr.Dropdown(["1", "2", "3", "4"], type="value", value="2", label="Rescaling factor")
128
  with gr.Accordion("Logs info", open=False):
129
  text_logger = gr.Textbox(label="login", lines=5, show_label=False)
130
  gr.Button("Save settings")
131
 
132
- # btn_hires.click(enhance_face, inputs=[img_input, restorer, enhancer, rescale], outputs=[img_output, text_logger])
133
  btn_hires.click(gfpgan_face, inputs=[img_input, enhancer, rescale], outputs=[img_output])
134
  btn_eraser.click(zero_scratches, inputs=[img_input], outputs=[img_output])
135
  btn_color.click(colorize_photo, inputs=[img_input], outputs=[img_output])
136
  btn_light.click(enhance_light, inputs=[img_input], outputs=[img_output])
137
- btn_newbgr.click(zero_background, inputs=[img_input, img_newbgr], outputs=[img_output])
138
  btn_swap.click(mirror, inputs=[img_output], outputs=[img_input])
 
139
 
140
  with gr.Row():
141
  gr.HTML(footer)
 
1
+ from glob import glob
 
2
 
3
  import gradio as gr
4
  from gradio_client import Client
 
5
 
6
+ from utils import make_flatten_background
 
 
 
 
 
 
7
 
8
  REPO_ID = "leonelhs/faceshine"
9
 
10
+ clients = {
11
+ "GFPGAN": "leonelhs/GFPGAN",
12
+ "ZeroScratches": "leonelhs/ZeroScratches",
13
+ "Deoldify": "leonelhs/deoldify",
14
+ "EnhanceLight": "leonelhs/Zero-DCE",
15
+ "ZeroBackground": "leonelhs/rembg",
16
+ }
17
 
 
 
 
 
 
18
 
19
+ def load_client(space):
20
+ try:
21
+ return Client(space)
22
+ except ValueError as err:
23
+ print(err)
24
+ logger.value.append(f"Space: {space}, log: {err}")
25
+ pass
26
 
27
 
28
+ def gfpgan_face(image, version, scale):
29
+ return clients["GFPGAN"].predict(image, version, scale, fn_index=0)[0]
30
 
31
 
32
  def zero_scratches(image):
33
+ return clients["ZeroScratches"].predict(image, api_name="/predict")
34
 
35
 
36
  def colorize_photo(image):
37
+ return clients["Deoldify"].predict(image, api_name="/predict")
38
 
39
 
40
  def enhance_light(image):
41
+ return clients["EnhanceLight"].predict(image, api_name="/predict")
42
 
43
 
44
  def zero_background(image, new_bgr=None):
 
45
  # Fixme: cant find predict function by name
46
+ # return clients["ZeroBackground"].predict(image, new_bgr, fn_index=0)[1]
47
+ # return clients["ZeroBackground"].predict(image, fn_index=0)
48
+ img, mask = clients["ZeroBackground"].predict(image, "U2NET Human Seg", False, fn_index=9)
49
+ return make_flatten_background(img, mask)
50
 
51
 
52
  def parse_face(image):
53
+ return clients["FaceParser"].predict(image, api_name="/predict")
54
 
55
 
56
  def mirror(x):
 
61
  return gr.Tabs.update(selected=0)
62
 
63
 
64
+ def clear():
65
+ return None, None
66
+
67
+
68
  footer = r"""
69
  <center>
70
  <p>This App is running on a CPU, help us to upgrade a GPU or just give us a <a href='https://github.com/leonelhs/face-shine' target='_blank'>Github ⭐</a></p>
 
78
  """
79
 
80
  with gr.Blocks(title="Face Shine") as app:
81
+ logger = gr.State(value=[])
82
+
83
+ for client, endpoint in clients.items():
84
+ clients[client] = load_client(endpoint)
85
+
86
  with gr.Row():
87
  gr.HTML("<center><h1>Face Shine</h1></center>")
88
 
89
  with gr.Tabs() as tabs:
90
  with gr.TabItem("Photo restorer", id=0):
91
+ with gr.Row(equal_height=False):
92
  with gr.Column(scale=1):
 
93
  btn_eraser = gr.Button(value="Erase scratches")
94
  btn_color = gr.Button(value="Colorize photo")
95
+ btn_hires = gr.Button(value="Enhance face")
96
  btn_light = gr.Button(value="Enhance light")
97
+ btn_clear = gr.Button(value="Flatten background")
 
 
98
 
99
+ with gr.Column(scale=2):
100
  with gr.Row():
101
  img_input = gr.Image(label="Input", type="filepath")
 
102
  with gr.Row():
103
+ btn_reset = gr.Button(value="Reset", variant="stop")
104
+ btn_swap = gr.Button(value="Ok", variant="primary")
105
+
106
+ with gr.Column(scale=2):
107
+ with gr.Row():
108
+ img_output = gr.Image(label="Result", type="filepath", interactive=False)
109
 
110
  with gr.TabItem("Examples", id=1):
111
+ gr.Examples(examples=glob("lowres/*"), inputs=[img_input], label="Low resolution")
112
+ gr.Examples(examples=glob("gray/*"), inputs=[img_input], label="Gray scale")
113
+ gr.Examples(examples=glob("scratch/*"), inputs=[img_input], label="Scratched")
 
114
  gr.Button(value="Ok", variant="primary").click(active_first, None, tabs)
115
 
116
  with gr.TabItem("Settings", id=2):
117
  with gr.Accordion("Image restoration settings", open=False):
118
+ enhancer = gr.Dropdown(['v1.2', 'v1.3', 'v1.4', 'RestoreFormer'],
119
+ label='GFPGAN face restoration algorithm',
120
+ type="value", value='RestoreFormer',
121
+ info="version")
122
+ rescale = gr.Dropdown(["1", "2", "3", "4"],
123
+ type="value", value="2", label="Rescaling factor")
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  with gr.Accordion("Logs info", open=False):
125
  text_logger = gr.Textbox(label="login", lines=5, show_label=False)
126
  gr.Button("Save settings")
127
 
 
128
  btn_hires.click(gfpgan_face, inputs=[img_input, enhancer, rescale], outputs=[img_output])
129
  btn_eraser.click(zero_scratches, inputs=[img_input], outputs=[img_output])
130
  btn_color.click(colorize_photo, inputs=[img_input], outputs=[img_output])
131
  btn_light.click(enhance_light, inputs=[img_input], outputs=[img_output])
132
+ btn_clear.click(zero_background, inputs=[img_input], outputs=[img_output])
133
  btn_swap.click(mirror, inputs=[img_output], outputs=[img_input])
134
+ btn_reset.click(clear, outputs=[img_input, img_output])
135
 
136
  with gr.Row():
137
  gr.HTML(footer)