# -*- coding: UTF-8 -*- """ @Time : 28/05/2025 16:29 @Author : xiaoguangliang @File : app.py @Project : Faice_text2face """ import gradio as gr from inference_models.unconditional_diffusion_inference import inference_unconditional from inference_models.class_guidance_inference import inference_class_guidance, GENDER_CHOICES from inference_models.stable_diffusion_inference import inference_sd, MAX_SEED from inference_api import api_unconditional, api_class_guidance, api_sd from utils import timer MAX_IMAGE_SIZE = 1024 examples = [ "Portrait of a young woman with long wavy hair, soft studio lighting, high contrast, 4k resolution, professional headshot", "Close-up of a smiling man with sharp jawline, cinematic lighting, shallow depth of field, bokeh background", "Candid portrait, natural light, slight smile, outdoor background, wind-blown hair", "Retro 80s style portrait, neon colors, grainy texture, bold shadows, high contrast", "Black and white portrait of an elderly woman with wrinkles, deep shadows, textured background" ] css = """ body { background: linear-gradient(135deg, #f9e2e6 0%, #e8f3fc 50%, #e2f9f2 100%); min-height: 100vh; } .gradio-container { background-image: url('https://lh3.googleusercontent.com/d/1c4-K7_jQ4Yz_Jl_nqe2cf3IHC0OqmE5v'); background-repeat: no-repeat; background-attachment: fixed; background-position: center; background-size: cover; } #col-container { margin: 0 auto; max-width: 960px; background-color: rgba(255, 255, 255, 0.85); border-radius: 20px; box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1); padding: 24px; backdrop-filter: blur(10px); } .gr-button-primary { background: linear-gradient(90deg, #6b9dfc, #8c6bfc) !important; border: none !important; transition: all 0.3s ease; } .gr-button-primary:hover { transform: translateY(-2px); box-shadow: 0 5px 15px rgba(108, 99, 255, 0.3); } .gr-form { border-radius: 12px; background-color: rgba(255, 255, 255, 0.7); } .gr-accordion { border-radius: 12px; overflow: hidden; } h1 { background: linear-gradient(90deg, #6b9dfc, #8c6bfc); -webkit-background-clip: text; -webkit-text-fill-color: transparent; font-weight: 800; } """ theme = gr.themes.Ocean( primary_hue="fuchsia", ) with gr.Blocks(theme=theme, css=css) as demo: with gr.Column(elem_id="col-container"): gr.HTML("""
""") gr.Markdown("---") with gr.Row(): gr.Markdown("## Part 1. Unconditional Face Generation") run_button_1 = gr.Button("Run", scale=0, variant="primary", elem_classes="gr-button-primary") result_1 = gr.Image(label="Result", show_label=False) with gr.Accordion("Advanced Settings", open=False): seed_1 = gr.Slider( label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, ) randomize_seed_1 = gr.Checkbox(label="Randomize seed", value=True) with gr.Row(): num_inference_steps_1 = gr.Slider( label="Number of inference steps", minimum=20, maximum=1000, step=1, value=100, ) gr.Markdown("---") with gr.Row(): gr.Markdown("## Part 2. Class Guidance Face Generation") run_button_2 = gr.Button("Run", scale=0, variant="primary") gender_select_radio = gr.Radio( label="Select Gender", choices=GENDER_CHOICES, value=GENDER_CHOICES[0], ) result_2 = gr.Image(label="Result", show_label=False) with gr.Accordion("Advanced Settings", open=False): seed_2 = gr.Slider( label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, ) randomize_seed_2 = gr.Checkbox(label="Randomize seed", value=True) with gr.Row(): num_inference_steps_2 = gr.Slider( label="Number of inference steps", minimum=20, maximum=1000, step=1, value=100, ) gr.Markdown("---") gr.Markdown("## Part 3. Text-to-Face Generation") with gr.Row(): prompt = gr.Text( label="Prompt", show_label=False, max_lines=1, placeholder="Enter your prompt", container=False, ) run_button_3 = gr.Button("Run", scale=0, variant="primary") result_3 = gr.Image(label="Result", show_label=False) with gr.Accordion("Advanced Settings", open=False): negative_prompt = gr.Text( label="Negative prompt", max_lines=1, placeholder="Enter a negative prompt", ) seed_3 = gr.Slider( label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, ) randomize_seed_3 = gr.Checkbox(label="Randomize seed", value=True) # with gr.Row(): # width = gr.Slider( # label="Width", # minimum=512, # maximum=MAX_IMAGE_SIZE, # step=32, # value=1024, # ) # # height = gr.Slider( # label="Height", # minimum=512, # maximum=MAX_IMAGE_SIZE, # step=32, # value=1024, # ) with gr.Row(): guidance_scale = gr.Slider( label="Guidance scale", minimum=0.0, maximum=7.5, step=0.1, value=7.5, ) num_inference_steps_3 = gr.Slider( label="Number of inference steps", minimum=1, maximum=100, step=1, value=50, ) gr.Examples(examples=examples, inputs=[prompt], outputs=[result_3], fn=api_sd, cache_examples=True, cache_mode="lazy") gr.on( triggers=[run_button_1.click], fn=api_unconditional, inputs=[ seed_1, randomize_seed_1, num_inference_steps_1, ], outputs=[result_1], ) gr.on( triggers=[run_button_2.click], fn=api_class_guidance, inputs=[ gender_select_radio, seed_2, randomize_seed_2, num_inference_steps_2, ], outputs=[result_2], ) gr.on( triggers=[run_button_3.click, prompt.submit], fn=api_sd, inputs=[ prompt, negative_prompt, seed_3, randomize_seed_3, guidance_scale, num_inference_steps_3, ], outputs=[result_3], ) if __name__ == "__main__": with timer("All tasks"): # demo.launch(mcp_server=True) demo.launch(share=True, allowed_paths=["./"])