Spaces:
Paused
Paused
File size: 9,237 Bytes
16c783e 9613d25 16c783e edc922b 16c783e c606315 9613d25 6d90ae1 16cd269 873990e c606315 16cd269 873990e c606315 873990e c606315 16cd269 c606315 16cd269 c606315 873990e c606315 16cd269 c606315 873990e c606315 16cd269 c606315 16cd269 c606315 873990e c606315 16cd269 c606315 873990e 16cd269 873990e 16cd269 873990e c606315 6d90ae1 c606315 16cd269 c606315 8299c8e 16cd269 c606315 16cd269 8299c8e 16cd269 8299c8e 6d90ae1 293de33 6d90ae1 293de33 9613d25 6d90ae1 9613d25 16cd269 6d90ae1 8299c8e c606315 8299c8e c606315 8299c8e 6d90ae1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
import gradio as gr
from urllib.parse import urlparse
import requests
import time
import os
from utils.gradio_helpers import parse_outputs, process_outputs
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']
def predict(request: gr.Request, *args, progress=gr.Progress(track_tqdm=True)):
headers = {'Content-Type': 'application/json'}
payload = {"input": {}}
base_url = "http://0.0.0.0:7860"
for i, key in enumerate(names):
value = args[i]
if value and (os.path.exists(str(value))):
value = f"{base_url}/file=" + value
if value is not None and value != "":
payload["input"][key] = value
response = requests.post("http://0.0.0.0:5000/predictions", headers=headers, json=payload)
if response.status_code == 201:
follow_up_url = response.json()["urls"]["get"]
response = requests.get(follow_up_url, headers=headers)
while response.json()["status"] != "succeeded":
if response.json()["status"] == "failed":
raise gr.Error("The submission failed!")
response = requests.get(follow_up_url, headers=headers)
time.sleep(1)
if response.status_code == 200:
json_response = response.json()
if(outputs[0].get_config()["name"] == "json"):
return json_response["output"]
predict_outputs = parse_outputs(json_response["output"])
processed_outputs = process_outputs(predict_outputs)
return tuple(processed_outputs) if len(processed_outputs) > 1 else processed_outputs[0]
else:
if(response.status_code == 409):
raise gr.Error(f"Sorry, the Cog image is still processing. Try again in a bit.")
raise gr.Error(f"The submission failed! Error: {response.status_code}")
def check_password(password):
return password == "pixo"
css = '''
#top{position: fixed; right: 20px; top: 20px; max-width: 300px; z-index: 1000;}
body {
font-family: 'Roboto', sans-serif;
background: linear-gradient(135deg, #f5d0fe, #d8b4fe);
color: #4a0e4e;
transition: background-color 0.3s, color 0.3s;
}
.container {
background-color: rgba(255, 255, 255, 0.2);
backdrop-filter: blur(10px);
border-radius: 15px;
box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37);
padding: 20px;
margin-bottom: 20px;
}
.gradio-slider input[type="range"] {
accent-color: #d946ef;
}
.gradio-button {
background-color: #a21caf;
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s, transform 0.1s;
font-weight: bold;
}
.gradio-button:hover {
background-color: #86198f;
transform: translateY(-2px);
}
.gradio-slider, .gradio-dropdown, .gradio-checkbox, .gradio-radio, .gradio-textbox, .gradio-number {
background-color: rgba(255, 255, 255, 0.1);
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 5px;
padding: 10px;
margin-bottom: 10px;
}
h1, h2, h3 {
color: #701a75;
text-shadow: 1px 1px 2px rgba(0,0,0,0.1);
}
@media (prefers-color-scheme: dark) {
body {
background: linear-gradient(135deg, #3b0764, #581c87);
color: #e9d5ff;
}
.container {
background-color: rgba(0, 0, 0, 0.2);
}
h1, h2, h3 {
color: #d8b4fe;
}
.gradio-button {
background-color: #d946ef;
color: #1a1a1a;
}
.gradio-button:hover {
background-color: #e879f9;
}
}
'''
with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
gr.Markdown("# 🎭 Expression Editor")
with gr.Column():
password = gr.Textbox(type="password", label="Enter password", placeholder="Enter 'pixo' to access")
login_button = gr.Button("Login", variant="primary")
with gr.Column(visible=False) as main_interface:
gr.Markdown("## 🎨 Demo for expression-editor cog image by fofr")
with gr.Row():
with gr.Column(scale=2, elem_classes="container"):
image = gr.Image(
label="Input image",
type="filepath",
height=180
)
with gr.Row():
rotate_pitch = gr.Slider(
label="Rotate Up-Down",
value=0,
minimum=-20, maximum=20
)
rotate_yaw = gr.Slider(
label="Rotate Left-Right turn",
value=0,
minimum=-20, maximum=20
)
rotate_roll = gr.Slider(
label="Rotate Left-Right tilt", value=0,
minimum=-20, maximum=20
)
with gr.Row():
blink = gr.Slider(
label="Blink", value=0,
minimum=-20, maximum=5
)
eyebrow = gr.Slider(
label="Eyebrow", value=0,
minimum=-10, maximum=15
)
wink = gr.Slider(
label="Wink", value=0,
minimum=0, maximum=25
)
with gr.Row():
pupil_x = gr.Slider(
label="Pupil X", value=0,
minimum=-15, maximum=15
)
pupil_y = gr.Slider(
label="Pupil Y", value=0,
minimum=-15, maximum=15
)
with gr.Row():
aaa = gr.Slider(
label="Aaa", value=0,
minimum=-30, maximum=120
)
eee = gr.Slider(
label="Eee", value=0,
minimum=-20, maximum=15
)
woo = gr.Slider(
label="Woo", value=0,
minimum=-20, maximum=15
)
smile = gr.Slider(
label="Smile", value=0,
minimum=-0.3, maximum=1.3
)
with gr.Accordion("More Settings", open=False):
src_ratio = gr.Number(
label="Src Ratio", info='''Source ratio''', value=1
)
sample_ratio = gr.Slider(
label="Sample Ratio", info='''Sample ratio''', value=1,
minimum=-0.2, maximum=1.2
)
crop_factor = gr.Slider(
label="Crop Factor", info='''Crop factor''', value=1.7,
minimum=1.5, maximum=2.5
)
output_format = gr.Dropdown(
choices=['webp', 'jpg', 'png'], label="output_format", info='''Format of the output images''', value="webp"
)
output_quality = gr.Number(
label="Output Quality", info='''Quality of the output images, from 0 to 100. 100 is best quality, 0 is lowest quality.''', value=95
)
submit_btn = gr.Button("Generate", variant="primary")
with gr.Column(scale=1):
result_image = gr.Image(elem_id="top", label="Generated Image")
gr.HTML("""
<div style="display: flex; justify-content: center; align-items: center; text-align: center;">
<p style="display: flex;gap: 6px;">
<a href="https://huggingface.co/spaces/fffiloni/expression-editor?duplicate=true">
<img src="https://huggingface.co/datasets/huggingface/badges/resolve/main/duplicate-this-space-md.svg" alt="Duplicate this Space">
</a> to skip the queue and enjoy faster inference on the GPU of your choice
</p>
</div>
""")
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]
outputs = [result_image]
def login(password):
if check_password(password):
return gr.update(visible=True), gr.update(visible=False)
else:
return gr.update(visible=False), gr.update(visible=True, value="")
login_button.click(
fn=login,
inputs=[password],
outputs=[main_interface, password]
)
submit_btn.click(
fn=predict,
inputs=inputs,
outputs=outputs,
)
for slider in [rotate_pitch, rotate_yaw, rotate_roll, blink, eyebrow, wink, pupil_x, pupil_y, aaa, eee, woo, smile]:
slider.release(fn=predict, inputs=inputs, outputs=outputs, show_progress="minimal")
demo.launch(share=False, show_error=True) |