davidvgilmore commited on
Commit
518d7fb
·
verified ·
1 Parent(s): e9916d2

Upload hy3dgen/text2image.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. hy3dgen/text2image.py +92 -0
hy3dgen/text2image.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Open Source Model Licensed under the Apache License Version 2.0
2
+ # and Other Licenses of the Third-Party Components therein:
3
+ # The below Model in this distribution may have been modified by THL A29 Limited
4
+ # ("Tencent Modifications"). All Tencent Modifications are Copyright (C) 2024 THL A29 Limited.
5
+
6
+ # Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
7
+ # The below software and/or models in this distribution may have been
8
+ # modified by THL A29 Limited ("Tencent Modifications").
9
+ # All Tencent Modifications are Copyright (C) THL A29 Limited.
10
+
11
+ # Hunyuan 3D is licensed under the TENCENT HUNYUAN NON-COMMERCIAL LICENSE AGREEMENT
12
+ # except for the third-party components listed below.
13
+ # Hunyuan 3D does not impose any additional limitations beyond what is outlined
14
+ # in the repsective licenses of these third-party components.
15
+ # Users must comply with all terms and conditions of original licenses of these third-party
16
+ # components and must ensure that the usage of the third party components adheres to
17
+ # all relevant laws and regulations.
18
+
19
+ # For avoidance of doubts, Hunyuan 3D means the large language models and
20
+ # their software and algorithms, including trained model weights, parameters (including
21
+ # optimizer states), machine-learning model code, inference-enabling code, training-enabling code,
22
+ # fine-tuning enabling code and other elements of the foregoing made publicly available
23
+ # by Tencent in accordance with TENCENT HUNYUAN COMMUNITY LICENSE AGREEMENT.
24
+
25
+
26
+ import os
27
+ import random
28
+
29
+ import numpy as np
30
+ import torch
31
+ from diffusers import AutoPipelineForText2Image
32
+
33
+
34
+ def seed_everything(seed):
35
+ random.seed(seed)
36
+ np.random.seed(seed)
37
+ torch.manual_seed(seed)
38
+ os.environ["PL_GLOBAL_SEED"] = str(seed)
39
+
40
+
41
+ class HunyuanDiTPipeline:
42
+ def __init__(
43
+ self,
44
+ model_path="Tencent-Hunyuan/HunyuanDiT-v1.1-Diffusers-Distilled",
45
+ device='cuda'
46
+ ):
47
+ self.device = device
48
+ self.pipe = AutoPipelineForText2Image.from_pretrained(
49
+ model_path,
50
+ torch_dtype=torch.float16,
51
+ enable_pag=True,
52
+ pag_applied_layers=["blocks.(16|17|18|19)"]
53
+ ).to(device)
54
+ self.pos_txt = ",白色背景,3D风格,最佳质量"
55
+ self.neg_txt = "文本,特写,裁剪,出框,最差质量,低质量,JPEG伪影,PGLY,重复,病态," \
56
+ "残缺,多余的手指,变异的手,画得不好的手,画得不好的脸,变异,畸形,模糊,脱水,糟糕的解剖学," \
57
+ "糟糕的比例,多余的肢体,克隆的脸,毁容,恶心的比例,畸形的肢体,缺失的手臂,缺失的腿," \
58
+ "额外的手臂,额外的腿,融合的手指,手指太多,长脖子"
59
+
60
+ def compile(self):
61
+ # accelarate hunyuan-dit transformer,first inference will cost long time
62
+ torch.set_float32_matmul_precision('high')
63
+ self.pipe.transformer = torch.compile(self.pipe.transformer, fullgraph=True)
64
+ # self.pipe.vae.decode = torch.compile(self.pipe.vae.decode, fullgraph=True)
65
+ generator = torch.Generator(device=self.pipe.device) # infer once for hot-start
66
+ out_img = self.pipe(
67
+ prompt='美少女战士',
68
+ negative_prompt='模糊',
69
+ num_inference_steps=25,
70
+ pag_scale=1.3,
71
+ width=1024,
72
+ height=1024,
73
+ generator=generator,
74
+ return_dict=False
75
+ )[0][0]
76
+
77
+ @torch.no_grad()
78
+ def __call__(self, prompt, seed=0):
79
+ seed_everything(seed)
80
+ generator = torch.Generator(device=self.pipe.device)
81
+ generator = generator.manual_seed(int(seed))
82
+ out_img = self.pipe(
83
+ prompt=self.pos_txt+prompt,
84
+ negative_prompt=self.neg_txt,
85
+ num_inference_steps=20,
86
+ pag_scale=1.3,
87
+ width=1024,
88
+ height=1024,
89
+ generator=generator,
90
+ return_dict=False
91
+ )[0][0]
92
+ return out_img