Upload qwen-image.py
Browse files- qwen-image.py +48 -0
qwen-image.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from modelscope import DiffusionPipeline
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
model_name = "/app/model/Qwen-Image-bf16"
|
| 5 |
+
|
| 6 |
+
# Load the pipeline
|
| 7 |
+
if torch.cuda.is_available():
|
| 8 |
+
torch_dtype = torch.float8_e4m3fn
|
| 9 |
+
device = "cuda"
|
| 10 |
+
else:
|
| 11 |
+
torch_dtype = torch.float32
|
| 12 |
+
device = "cpu"
|
| 13 |
+
|
| 14 |
+
pipe = DiffusionPipeline.from_pretrained(model_name)
|
| 15 |
+
pipe = pipe.to(device)
|
| 16 |
+
|
| 17 |
+
pipe.enable_model_cpu_offload()
|
| 18 |
+
|
| 19 |
+
positive_magic = "Ultra HD, 4K, cinematic composition." # for english prompt
|
| 20 |
+
# positive_magic = "超清,4K,电影级构图" # for chinese prompt
|
| 21 |
+
|
| 22 |
+
# Generate image
|
| 23 |
+
prompt = '''A coffee shop entrance features a chalkboard sign reading "Qwen Coffee 😊 $2 per cup," with a neon light beside it displaying "通义千问". Next to it hangs a poster showing a beautiful Chinese woman, and beneath the poster is written "π≈3.1415926-53589793-23846264-33832795-02384197". Ultra HD, 4K, cinematic composition'''
|
| 24 |
+
|
| 25 |
+
negative_prompt = " "
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# Generate with different aspect ratios
|
| 29 |
+
aspect_ratios = {
|
| 30 |
+
"1:1": (1328, 1328),
|
| 31 |
+
"16:9": (1664, 928),
|
| 32 |
+
"9:16": (928, 1664),
|
| 33 |
+
"4:3": (1472, 1140),
|
| 34 |
+
"3:4": (1140, 1472)
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
width, height = aspect_ratios["16:9"]
|
| 38 |
+
|
| 39 |
+
image = pipe(
|
| 40 |
+
prompt=prompt + positive_magic,
|
| 41 |
+
width=512,
|
| 42 |
+
height=512,
|
| 43 |
+
num_inference_steps=50,
|
| 44 |
+
true_cfg_scale=4.0,
|
| 45 |
+
generator=torch.Generator(device="cuda").manual_seed(42)
|
| 46 |
+
).images[0]
|
| 47 |
+
|
| 48 |
+
image.save("example.png")
|