fffiloni commited on
Commit
8299c8e
1 Parent(s): 49771b5

refactor to blocks

Browse files
Files changed (1) hide show
  1. app.py +101 -108
app.py CHANGED
@@ -6,98 +6,7 @@ import os
6
 
7
  from utils.gradio_helpers import parse_outputs, process_outputs
8
 
9
- inputs = []
10
- inputs.append(gr.Image(
11
- label="Image", type="filepath"
12
- ))
13
 
14
- inputs.append(gr.Slider(
15
- label="Rotate Pitch", info='''Rotation pitch: Adjusts the up and down tilt of the face''', value=0,
16
- minimum=-20, maximum=20
17
- ))
18
-
19
- inputs.append(gr.Slider(
20
- label="Rotate Yaw", info='''Rotation yaw: Adjusts the left and right turn of the face''', value=0,
21
- minimum=-20, maximum=20
22
- ))
23
-
24
- inputs.append(gr.Slider(
25
- label="Rotate Roll", info='''Rotation roll: Adjusts the tilt of the face to the left or right''', value=0,
26
- minimum=-20, maximum=20
27
- ))
28
-
29
- inputs.append(gr.Slider(
30
- label="Blink", info='''Blink: Controls the degree of eye closure''', value=0,
31
- minimum=-20, maximum=5
32
- ))
33
-
34
- inputs.append(gr.Slider(
35
- label="Eyebrow", info='''Eyebrow: Adjusts the height and shape of the eyebrows''', value=0,
36
- minimum=-10, maximum=15
37
- ))
38
-
39
- inputs.append(gr.Number(
40
- label="Wink", info='''Wink: Controls the degree of one eye closing''', value=0
41
- ))
42
-
43
- inputs.append(gr.Slider(
44
- label="Pupil X", info='''Pupil X: Adjusts the horizontal position of the pupils''', value=0,
45
- minimum=-15, maximum=15
46
- ))
47
-
48
- inputs.append(gr.Slider(
49
- label="Pupil Y", info='''Pupil Y: Adjusts the vertical position of the pupils''', value=0,
50
- minimum=-15, maximum=15
51
- ))
52
-
53
- inputs.append(gr.Slider(
54
- label="Aaa", info='''AAA: Controls the mouth opening for 'aaa' sound''', value=0,
55
- minimum=-30, maximum=120
56
- ))
57
-
58
- inputs.append(gr.Slider(
59
- label="Eee", info='''EEE: Controls the mouth shape for 'eee' sound''', value=0,
60
- minimum=-20, maximum=15
61
- ))
62
-
63
- inputs.append(gr.Slider(
64
- label="Woo", info='''WOO: Controls the mouth shape for 'woo' sound''', value=0,
65
- minimum=-20, maximum=15
66
- ))
67
-
68
- inputs.append(gr.Slider(
69
- label="Smile", info='''Smile: Adjusts the degree of smiling''', value=0,
70
- minimum=-0.3, maximum=1.3
71
- ))
72
-
73
- inputs.append(gr.Number(
74
- label="Src Ratio", info='''Source ratio''', value=1
75
- ))
76
-
77
- inputs.append(gr.Slider(
78
- label="Sample Ratio", info='''Sample ratio''', value=1,
79
- minimum=-0.2, maximum=1.2
80
- ))
81
-
82
- inputs.append(gr.Slider(
83
- label="Crop Factor", info='''Crop factor''', value=1.7,
84
- minimum=1.5, maximum=2.5
85
- ))
86
-
87
- inputs.append(gr.Dropdown(
88
- choices=['webp', 'jpg', 'png'], label="output_format", info='''Format of the output images''', value="webp"
89
- ))
90
-
91
- inputs.append(gr.Number(
92
- label="Output Quality", info='''Quality of the output images, from 0 to 100. 100 is best quality, 0 is lowest quality.''', value=95
93
- ))
94
-
95
- names = ['image', 'rotate_pitch', 'rotate_yaw', 'rotate_roll', 'blink', 'eyebrow', 'wink', 'pupil_x', 'pupil_y', 'aaa', 'eee', 'woo', 'smile', 'src_ratio', 'sample_ratio', 'crop_factor', 'output_format', 'output_quality']
96
-
97
- outputs = []
98
- outputs.append(gr.Image())
99
-
100
- expected_outputs = len(outputs)
101
  def predict(request: gr.Request, *args, progress=gr.Progress(track_tqdm=True)):
