import gradio as gr from PIL import Image import requests import time from pathlib import Path import json import io def preprocess_image(image_path): if not Path(image_path).exists(): raise FileNotFoundError(f"No such file: '{image_path}'") valid_extensions = ['jpg', 'jpeg', 'png', 'webp'] file_extension = image_path.split('.')[-1].lower() if file_extension not in valid_extensions: raise ValueError("Invalid file type. Only JPG, PNG, and WEBP are allowed.") with Image.open(image_path) as img: width, height = img.size if width > 1024 or height > 1024: if width > height: new_width = 1024 new_height = int((new_width / width) * height) else: new_height = 1024 new_width = int((new_height / height) * width) img_resized = img.resize((new_width, new_height), Image.LANCZOS) print(f"Resized image to {new_width}x{new_height}.") return img_resized else: print("Image size is within the limit, no resizing needed.") return image_path def display_uploaded_image(image_in): return image_in def reset_parameters(): 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) 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'] def predict( image, rotate_pitch, rotate_yaw, rotate_roll, blink, eyebrow, wink, pupil_x, pupil_y, aaa, eee, woo, smile, src_ratio, sample_ratio, crop_factor ): comfyui_url = "http://127.0.0.1:8188" workflow_path = Path("workflow_api.json") if not workflow_path.exists(): raise gr.Error("Workflow file missing.") with open(workflow_path, "r", encoding="utf-8") as file: workflow = json.load(file) image_path = image if not image_path: raise gr.Error("Please upload an input image.") uploaded_name = None prompt_id = None generated_image_paths = [] try: with open(image_path, "rb") as file: files = {"image": file} upload_response = requests.post(f"{comfyui_url}/upload/image", files=files) if upload_response.status_code != 200: raise gr.Error("Failed to upload image to ComfyUI.") uploaded_name = upload_response.json()["name"] workflow["15"]["inputs"]["image"] = uploaded_name workflow["14"]["inputs"]["rotate_pitch"] = rotate_pitch workflow["14"]["inputs"]["rotate_yaw"] = rotate_yaw workflow["14"]["inputs"]["rotate_roll"] = rotate_roll workflow["14"]["inputs"]["blink"] = blink workflow["14"]["inputs"]["eyebrow"] = eyebrow workflow["14"]["inputs"]["wink"] = wink workflow["14"]["inputs"]["pupil_x"] = pupil_x workflow["14"]["inputs"]["pupil_y"] = pupil_y workflow["14"]["inputs"]["aaa"] = aaa workflow["14"]["inputs"]["eee"] = eee workflow["14"]["inputs"]["woo"] = woo workflow["14"]["inputs"]["smile"] = smile workflow["14"]["inputs"]["src_ratio"] = src_ratio workflow["14"]["inputs"]["sample_ratio"] = sample_ratio workflow["14"]["inputs"]["crop_factor"] = crop_factor payload = {"prompt": workflow} prompt_response = requests.post(f"{comfyui_url}/prompt", json=payload) if prompt_response.status_code != 200: raise gr.Error("Failed to execute ComfyUI workflow.") prompt_id = prompt_response.json()["prompt_id"] start_time = time.time() while True: if time.time() - start_time > 120: raise gr.Error("Inference timed out.") history_response = requests.get(f"{comfyui_url}/history/{prompt_id}") if history_response.status_code == 200: history = history_response.json() if prompt_id in history: outputs = history[prompt_id]["outputs"] if "16" in outputs and "images" in outputs["16"]: image_info = outputs["16"]["images"][0] filename = image_info["filename"] subfolder = image_info["subfolder"] folder_type = image_info["type"] comfy_temp_path = Path("temp") / subfolder / filename if subfolder else Path("temp") / filename generated_image_paths.append(comfy_temp_path) image_url = f"{comfyui_url}/view?filename={filename}&subfolder={subfolder}&type={folder_type}" image_response = requests.get(image_url) if image_response.status_code == 200: image_data = image_response.content return Image.open(io.BytesIO(image_data)) time.sleep(1) finally: if uploaded_name: comfy_input_path = Path("input") / uploaded_name if comfy_input_path.exists(): comfy_input_path.unlink() for temp_path in generated_image_paths: if temp_path.exists(): temp_path.unlink() if prompt_id: requests.post(f"{comfyui_url}/history", json={"delete": [prompt_id]}) with gr.Blocks(title="Expression Editor") as demo: with gr.Column(elem_id="col-container"): gr.Markdown("# Expression Editor") with gr.Row(): with gr.Column(): image = gr.Image( label="Input image", sources=["upload"], type="filepath" ) with gr.Tab("HEAD"): with gr.Column(): 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.Tab("EYES"): with gr.Column(): eyebrow = gr.Slider( label="Eyebrow", value=0, minimum=-10, maximum=15 ) with gr.Row(): blink = gr.Slider( label="Blink", value=0, minimum=-20, maximum=5 ) 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.Tab("MOUTH"): with gr.Column(): 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.Tab("More Settings"): with gr.Column(): 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 ) with gr.Row(): reset_btn = gr.Button("Reset") submit_btn = gr.Button("Submit") with gr.Column(): result_image = gr.Image(elem_id="top") 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] outputs = [result_image] image.upload( fn = preprocess_image, inputs = [image], outputs = [image], queue = False, api_name="preprocess_image" ) reset_btn.click( fn = reset_parameters, inputs = None, outputs = [rotate_pitch, rotate_yaw, rotate_roll, blink, eyebrow, wink, pupil_x, pupil_y, aaa, eee, woo, smile], queue = False, api_name=False ).then( fn=predict, inputs=inputs, outputs=outputs, api_name=False ) submit_btn.click( fn=predict, inputs=inputs, outputs=outputs, api_name="predict" ) rotate_pitch.release(fn=predict, inputs=inputs, outputs=outputs, api_name=False) rotate_yaw.release(fn=predict, inputs=inputs, outputs=outputs, api_name=False) rotate_roll.release(fn=predict, inputs=inputs, outputs=outputs, api_name=False) blink.release(fn=predict, inputs=inputs, outputs=outputs, api_name=False) eyebrow.release(fn=predict, inputs=inputs, outputs=outputs, api_name=False) wink.release(fn=predict, inputs=inputs, outputs=outputs, api_name=False) pupil_x.release(fn=predict, inputs=inputs, outputs=outputs, api_name=False) pupil_y.release(fn=predict, inputs=inputs, outputs=outputs, api_name=False) aaa.release(fn=predict, inputs=inputs, outputs=outputs, api_name=False) eee.release(fn=predict, inputs=inputs, outputs=outputs, api_name=False) woo.release(fn=predict, inputs=inputs, outputs=outputs, api_name=False) smile.release(fn=predict, inputs=inputs, outputs=outputs, api_name=False) demo.queue(default_concurrency_limit=1, max_size=20).launch(server_name="0.0.0.0", server_port=7860)