multimodalart HF staff commited on
Commit
25345fc
1 Parent(s): 091f1bb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +177 -0
app.py ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from huggingface_hub import snapshot_download
3
+ import spaces
4
+ os.makedirs("/home/user/app/ckpts", exist_ok=True)
5
+ snapshot_download(repo_id="Tencent-Hunyuan/HunyuanDiT", local_dir="/home/user/app/ckpts")
6
+
7
+ import gradio as gr
8
+ import pandas as pd
9
+ from pathlib import Path
10
+ from PIL import Image
11
+ import sys
12
+ #sys.path.insert(0, str(Path(__file__).parent.parent))
13
+
14
+ from hydit.constants import SAMPLER_FACTORY
15
+ from sample_t2i import inferencer
16
+
17
+ ROOT = Path(__file__).parent.parent
18
+ SAMPLERS = list(SAMPLER_FACTORY.keys())
19
+ SIZES = {
20
+ "square": (1024, 1024),
21
+ "landscape": (768, 1280),
22
+ "portrait": (1280, 768),
23
+ }
24
+
25
+ def get_strings(lang):
26
+ lang_file = Path(f"app/lang/{lang}.csv")
27
+ strings = pd.read_csv(lang_file, header=0)
28
+ strings = strings.set_index("key")['value'].to_dict()
29
+ return strings
30
+
31
+
32
+ args, gen, enhancer = inferencer()
33
+ strings = get_strings("en")
34
+
35
+ @spaces.GPU
36
+ def infer(
37
+ prompt,
38
+ negative_prompt,
39
+ seed,
40
+ cfg_scale,
41
+ infer_steps,
42
+ oriW, oriH,
43
+ sampler,
44
+ size,
45
+ enhance,
46
+ progress=gr.Progress(track_tqdm=True)
47
+ ):
48
+ if enhance and enhancer is not None:
49
+ success, enhanced_prompt = enhancer(prompt)
50
+ if not success:
51
+ fail_image = Image.open(ROOT / 'app/fail.png')
52
+ return fail_image
53
+ else:
54
+ enhanced_prompt = None
55
+
56
+ height, width = SIZES[size]
57
+ results = gen.predict(prompt,
58
+ height=height,
59
+ width=width,
60
+ seed=seed,
61
+ enhanced_prompt=enhanced_prompt,
62
+ negative_prompt=negative_prompt,
63
+ infer_steps=infer_steps,
64
+ guidance_scale=cfg_scale,
65
+ batch_size=1,
66
+ src_size_cond=(oriW, oriH),
67
+ sampler=sampler,
68
+ )
69
+ image = results['images'][0]
70
+ return image
71
+
72
+
73
+ def ui():
74
+ block = gr.Blocks()
75
+
76
+ description = f"""
77
+ # {strings['title']}
78
+
79
+ ## {strings['desc']}
80
+
81
+ """
82
+
83
+ with block:
84
+ with gr.Row():
85
+ gr.Markdown(description)
86
+ with gr.Row():
87
+ with gr.Column():
88
+ with gr.Row():
89
+ size = gr.Radio(
90
+ label=strings['size'], choices=[
91
+ (strings['square'], 'square'),
92
+ (strings['landscape'], 'landscape'),
93
+ (strings['portrait'], 'portrait'),
94
+ ],
95
+ value="square"
96
+ )
97
+ prompt = gr.Textbox(label=strings['prompt'], value=strings['default prompt'], lines=3)
98
+ with gr.Row():
99
+ infer_steps = gr.Slider(
100
+ label=strings['infer steps'], minimum=1, maximum=200, value=50, step=1,
101
+ )
102
+ seed = gr.Number(
103
+ label=strings['seed'], minimum=-1, maximum=1_000_000_000, value=1, step=1, precision=0,
104
+ )
105
+ enhance = gr.Checkbox(
106
+ label=strings['enhance'], value=enhancer is not None, interactive=True,
107
+ )
108
+
109
+ with gr.Accordion(
110
+ strings['accordion'], open=False
111
+ ):
112
+ with gr.Row():
113
+ negative_prompt = gr.Textbox(label=strings['negative_prompt'],
114
+ value=gen.default_negative_prompt,
115
+ lines=2,
116
+ )
117
+ with gr.Row():
118
+ sampler = gr.Dropdown(SAMPLERS, label=strings['sampler'], value="ddpm")
119
+ cfg_scale = gr.Slider(
120
+ label=strings['cfg'], minimum=1.0, maximum=16.0, value=6.0, step=1
121
+ )
122
+ oriW = gr.Number(
123
+ label=strings['width cond'], minimum=1024, maximum=4096, value=1024, step=64, precision=0,
124
+ min_width=80,
125
+ )
126
+ oriH = gr.Number(
127
+ label=strings['height cond'], minimum=1024, maximum=4096, value=1024, step=64, precision=0,
128
+ min_width=80,
129
+ )
130
+ with gr.Row():
131
+ advanced_button = gr.Button(strings['run'])
132
+ with gr.Column():
133
+ #default_img = Image.open(ROOT / 'app/default.png')
134
+ output_img = gr.Image(
135
+ label=strings['generated image'],
136
+ interactive=False,
137
+ format='png',
138
+ #value=default_img,
139
+ )
140
+ advanced_button.click(
141
+ fn=infer,
142
+ inputs=[
143
+ prompt, negative_prompt, seed, cfg_scale, infer_steps,
144
+ oriW, oriH, sampler, size, enhance,
145
+ ],
146
+ outputs=output_img,
147
+ )
148
+
149
+ with gr.Row():
150
+ gr.Examples([
151
+ ['一只小猫'],
152
+ ['现实主义风格,画面主要描述一个巴洛克风格的花瓶,带有金色的装饰边框,花瓶上盛开着各种色彩鲜艳的花,白色背景'],
153
+ ['一只聪明的狐狸走在阔叶树林里, 旁边是一条小溪, 细节真实, 摄影'],
154
+ ['飞流直下三千尺,疑是银河落九天'],
155
+ ['一只长靴猫手持亮银色的宝剑,身着铠甲,眼神坚毅,站在一堆金币上,背景是暗色调的洞穴,图像上有金币的光影点缀。'],
156
+ ['麻婆豆腐'],
157
+ ['苏州园林'],
158
+ ['一颗新鲜的草莓特写,红色的外表,表面布满许多种子,背景是淡绿色的叶子'],
159
+ ['请画出“忽如一夜春风来 千树万树梨花开”'],
160
+ ['请将“杞人忧天”的样子画出来'],
161
+ ['枯藤老树昏鸦,小桥流水人家'],
162
+ ['湖水清澈,天空湛蓝,阳光灿烂。一只优雅的白天鹅在湖边游泳。它周围有几只小鸭子,看起来非常可爱,整个画面给人一种宁静祥和的感觉。'],
163
+ ['一朵鲜艳的红色玫瑰花,花瓣撒有一些水珠,晶莹剔透,特写镜头'],
164
+ ['臭豆腐'],
165
+ ['九寨沟'],
166
+ ['俗语“鲤鱼跃龙门”'],
167
+ ['风格是写实,画面主要描述一个亚洲戏曲艺术家正在表演,她穿着华丽的戏服,脸上戴着精致的面具,身姿优雅,背景是古色古香的舞台,镜头是近景'],
168
+ ],
169
+ [prompt],
170
+ label=strings['examples']
171
+ )
172
+ return block
173
+
174
+
175
+ if __name__ == "__main__":
176
+ interface = ui()
177
+ interface.launch()