102
  headers = {'Content-Type': 'application/json'}
103
 
@@ -130,14 +39,6 @@ def predict(request: gr.Request, *args, progress=gr.Progress(track_tqdm=True)):
130
  return json_response["output"]
131
  predict_outputs = parse_outputs(json_response["output"])
132
  processed_outputs = process_outputs(predict_outputs)
133
- difference_outputs = expected_outputs - len(processed_outputs)
134
- # If less outputs than expected, hide the extra ones
135
- if difference_outputs > 0:
136
- extra_outputs = [gr.update(visible=False)] * difference_outputs
137
- processed_outputs.extend(extra_outputs)
138
- # If more outputs than expected, cap the outputs to the expected number
139
- elif difference_outputs < 0:
140
- processed_outputs = processed_outputs[:difference_outputs]
141
 
142
  return tuple(processed_outputs) if len(processed_outputs) > 1 else processed_outputs[0]
143
  else:
@@ -148,13 +49,105 @@ def predict(request: gr.Request, *args, progress=gr.Progress(track_tqdm=True)):
148
  title = "Demo for expression-editor cog image by fofr"
149
  model_description = "None"
150
 
151
- app = gr.Interface(
152
- fn=predict,
153
- inputs=inputs,
154
- outputs=outputs,
155
- title=title,
156
- description=model_description,
157
- allow_flagging="never",
158
- )
159
- app.launch(share=False, show_error=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
 
 
6
 
7
  from utils.gradio_helpers import parse_outputs, process_outputs
8
 
 
 
 
 
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  def predict(request: gr.Request, *args, progress=gr.Progress(track_tqdm=True)):
11
  headers = {'Content-Type': 'application/json'}
12
 
 
39
  return json_response["output"]
40
  predict_outputs = parse_outputs(json_response["output"])
41
  processed_outputs = process_outputs(predict_outputs)
 
 
 
 
 
 
 
 
42
 
43
  return tuple(processed_outputs) if len(processed_outputs) > 1 else processed_outputs[0]
44
  else:
 
49
  title = "Demo for expression-editor cog image by fofr"
50
  model_description = "None"
51
 
52
+ with gr.Blocks() as demo:
53
+ with gr.Column():
54
+ with gr.Row():
55
+ with gr.Column():
56
+ image = gr.Image(
57
+ label="Image",
58
+ type="filepath"
59
+ )
60
+ rotate_pitch = gr.Slider(
61
+ label="Rotate Pitch", info='''Rotation pitch: Adjusts the up and down tilt of the face''',
62
+ value=0,
63
+ minimum=-20, maximum=20
64
+ )
65
+ rotate_yaw = gr.Slider(
66
+ label="Rotate Yaw", info='''Rotation yaw: Adjusts the left and right turn of the face''',
67
+ value=0,
68
+ minimum=-20, maximum=20
69
+ )
70
+ rotate_roll = gr.Slider(
71
+ label="Rotate Roll", info='''Rotation roll: Adjusts the tilt of the face to the left or right''', value=0,
72
+ minimum=-20, maximum=20
73
+ )
74
+ blink = gr.Slider(
75
+ label="Blink", info='''Blink: Controls the degree of eye closure''', value=0,
76
+ minimum=-20, maximum=5
77
+ )
78
+ eyebrow = gr.Slider(
79
+ label="Eyebrow", info='''Eyebrow: Adjusts the height and shape of the eyebrows''', value=0,
80
+ minimum=-10, maximum=15
81
+ )
82
+ wink = gr.Number(
83
+ label="Wink", info='''Wink: Controls the degree of one eye closing''', value=0
84
+ )
85
+ pupil_x = gr.Slider(
86
+ label="Pupil X", info='''Pupil X: Adjusts the horizontal position of the pupils''', value=0,
87
+ minimum=-15, maximum=15
88
+ )
89
+ pupil_y = gr.Slider(
90
+ label="Pupil Y", info='''Pupil Y: Adjusts the vertical position of the pupils''', value=0,
91
+ minimum=-15, maximum=15
92
+ )
93
+ aaa = gr.Slider(
94
+ label="Aaa", info='''AAA: Controls the mouth opening for 'aaa' sound''', value=0,
95
+ minimum=-30, maximum=120
96
+ )
97
+ eee = gr.Slider(
98
+ label="Eee", info='''EEE: Controls the mouth shape for 'eee' sound''', value=0,
99
+ minimum=-20, maximum=15
100
+ )
101
+ woo = gr.Slider(
102
+ label="Woo", info='''WOO: Controls the mouth shape for 'woo' sound''', value=0,
103
+ minimum=-20, maximum=15
104
+ )
105
+ smile = gr.Slider(
106
+ label="Smile", info='''Smile: Adjusts the degree of smiling''', value=0,
107
+ minimum=-0.3, maximum=1.3
108
+ )
109
+ src_ratio = gr.Number(
110
+ label="Src Ratio", info='''Source ratio''', value=1
111
+ )
112
+ sample_ratio = gr.Slider(
113
+ label="Sample Ratio", info='''Sample ratio''', value=1,
114
+ minimum=-0.2, maximum=1.2
115
+ )
116
+ crop_factor = gr.Slider(
117
+ label="Crop Factor", info='''Crop factor''', value=1.7,
118
+ minimum=1.5, maximum=2.5
119
+ )
120
+ output_format = gr.Dropdown(
121
+ choices=['webp', 'jpg', 'png'], label="output_format", info='''Format of the output images''', value="webp"
122
+ )
123
+ output_quality = gr.Number(
124
+ label="Output Quality", info='''Quality of the output images, from 0 to 100. 100 is best quality, 0 is lowest quality.''', value=95
125
+ )
126
+ submit_btn = gr.Button("Submit")
127
+ with gr.Column():
128
+ result_image = gr.Image()
129
+
130
+ inputs = [image, rotate_pitch, rotate_yaw, rotate_roll, blink, eyebrow, wink, pupil_x, pupil_y, aaa, eee, woo, smile, src_ratio, sample_ratio, crop_factor, output_format, output_quality]
131
+ outputs = [result_image]
132
+
133
+ submit_btn.click(
134
+ fn=predict,
135
+ inputs=inputs,
136
+ outputs=outputs,
137
+ )
138
+
139
+ rotate_pitch.change(fn=predict, inputs=inputs, outputs=outputs)
140
+ rotate_yaw.change(fn=predict, inputs=inputs, outputs=outputs)
141
+ rotate_roll.change(fn=predict, inputs=inputs, outputs=outputs)
142
+ blink.change(fn=predict, inputs=inputs, outputs=outputs)
143
+ eyebrow.change(fn=predict, inputs=inputs, outputs=outputs)
144
+ wink.change(fn=predict, inputs=inputs, outputs=outputs)
145
+ pupil_x.change(fn=predict, inputs=inputs, outputs=outputs)
146
+ pupil_y.change(fn=predict, inputs=inputs, outputs=outputs)
147
+ aaa.change(fn=predict, inputs=inputs, outputs=outputs)
148
+ eee.change(fn=predict, inputs=inputs, outputs=outputs)
149
+ woo.change(fn=predict, inputs=inputs, outputs=outputs)
150
+ smile.change(fn=predict, inputs=inputs, outputs=outputs)
151
+
152
+ demo.launch(share=False, show_error=True)
153