Spaces:
Paused
Paused
| 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 == "ittpixio" | |
| css = ''' | |
| body { | |
| font-family: 'Roboto', sans-serif; | |
| background-color: #f0f0f0; | |
| color: #333; | |
| } | |
| .container { | |
| background-color: white; | |
| border-radius: 10px; | |
| box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); | |
| padding: 20px; | |
| margin-bottom: 20px; | |
| } | |
| .gradio-slider input[type="range"] { | |
| accent-color: #6200ea; | |
| } | |
| .gradio-button { | |
| background-color: #6200ea; | |
| color: white; | |
| border: none; | |
| padding: 10px 20px; | |
| border-radius: 5px; | |
| cursor: pointer; | |
| transition: background-color 0.3s; | |
| } | |
| .gradio-button:hover { | |
| background-color: #3700b3; | |
| } | |
| .result-image { | |
| width: 100%; | |
| max-width: 512px; | |
| margin: 0 auto; | |
| display: block; | |
| } | |
| @media (max-width: 768px) { | |
| .container { | |
| padding: 10px; | |
| } | |
| } | |
| ''' | |
| with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("# 🎭 Private FaceCraft Alpha") | |
| with gr.Column(): | |
| password = gr.Textbox(type="password", label="Enter password", placeholder="muhahaha") | |
| login_button = gr.Button("Login", variant="primary") | |
| with gr.Column(visible=False) as main_interface: | |
| gr.Markdown("## 🎨 FaceCraft Alpha") | |
| with gr.Row(): | |
| with gr.Column(scale=1, elem_classes="container"): | |
| image = gr.Image( | |
| label="Input image", | |
| type="filepath", | |
| height=256 | |
| ) | |
| 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) | |
| with gr.Row(): | |
| rotate_roll = gr.Slider(label="Rotate Left-Right tilt", value=0, minimum=-20, maximum=20) | |
| blink = gr.Slider(label="Blink", value=0, minimum=-20, maximum=5) | |
| with gr.Row(): | |
| 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) | |
| with gr.Row(): | |
| 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_classes="result-image", label="Generated Image") | |
| gr.HTML(""" | |
| <div style="text-align: center; margin-top: 20px;"> | |
| <a href="https://pixio.myapps.ai" target="_blank"> | |
| <button style="background-color: #4CAF50; border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; border-radius: 12px;"> | |
| Go to Pixio | |
| </button> | |
| </a> | |
| <p>Click the button above to visit Pixio</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) |