File size: 8,245 Bytes
d3f0ae3
 
da03eea
d3f0ae3
562f182
d3f0ae3
 
 
 
4d24ceb
 
 
 
da03eea
d3f0ae3
beac1fd
 
da03eea
3db47b6
 
b38dddf
3db47b6
135c512
da03eea
d3f0ae3
da03eea
4d24ceb
42b0ed5
 
 
 
9094683
cd67639
 
 
42b0ed5
94e7def
da03eea
 
67d4943
da03eea
9127eae
da03eea
 
e8221d2
da03eea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bbb970
 
 
 
 
da03eea
efab3cc
 
9d92216
efab3cc
0559bb0
efab3cc
 
f0bbe4b
efab3cc
 
 
 
 
 
d082263
ae53881
f0bbe4b
 
ae53881
 
 
 
 
 
 
 
 
 
 
 
96c03e9
ae53881
 
 
 
f0bbe4b
 
ae53881
f0bbe4b
ae53881
 
 
 
f0bbe4b
ae53881
 
f0bbe4b
 
 
 
 
ae53881
 
 
 
 
 
 
 
 
 
 
96c03e9
ae53881
 
 
 
f0bbe4b
 
ae53881
f0bbe4b
ae53881
 
 
 
da03eea
 
 
 
 
 
 
 
 
ae53881
da03eea
ae53881
da03eea
 
 
 
 
 
 
 
ae53881
da03eea
 
 
 
 
 
 
96c03e9
da03eea
d082263
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3f0ae3
da03eea
 
 
 
 
 
0559bb0
da03eea
4d16190
ae53881
da03eea
 
24c45c9
da03eea
a51642e
da03eea
4d16190
4d24ceb
343d878
ae53881
 
 
4d24ceb
ae53881
9647f84
ae53881
 
 
9647f84
ae53881
 
 
 
4d24ceb
ae53881
 
 
 
 
 
9647f84
ae53881
 
da03eea
ae53881
4d24ceb
da03eea
5080ca8
da03eea
ae53881
 
da03eea
ae53881
da03eea
ea6c06b
da03eea
4d16190
d4c2ed0
 
 
5080ca8
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
253
254
255
256
257
258
259
260
261
# -*- 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("""
                <div align="center" style="margin-bottom: 20px;">
                    <img src='https://lh3.googleusercontent.com/d/1dmZMaOhZxcC_93Rc0ReYTB9oQEUuFJFu' width="160">
                    <p style="font-size: 16px; max-width: 960px; margin: 5px auto;">
                        Human Faces Generation with Diffusion Models.
                    </p>
                    <p style="font-size: 15px;">
                        📜 <a href="https://github.com/frankcholula/faice/blob/main/paper/human_faces_generation_with_diffusion_models.pdf" target="_blank">Report</a>
                        &nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;
                        💻 <a href="https://github.com/frankcholula/faice" target="_blank">Code</a>
                    </p>
                </div>
                """)

        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=["./"])