dangthr commited on
Commit
7359615
·
verified ·
1 Parent(s): eb052f6

Create app2.py

Browse files
Files changed (1) hide show
  1. app2.py +60 -0
app2.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import os
3
+ import torch
4
+ from diffusers import DiffusionPipeline
5
+
6
+ def main():
7
+ parser = argparse.ArgumentParser()
8
+ parser.add_argument("--prompt", type=str, required=True, help="输入提示词")
9
+ parser.add_argument("--seed", type=int, default=42, help="随机种子 (0 表示随机)")
10
+ parser.add_argument("--width", type=int, default=1664, help="图像宽度")
11
+ parser.add_argument("--height", type=int, default=928, help="图像高度")
12
+ args = parser.parse_args()
13
+
14
+ model_name = "Qwen/Qwen-Image"
15
+
16
+ # Load the pipeline
17
+ if torch.cuda.is_available():
18
+ torch_dtype = torch.bfloat16
19
+ device = "cuda"
20
+ else:
21
+ torch_dtype = torch.float32
22
+ device = "cpu"
23
+
24
+ pipe = DiffusionPipeline.from_pretrained(model_name, torch_dtype=torch_dtype)
25
+ pipe = pipe.to(device)
26
+
27
+ positive_magic = {
28
+ "en": ", Ultra HD, 4K, cinematic composition.", # for english prompt
29
+ "zh": ", 超清,4K,电影级构图." # for chinese prompt
30
+ }
31
+
32
+ negative_prompt = " " # Recommended if you don't use a negative prompt.
33
+
34
+ # 生成器(0 = 随机种子,不固定)
35
+ generator = None if args.seed == 0 else torch.Generator(device=device).manual_seed(args.seed)
36
+
37
+ # Generate image
38
+ image = pipe(
39
+ prompt=args.prompt + positive_magic["en"],
40
+ negative_prompt=negative_prompt,
41
+ width=args.width,
42
+ height=args.height,
43
+ num_inference_steps=50,
44
+ true_cfg_scale=4.0,
45
+ generator=generator
46
+ ).images[0]
47
+
48
+ # 输出目录
49
+ output_dir = os.path.abspath("output")
50
+ os.makedirs(output_dir, exist_ok=True)
51
+
52
+ # 固定文件名
53
+ output_path = os.path.join(output_dir, "output.png")
54
+ image.save(output_path)
55
+
56
+ seed_info = "random" if args.seed == 0 else args.seed
57
+ print(f"✅ Image saved at: {output_path} (seed={seed_info}, size={args.width}x{args.height})")
58
+
59
+ if __name__ == "__main__":
60
+ main()