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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from urllib.parse import urlparse
|
| 4 |
+
import requests
|
| 5 |
+
import time
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
from utils.gradio_helpers import parse_outputs, process_outputs
|
| 9 |
+
|
| 10 |
+
# Function to verify the image file type and resize it if necessary
|
| 11 |
+
def preprocess_image(image_path):
|
| 12 |
+
# Check if the file exists
|
| 13 |
+
if not os.path.exists(image_path):
|
| 14 |
+
raise FileNotFoundError(f"No such file: '{image_path}'")
|
| 15 |
+
|
| 16 |
+
# Get the file extension and make sure it's a valid image format
|
| 17 |
+
valid_extensions = ['jpg', 'jpeg', 'png', 'webp']
|
| 18 |
+
file_extension = image_path.split('.')[-1].lower()
|
| 19 |
+
|
| 20 |
+
if file_extension not in valid_extensions:
|
| 21 |
+
raise ValueError("Invalid file type. Only JPG, PNG, and WEBP are allowed.")
|
| 22 |
+
|
| 23 |
+
# Open the image
|
| 24 |
+
with Image.open(image_path) as img:
|
| 25 |
+
width, height = img.size
|
| 26 |
+
|
| 27 |
+
# Check if any dimension exceeds 1024 pixels
|
| 28 |
+
if width > 1024 or height > 1024:
|
| 29 |
+
# Calculate the new size while maintaining aspect ratio
|
| 30 |
+
if width > height:
|
| 31 |
+
new_width = 1024
|
| 32 |
+
new_height = int((new_width / width) * height)
|
| 33 |
+
else:
|
| 34 |
+
new_height = 1024
|
| 35 |
+
new_width = int((new_height / height) * width)
|
| 36 |
+
|
| 37 |
+
# Resize the image
|
| 38 |
+
img_resized = img.resize((new_width, new_height), Image.LANCZOS)
|
| 39 |
+
print(f"Resized image to {new_width}x{new_height}.")
|
| 40 |
+
|
| 41 |
+
# Save the resized image as 'resized_image.jpg'
|
| 42 |
+
output_path = 'resized_image.jpg'
|
| 43 |
+
img_resized.save(output_path, 'JPEG')
|
| 44 |
+
print(f"Resized image saved as {output_path}")
|
| 45 |
+
return output_path
|
| 46 |
+
else:
|
| 47 |
+
print("Image size is within the limit, no resizing needed.")
|
| 48 |
+
return image_path
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def display_uploaded_image(image_in):
|
| 52 |
+
return image_in
|
| 53 |
+
|
| 54 |
+
def reset_parameters():
|
| 55 |
+
return gr.update(value=0), gr.update(value=0), gr.update(value=0), gr.update(value=0), gr.update(value=0), gr.update(value=0), gr.update(value=0), gr.update(value=0), gr.update(value=0), gr.update(value=0), gr.update(value=0), gr.update(value=0)
|
| 56 |
+
|
| 57 |
+
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']
|
| 58 |
+
|
| 59 |
+
def predict(request: gr.Request, *args, progress=gr.Progress(track_tqdm=True)):
|
| 60 |
+
headers = {'Content-Type': 'application/json'}
|
| 61 |
+
|
| 62 |
+
payload = {"input": {}}
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
base_url = "http://0.0.0.0:7860"
|
| 66 |
+
for i, key in enumerate(names):
|
| 67 |
+
value = args[i]
|
| 68 |
+
if value and (os.path.exists(str(value))):
|
| 69 |
+
value = f"{base_url}/file=" + value
|
| 70 |
+
if value is not None and value != "":
|
| 71 |
+
payload["input"][key] = value
|
| 72 |
+
|
| 73 |
+
time.sleep(0.4)
|
| 74 |
+
response = requests.post("http://0.0.0.0:5000/predictions", headers=headers, json=payload)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
if response.status_code == 201:
|
| 78 |
+
time.sleep(0.4)
|
| 79 |
+
follow_up_url = response.json()["urls"]["get"]
|
| 80 |
+
response = requests.get(follow_up_url, headers=headers)
|
| 81 |
+
while response.json()["status"] != "succeeded":
|
| 82 |
+
if response.json()["status"] == "failed":
|
| 83 |
+
raise gr.Error("The submission failed!")
|
| 84 |
+
response = requests.get(follow_up_url, headers=headers)
|
| 85 |
+
|
| 86 |
+
if response.status_code == 200:
|
| 87 |
+
|
| 88 |
+
json_response = response.json()
|
| 89 |
+
#If the output component is JSON return the entire output response
|
| 90 |
+
if(outputs[0].get_config()["name"] == "json"):
|
| 91 |
+
return json_response["output"]
|
| 92 |
+
predict_outputs = parse_outputs(json_response["output"])
|
| 93 |
+
processed_outputs = process_outputs(predict_outputs)
|
| 94 |
+
return tuple(processed_outputs) if len(processed_outputs) > 1 else processed_outputs[0]
|
| 95 |
+
else:
|
| 96 |
+
time.sleep(1)
|
| 97 |
+
if(response.status_code == 409):
|
| 98 |
+
raise gr.Error(f"Sorry, the Cog image is still processing. Try again in a bit.")
|
| 99 |
+
raise gr.Error(f"The submission failed! Error: {response.status_code}")
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
css = '''
|
| 103 |
+
#col-container{max-width: 720px;margin: 0 auto;}
|
| 104 |
+
'''
|
| 105 |
+
with gr.Blocks(css=css) as demo:
|
| 106 |
+
with gr.Column(elem_id="col-container"):
|
| 107 |
+
gr.Markdown("# Expression Editor")
|
| 108 |
+
gr.Markdown("Demo for expression-editor cog image by fofr")
|
| 109 |
+
with gr.Row():
|
| 110 |
+
with gr.Column():
|
| 111 |
+
image = gr.Image(
|
| 112 |
+
label="Input image",
|
| 113 |
+
sources=["upload"],
|
| 114 |
+
type="filepath",
|
| 115 |
+
height=180
|
| 116 |
+
)
|
| 117 |
+
with gr.Tab("HEAD"):
|
| 118 |
+
with gr.Column():
|
| 119 |
+
rotate_pitch = gr.Slider(
|
| 120 |
+
label="Rotate Up-Down",
|
| 121 |
+
value=0,
|
| 122 |
+
minimum=-20, maximum=20
|
| 123 |
+
)
|
| 124 |
+
rotate_yaw = gr.Slider(
|
| 125 |
+
label="Rotate Left-Right turn",
|
| 126 |
+
value=0,
|
| 127 |
+
minimum=-20, maximum=20
|
| 128 |
+
)
|
| 129 |
+
rotate_roll = gr.Slider(
|
| 130 |
+
label="Rotate Left-Right tilt", value=0,
|
| 131 |
+
minimum=-20, maximum=20
|
| 132 |
+
)
|
| 133 |
+
with gr.Tab("EYES"):
|
| 134 |
+
with gr.Column():
|
| 135 |
+
eyebrow = gr.Slider(
|
| 136 |
+
label="Eyebrow", value=0,
|
| 137 |
+
minimum=-10, maximum=15
|
| 138 |
+
)
|
| 139 |
+
with gr.Row():
|
| 140 |
+
blink = gr.Slider(
|
| 141 |
+
label="Blink", value=0,
|
| 142 |
+
minimum=-20, maximum=5
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
wink = gr.Slider(
|
| 146 |
+
label="Wink", value=0,
|
| 147 |
+
minimum=0, maximum=25
|
| 148 |
+
)
|
| 149 |
+
with gr.Row():
|
| 150 |
+
pupil_x = gr.Slider(
|
| 151 |
+
label="Pupil X", value=0,
|
| 152 |
+
minimum=-15, maximum=15
|
| 153 |
+
)
|
| 154 |
+
pupil_y = gr.Slider(
|
| 155 |
+
label="Pupil Y", value=0,
|
| 156 |
+
minimum=-15, maximum=15
|
| 157 |
+
)
|
| 158 |
+
with gr.Tab("MOUTH"):
|
| 159 |
+
with gr.Column():
|
| 160 |
+
with gr.Row():
|
| 161 |
+
aaa = gr.Slider(
|
| 162 |
+
label="Aaa", value=0,
|
| 163 |
+
minimum=-30, maximum=120
|
| 164 |
+
)
|
| 165 |
+
eee = gr.Slider(
|
| 166 |
+
label="Eee", value=0,
|
| 167 |
+
minimum=-20, maximum=15
|
| 168 |
+
)
|
| 169 |
+
woo = gr.Slider(
|
| 170 |
+
label="Woo", value=0,
|
| 171 |
+
minimum=-20, maximum=15
|
| 172 |
+
)
|
| 173 |
+
smile = gr.Slider(
|
| 174 |
+
label="Smile", value=0,
|
| 175 |
+
minimum=-0.3, maximum=1.3
|
| 176 |
+
)
|
| 177 |
+
with gr.Tab("More Settings"):
|
| 178 |
+
with gr.Column():
|
| 179 |
+
src_ratio = gr.Number(
|
| 180 |
+
label="Src Ratio", info='''Source ratio''', value=1
|
| 181 |
+
)
|
| 182 |
+
sample_ratio = gr.Slider(
|
| 183 |
+
label="Sample Ratio", info='''Sample ratio''', value=1,
|
| 184 |
+
minimum=-0.2, maximum=1.2
|
| 185 |
+
)
|
| 186 |
+
crop_factor = gr.Slider(
|
| 187 |
+
label="Crop Factor", info='''Crop factor''', value=1.7,
|
| 188 |
+
minimum=1.5, maximum=2.5
|
| 189 |
+
)
|
| 190 |
+
output_format = gr.Dropdown(
|
| 191 |
+
choices=['webp', 'jpg', 'png'], label="output_format", info='''Format of the output images''', value="webp"
|
| 192 |
+
)
|
| 193 |
+
output_quality = gr.Number(
|
| 194 |
+
label="Output Quality", info='''Quality of the output images, from 0 to 100. 100 is best quality, 0 is lowest quality.''', value=95
|
| 195 |
+
)
|
| 196 |
+
with gr.Row():
|
| 197 |
+
reset_btn = gr.Button("Reset")
|
| 198 |
+
submit_btn = gr.Button("Submit")
|
| 199 |
+
with gr.Column():
|
| 200 |
+
result_image = gr.Image(elem_id="top")
|
| 201 |
+
gr.HTML("""
|
| 202 |
+
<div style="display: flex; flex-direction: column;justify-content: center; align-items: center; text-align: center;">
|
| 203 |
+
<p style="display: flex;gap: 6px;">
|
| 204 |
+
<a href="https://huggingface.co/spaces/fffiloni/expression-editor?duplicate=true">
|
| 205 |
+
<img src="https://huggingface.co/datasets/huggingface/badges/resolve/main/duplicate-this-space-lg.svg" alt="Duplicate this Space">
|
| 206 |
+
</a>
|
| 207 |
+
</p>
|
| 208 |
+
<p>to skip the queue and enjoy faster inference on the GPU of your choice </p>
|
| 209 |
+
</div>
|
| 210 |
+
""")
|
| 211 |
+
|
| 212 |
+
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]
|
| 213 |
+
outputs = [result_image]
|
| 214 |
+
|
| 215 |
+
image.upload(
|
| 216 |
+
fn = preprocess_image,
|
| 217 |
+
inputs = [image],
|
| 218 |
+
outputs = [image],
|
| 219 |
+
queue = False
|
| 220 |
+
)
|
| 221 |
+
|
| 222 |
+
reset_btn.click(
|
| 223 |
+
fn = reset_parameters,
|
| 224 |
+
inputs = None,
|
| 225 |
+
outputs = [rotate_pitch, rotate_yaw, rotate_roll, blink, eyebrow, wink, pupil_x, pupil_y, aaa, eee, woo, smile],
|
| 226 |
+
queue = False
|
| 227 |
+
).then(
|
| 228 |
+
fn=predict,
|
| 229 |
+
inputs=inputs,
|
| 230 |
+
outputs=outputs,
|
| 231 |
+
show_api=False
|
| 232 |
+
)
|
| 233 |
+
|
| 234 |
+
submit_btn.click(
|
| 235 |
+
fn=predict,
|
| 236 |
+
inputs=inputs,
|
| 237 |
+
outputs=outputs,
|
| 238 |
+
show_api=False
|
| 239 |
+
)
|
| 240 |
+
|
| 241 |
+
rotate_pitch.release(fn=predict, inputs=inputs, outputs=outputs, show_progress="minimal", show_api=False)
|
| 242 |
+
rotate_yaw.release(fn=predict, inputs=inputs, outputs=outputs, show_progress="minimal", show_api=False)
|
| 243 |
+
rotate_roll.release(fn=predict, inputs=inputs, outputs=outputs, show_progress="minimal", show_api=False)
|
| 244 |
+
blink.release(fn=predict, inputs=inputs, outputs=outputs, show_progress="minimal", show_api=False)
|
| 245 |
+
eyebrow.release(fn=predict, inputs=inputs, outputs=outputs, show_progress="minimal", show_api=False)
|
| 246 |
+
wink.release(fn=predict, inputs=inputs, outputs=outputs, show_progress="minimal", show_api=False)
|
| 247 |
+
pupil_x.release(fn=predict, inputs=inputs, outputs=outputs, show_progress="minimal", show_api=False)
|
| 248 |
+
pupil_y.release(fn=predict, inputs=inputs, outputs=outputs, show_progress="minimal", show_api=False)
|
| 249 |
+
aaa.release(fn=predict, inputs=inputs, outputs=outputs, show_progress="minimal", show_api=False)
|
| 250 |
+
eee.release(fn=predict, inputs=inputs, outputs=outputs, show_progress="minimal", show_api=False)
|
| 251 |
+
woo.release(fn=predict, inputs=inputs, outputs=outputs, show_progress="minimal", show_api=False)
|
| 252 |
+
smile.release(fn=predict, inputs=inputs, outputs=outputs, show_progress="minimal", show_api=False)
|
| 253 |
+
|
| 254 |
+
demo.queue(api_open=False).launch(share=False, show_error=True, show_api=False)
|