File size: 2,739 Bytes
5691542
 
 
 
 
 
 
b13b851
9f40a8f
6ede5ff
5691542
 
b13b851
6ede5ff
 
5691542
 
b13b851
 
6ede5ff
 
 
b13b851
 
 
6ede5ff
5691542
 
fefcd29
 
6ede5ff
fefcd29
 
 
6ede5ff
fefcd29
 
6ede5ff
 
 
 
 
 
 
 
 
fefcd29
6ede5ff
fefcd29
6ede5ff
fefcd29
6ede5ff
 
 
 
 
 
 
 
 
 
 
fefcd29
 
 
 
6ede5ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python

from __future__ import annotations

import gradio as gr
import numpy as np

from model import Model

DESCRIPTION = "# [StyleGAN3](https://github.com/NVlabs/stylegan3)"


def get_sample_image_url(name: str) -> str:
    sample_image_dir = "https://huggingface.co./spaces/hysts/StyleGAN3/resolve/main/samples"
    return f"{sample_image_dir}/{name}.jpg"


def get_sample_image_markdown(name: str) -> str:
    url = get_sample_image_url(name)
    size = 512 if name == "afhqv2" else 1024
    seed = "0-99"
    return f"""
    - size: {size}x{size}
    - seed: {seed}
    - truncation: 0.7
    ![sample images]({url})"""


model = Model()

with gr.Blocks(css="style.css") as demo:
    gr.Markdown(DESCRIPTION)

    with gr.Tabs():
        with gr.TabItem("App"):
            with gr.Row():
                with gr.Column():
                    model_name = gr.Dropdown(
                        label="Model", choices=list(model.MODEL_NAME_DICT.keys()), value="FFHQ-1024-R"
                    )
                    seed = gr.Slider(label="Seed", minimum=0, maximum=np.iinfo(np.uint32).max, step=1, value=0)
                    psi = gr.Slider(label="Truncation psi", minimum=0, maximum=2, step=0.05, value=0.7)
                    tx = gr.Slider(label="Translate X", minimum=-1, maximum=1, step=0.05, value=0)
                    ty = gr.Slider(label="Translate Y", minimum=-1, maximum=1, step=0.05, value=0)
                    angle = gr.Slider(label="Angle", minimum=-180, maximum=180, step=5, value=0)
                    run_button = gr.Button()
                with gr.Column():
                    result = gr.Image(label="Result", elem_id="result")

        with gr.TabItem("Sample Images"):
            with gr.Row():
                model_name2 = gr.Dropdown(
                    [
                        "afhqv2",
                        "ffhq",
                        "ffhq-u",
                        "metfaces",
                        "metfaces-u",
                    ],
                    value="afhqv2",
                    label="Model",
                )
            with gr.Row():
                text = get_sample_image_markdown(model_name2.value)
                sample_images = gr.Markdown(text)

    run_button.click(
        fn=model.set_model_and_generate_image,
        inputs=[
            model_name,
            seed,
            psi,
            tx,
            ty,
            angle,
        ],
        outputs=result,
        api_name="run",
    )
    model_name2.change(
        fn=get_sample_image_markdown,
        inputs=model_name2,
        outputs=sample_images,
        queue=False,
        api_name=False,
    )

if __name__ == "__main__":
    demo.queue(max_size=20).launch()