Spaces:
orrinin
/
Runtime error

File size: 6,231 Bytes
694468d
 
 
ef187eb
694468d
 
ef187eb
694468d
63b6eaf
2b0f02c
694468d
 
 
 
ec35e66
694468d
ec35e66
8b1e96d
 
694468d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a2b9b2
9b38787
694468d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0cffd40
8b3ca8d
694468d
 
 
 
 
 
8b3ca8d
 
694468d
 
 
 
 
 
 
 
 
8b1e96d
0cffd40
694468d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b3ca8d
 
 
694468d
 
fe16630
8b3ca8d
8b1e96d
694468d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import random

import gradio as gr
from gradio_client import Client, file
import numpy as np
from PIL import Image
from typing import Tuple
from translatepy import Translator

MODEL = os.environ.get("MODEL")
API_URL = "https://api-inference.huggingface.co/models/tianweiy/DMD2"
DESCRIPTION = """
# DMD2 文生图
"""
translator = Translator()



def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
    if randomize_seed:
        seed = random.randint(0, MAX_SEED)
    return seed

MAX_SEED = np.iinfo(np.int32).max

client = Client(MODEL)

style_list = [
    {
        "name": "(无风格)",
        "prompt": "{prompt}",
    },
    {
        "name": "电影",
        "prompt": "cinematic still {prompt} . emotional, harmonious, vignette, highly detailed, high budget, bokeh, cinemascope, moody, epic, gorgeous, film grain, grainy",
    },
    {
        "name": "摄影",
        "prompt": "cinematic photo {prompt} . 35mm photograph, film, bokeh, professional, 4k, highly detailed",
    },
    {
        "name": "动画",
        "prompt": "anime artwork {prompt} . anime style, key visual, vibrant, studio anime,  highly detailed",
    },
    {
        "name": "漫画",
        "prompt": "manga style {prompt} . vibrant, high-energy, detailed, iconic, Japanese comic style",
    },
    {
        "name": "数绘",
        "prompt": "concept art {prompt} . digital artwork, illustrative, painterly, matte painting, highly detailed",
    },
    {
        "name": "像素",
        "prompt": "pixel-art {prompt} . low-res, blocky, pixel art style, 8-bit graphics",
    },
    {
        "name": "幻想",
        "prompt": "ethereal fantasy concept art of  {prompt} . magnificent, celestial, ethereal, painterly, epic, majestic, magical, fantasy art, cover art, dreamy",
    },
    {
        "name": "朋克",
        "prompt": "neonpunk style {prompt} . cyberpunk, vaporwave, neon, vibes, vibrant, stunningly beautiful, crisp, detailed, sleek, ultramodern, magenta highlights, dark purple shadows, high contrast, cinematic, ultra detailed, intricate, professional",
    },
    {
        "name": "三维",
        "prompt": "professional 3d model {prompt} . octane render, highly detailed, volumetric, dramatic lighting",
    },
]   
styles = {k["name"]: (k["prompt"]) for k in style_list}
print(styles)
STYLE_NAMES = list(styles.keys())
DEFAULT_STYLE_NAME = "(无风格)"

def apply_style(style_name: str, positive: str) -> Tuple[str, str]:
    p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
    return p.replace("{prompt}", positive), n

def generate(
    prompt: str,
    seed: int = 0,
    width: int = 1024,
    height: int = 1024,
    style: str = DEFAULT_STYLE_NAME,
    num_images: int = 2,
    randomize_seed: bool = False,
    progress=gr.Progress(track_tqdm=True),
):
    prompt = str(translator.translate(prompt, 'English'))
    print(prompt)
    seed = int(randomize_seed_fn(seed, randomize_seed))
    # print(client.view_api())
    result = client.predict(
		prompt=prompt,
		seed=seed,
		height=height,
		width=width,
		num_images=num_images,
		fast_vae_decode=True,
		api_name="/inference"
    )
    images = result[0]
    print(images)
    image_paths = []
  #  List[Dict(image: filepath, caption: str | None)]
    for img in images:
        image_paths.append(img["image"])
    print(image_paths)
    return image_paths, seed

examples = [
    "镭射眼的秋田犬",
    "一只吃起司的猫",
    "太空中骑马的宇航员",
    "放学回家的学生们,动画风格",
    "一个可爱的机器人艺术家在画架上绘画,概念艺术",
    "一位女士的特写,她戴着透明、棱柱形、精致的复仇女神头饰,摆出应有的姿势,棕色肤色"
]

CSS = '''
.gradio-container{max-width: 560px !important}
h1{text-align:center}
footer {
    visibility: hidden
}
'''
with gr.Blocks(css=CSS, theme="soft") as demo:
    gr.Markdown(DESCRIPTION)
    with gr.Group():
        with gr.Row():
            prompt = gr.Text(
                label="描述",
                show_label=False,
                max_lines=1,
                placeholder="画什么好呢",
                container=False,
                scale=2,
            )
            run_button = gr.Button("生成", scale=1)
        result = gr.Gallery(label="作品", columns=1, preview=True)
    with gr.Accordion("高级选项", open=False):
        with gr.Row():
            num_images = gr.Slider(
                label="数量",
                minimum=1,
                maximum=5,
                step=1,
                value=2,
            )
            seed = gr.Slider(
                label="种子",
                minimum=0,
                maximum=MAX_SEED,
                step=1,
                value=0,
                visible=True
            )
            randomize_seed = gr.Checkbox(label="随机种子", value=True)
        with gr.Row(visible=True):
            width = gr.Slider(
                label="宽",
                minimum=512,
                maximum=2048,
                step=8,
                value=1024,
            )
            height = gr.Slider(
                label="高",
                minimum=512,
                maximum=2048,
                step=8,
                value=1024,
            )
    with gr.Row(visible=True):
            style_selection = gr.Radio(
                show_label=True,
                container=True,
                interactive=True,
                choices=STYLE_NAMES,
                value=DEFAULT_STYLE_NAME,
                label="风格化",
            )
        

    gr.Examples(
        examples=examples,
        inputs=prompt,
        outputs=[result, seed],
        fn=generate,
        cache_examples="lazy",
    )
    
    gr.on(
        triggers=[
            prompt.submit,
            run_button.click,
        ],
        fn=generate,
        inputs=[
            prompt,
            seed,
            width,
            height,
            style_selection,
            num_images,
            randomize_seed,
        ],
        outputs=[result, seed],
        api_name="run",
    )


        
if __name__ == "__main__":
    demo.queue(max_size=20).launch(show_api=False, debug=False)