Upload 8 files
Browse files- t2i/Fluently-XL-v2.py +47 -0
- t2i/SDXL.py +32 -0
- t2i/SDXL_Lighting.py +39 -0
- t2i/StableCascade.py +61 -0
- t2i/Taiyi-XL.py +20 -0
- t2i/client.py +26 -0
- t2i/server.py +26 -0
- vlm/IXC2-4KHD.py +44 -0
t2i/Fluently-XL-v2.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# https://huggingface.co/spaces/ehristoforu/dalle-3-xl-lora-v2
|
| 2 |
+
# pip install diffusers transformers accelerate safetensors
|
| 3 |
+
# HF_ENDPOINT=https://hf-mirror.com python Fluently-XL-v2.py
|
| 4 |
+
|
| 5 |
+
import torch
|
| 6 |
+
from diffusers import StableDiffusionXLPipeline, EulerAncestralDiscreteScheduler
|
| 7 |
+
|
| 8 |
+
model_id = "fluently/Fluently-XL-v2"
|
| 9 |
+
negative_prompt = "(deformed, distorted, disfigured:1.3), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers:1.4), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, (NSFW:1.25)"
|
| 10 |
+
width, height = 1024, 1024
|
| 11 |
+
guidance_scale = 6
|
| 12 |
+
|
| 13 |
+
def save_image(img):
|
| 14 |
+
import uuid
|
| 15 |
+
unique_name = str(uuid.uuid4()) + ".png"
|
| 16 |
+
img.save(unique_name)
|
| 17 |
+
return unique_name
|
| 18 |
+
|
| 19 |
+
def t2i(prompt):
|
| 20 |
+
|
| 21 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(model_id, torch_dtype=torch.float16, use_safetensors=True,)
|
| 22 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
| 23 |
+
|
| 24 |
+
## lora
|
| 25 |
+
# pipe.load_lora_weights("ehristoforu/dalle-3-xl-v2", weight_name="dalle-3-xl-lora-v2.safetensors", adapter_name="dalle")
|
| 26 |
+
# pipe.set_adapters("dalle")
|
| 27 |
+
|
| 28 |
+
pipe.to("cuda")
|
| 29 |
+
|
| 30 |
+
image = pipe(
|
| 31 |
+
prompt=prompt,
|
| 32 |
+
negative_prompt=negative_prompt,
|
| 33 |
+
width=width,
|
| 34 |
+
height=height,
|
| 35 |
+
guidance_scale=guidance_scale,
|
| 36 |
+
num_inference_steps=25,
|
| 37 |
+
num_images_per_prompt=1,
|
| 38 |
+
cross_attention_kwargs={"scale": 0.65},
|
| 39 |
+
output_type="pil",
|
| 40 |
+
).images[0]
|
| 41 |
+
|
| 42 |
+
return image
|
| 43 |
+
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
prompt = "a girl in beijing"
|
| 46 |
+
image = t2i(prompt)
|
| 47 |
+
image.save("fluently-xl-v2_output.png")
|
t2i/SDXL.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0
|
| 2 |
+
# pip install diffusers transformers accelerate safetensors
|
| 3 |
+
# HF_ENDPOINT=https://hf-mirror.com python SDXL.py
|
| 4 |
+
|
| 5 |
+
## download
|
| 6 |
+
# git clone https://hf-mirror.com/stabilityai/stable-diffusion-xl-base-1.0
|
| 7 |
+
# cd unet && wget -c https://hf-mirror.com/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/unet/diffusion_pytorch_model.fp16.safetensors
|
| 8 |
+
# cd vae && wget -c https://hf-mirror.com/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/vae/diffusion_pytorch_model.fp16.safetensors
|
| 9 |
+
# wget -c https://hf-mirror.com/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/sd_xl_base_1.0_0.9vae.safetensors
|
| 10 |
+
|
| 11 |
+
from diffusers import DiffusionPipeline
|
| 12 |
+
import torch
|
| 13 |
+
|
| 14 |
+
model_id = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 15 |
+
|
| 16 |
+
def t2i(prompt):
|
| 17 |
+
pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16, use_safetensors=True, variant="fp16")
|
| 18 |
+
pipe.to("cuda")
|
| 19 |
+
|
| 20 |
+
# 使用 torch >= 2.0 时,通过 torch.compile 可以将推理速度提高 20-30%。在运行管道之前,使用 torchcompile 简单地包装unet:
|
| 21 |
+
# pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
|
| 22 |
+
|
| 23 |
+
# if using torch < 2.0
|
| 24 |
+
# pipe.enable_xformers_memory_efficient_attention()
|
| 25 |
+
|
| 26 |
+
image = pipe(prompt=prompt).images[0]
|
| 27 |
+
return image
|
| 28 |
+
|
| 29 |
+
if __name__ == "__main__":
|
| 30 |
+
prompt = "a girl in beijing"
|
| 31 |
+
image = t2i(prompt)
|
| 32 |
+
image.save("sdxl_output.png")
|
t2i/SDXL_Lighting.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
## download
|
| 2 |
+
# git clone https://hf-mirror.com/stabilityai/stable-diffusion-xl-base-1.0
|
| 3 |
+
# wget -c hf-mirror.com/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/unet/diffusion_pytorch_model.fp16.safetensors
|
| 4 |
+
# wget -c https://hf-mirror.com/ByteDance/SDXL-Lightning/resolve/main/sdxl_lightning_4step_unet.safetensors
|
| 5 |
+
# base = xxx/stable-diffusion-xl-base-1.0
|
| 6 |
+
# ckpt = SDXL-Lightning/sdxl_lightning_4step_unet.safetensors
|
| 7 |
+
|
| 8 |
+
import torch
|
| 9 |
+
from diffusers import StableDiffusionXLPipeline, UNet2DConditionModel, EulerDiscreteScheduler
|
| 10 |
+
from huggingface_hub import hf_hub_download
|
| 11 |
+
from safetensors.torch import load_file
|
| 12 |
+
|
| 13 |
+
base = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 14 |
+
repo = "ByteDance/SDXL-Lightning"
|
| 15 |
+
ckpt = "sdxl_lightning_4step_unet.safetensors" # Use the correct ckpt for your step setting!
|
| 16 |
+
|
| 17 |
+
def t2i(prompt):
|
| 18 |
+
|
| 19 |
+
# Load model.
|
| 20 |
+
|
| 21 |
+
# unet = UNet2DConditionModel.from_config(base, subfolder="unet").to("cuda", torch.float16)
|
| 22 |
+
# unet.load_state_dict(load_file(hf_hub_download(repo, ckpt), device="cuda"))
|
| 23 |
+
# pipe = StableDiffusionXLPipeline.from_pretrained(base, unet=unet, torch_dtype=torch.float16, variant="fp16").to("cuda")
|
| 24 |
+
|
| 25 |
+
unet = UNet2DConditionModel.from_config(base, subfolder="unet").to("cuda", torch.float16)
|
| 26 |
+
unet.load_state_dict(load_file(ckpt), device="cuda"))
|
| 27 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(base, unet=unet, torch_dtype=torch.float16, variant="fp16").to("cuda")
|
| 28 |
+
|
| 29 |
+
# Ensure sampler uses "trailing" timesteps.
|
| 30 |
+
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing")
|
| 31 |
+
|
| 32 |
+
# Ensure using the same inference steps as the loaded model and CFG set to 0.
|
| 33 |
+
image = pipe(prompt, num_inference_steps=4, guidance_scale=0).images[0]
|
| 34 |
+
return image
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
prompt = "a girl in beijing"
|
| 38 |
+
image = t2i(prompt)
|
| 39 |
+
image.save("sdxl_lighting_output.png")
|
t2i/StableCascade.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# https://hf-mirror.com/stabilityai/stable-cascade
|
| 2 |
+
# https://hf-mirror.com/stabilityai/stable-cascade-prior
|
| 3 |
+
|
| 4 |
+
import torch
|
| 5 |
+
from diffusers import StableCascadeDecoderPipeline, StableCascadePriorPipeline, StableCascadeCombinedPipeline
|
| 6 |
+
|
| 7 |
+
cas = "stabilityai/stable-cascade"
|
| 8 |
+
cas_prior = "stabilityai/stable-cascade-prior"
|
| 9 |
+
|
| 10 |
+
def t2i_(prompt):
|
| 11 |
+
|
| 12 |
+
prior = StableCascadePriorPipeline.from_pretrained(cas_prior, variant="bf16", torch_dtype=torch.bfloat16)
|
| 13 |
+
decoder = StableCascadeDecoderPipeline.from_pretrained(cas, variant="bf16", torch_dtype=torch.float16)
|
| 14 |
+
|
| 15 |
+
prior.to("cuda")
|
| 16 |
+
decoder.to("cuda")
|
| 17 |
+
# prior.enable_model_cpu_offload()
|
| 18 |
+
# decoder.enable_model_cpu_offload()
|
| 19 |
+
|
| 20 |
+
prior_output = prior(
|
| 21 |
+
prompt=prompt,
|
| 22 |
+
height=1024,
|
| 23 |
+
width=1024,
|
| 24 |
+
negative_prompt="",
|
| 25 |
+
guidance_scale=4.0,
|
| 26 |
+
num_images_per_prompt=1,
|
| 27 |
+
num_inference_steps=20
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
image = decoder(
|
| 31 |
+
image_embeddings=prior_output.image_embeddings.to(torch.float16),
|
| 32 |
+
prompt=prompt,
|
| 33 |
+
negative_prompt="",
|
| 34 |
+
guidance_scale=0.0,
|
| 35 |
+
output_type="pil",
|
| 36 |
+
num_inference_steps=10
|
| 37 |
+
).images[0]
|
| 38 |
+
|
| 39 |
+
return image
|
| 40 |
+
|
| 41 |
+
def t2i(prompt):
|
| 42 |
+
pipe = StableCascadeCombinedPipeline.from_pretrained(cas, variant="bf16", torch_dtype=torch.bfloat16)
|
| 43 |
+
pipe.to("cuda")
|
| 44 |
+
|
| 45 |
+
image = pipe(
|
| 46 |
+
prompt=prompt,
|
| 47 |
+
negative_prompt="",
|
| 48 |
+
num_inference_steps=10,
|
| 49 |
+
prior_num_inference_steps=20,
|
| 50 |
+
prior_guidance_scale=3.0,
|
| 51 |
+
width=1024,
|
| 52 |
+
height=1024,
|
| 53 |
+
).images[0]
|
| 54 |
+
|
| 55 |
+
return image
|
| 56 |
+
|
| 57 |
+
if __name__ == "__main__":
|
| 58 |
+
prompt = "a girl in beijing"
|
| 59 |
+
image = t2i(prompt)
|
| 60 |
+
# image = t2i_(prompt)
|
| 61 |
+
image.save("stablecascade_output.png")
|
t2i/Taiyi-XL.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# git clone https://github.com/IDEA-CCNL/Taiyi-Diffusion-XL.git
|
| 2 |
+
# cd ./Taiyi-Diffusion-XL/
|
| 3 |
+
# pip install -r requirements.txt
|
| 4 |
+
# HF_ENDPOINT=https://hf-mirror.com python Taiyi-XL.py
|
| 5 |
+
|
| 6 |
+
from diffusers import DiffusionPipeline
|
| 7 |
+
import torch
|
| 8 |
+
|
| 9 |
+
model_id = "IDEA-CCNL/Taiyi-Stable-Diffusion-XL-3.5B"
|
| 10 |
+
|
| 11 |
+
def t2i(prompt):
|
| 12 |
+
pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16, use_safetensors=True, variant="fp16")
|
| 13 |
+
pipe.to("cuda")
|
| 14 |
+
image = pipe(prompt=prompt).images[0]
|
| 15 |
+
return image
|
| 16 |
+
|
| 17 |
+
if __name__ == "__main__":
|
| 18 |
+
prompt = "一个身穿汉服的美丽的猫女,有着黄色眼睛和黑色头发,简单妆容,眼罩和摩托车"
|
| 19 |
+
image = t2i(prompt)
|
| 20 |
+
image.save("taiyi_output.png")
|
t2i/client.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import argparse
|
| 3 |
+
from gradio_client import Client
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def main():
|
| 7 |
+
parser = argparse.ArgumentParser(description="x")
|
| 8 |
+
parser.add_argument('--model', '-m', type=str, default="red")
|
| 9 |
+
parser.add_argument('--prompt', '-p', type=str, default="a girl in beijing")
|
| 10 |
+
parser.add_argument('--api-url', type=str, default="http://127.0.0.1:7860/")
|
| 11 |
+
parser.add_argument('--api-name', type=str, default="/predict")
|
| 12 |
+
|
| 13 |
+
args = parser.parse_args()
|
| 14 |
+
client = Client(args.api_url)
|
| 15 |
+
output = client.predict(args.model, args.prompt, api_name=args.api_name)
|
| 16 |
+
|
| 17 |
+
result = {
|
| 18 |
+
"model": args.model,
|
| 19 |
+
"prompt": args.prompt,
|
| 20 |
+
"output": output
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
print(json.dumps(result, indent=2))
|
| 24 |
+
|
| 25 |
+
if __name__ == "__main__":
|
| 26 |
+
main()
|
t2i/server.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# CUDA_VISIBLE_DEVICES=0 python server.py
|
| 2 |
+
|
| 3 |
+
import importlib
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def run(model_id, prompt):
|
| 8 |
+
print(f"{model_id}: {prompt}")
|
| 9 |
+
m = importlib.import_module(model_id)
|
| 10 |
+
print(m)
|
| 11 |
+
image = m.t2i(prompt)
|
| 12 |
+
return image
|
| 13 |
+
|
| 14 |
+
def app():
|
| 15 |
+
model_id = gr.Textbox(label="model-id")
|
| 16 |
+
prompt = gr.Textbox(label="prompt")
|
| 17 |
+
image = gr.Image(label="output")
|
| 18 |
+
interface = gr.Interface(
|
| 19 |
+
fn=run,
|
| 20 |
+
inputs=[model_id, prompt],
|
| 21 |
+
outputs=image,
|
| 22 |
+
)
|
| 23 |
+
interface.launch()
|
| 24 |
+
|
| 25 |
+
if __name__ == "__main__":
|
| 26 |
+
app()
|
vlm/IXC2-4KHD.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# https://huggingface.co/internlm/internlm-xcomposer2-4khd-7b
|
| 2 |
+
# wget -c https://hf-mirror.com/internlm/internlm-xcomposer2-4khd-7b/resolve/main/pytorch_model-00001-of-00002.bin
|
| 3 |
+
# wget -c https://hf-mirror.com/internlm/internlm-xcomposer2-4khd-7b/resolve/main/pytorch_model-00002-of-00002.bin
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
import torch
|
| 7 |
+
from transformers import AutoModel, AutoTokenizer
|
| 8 |
+
|
| 9 |
+
torch.set_grad_enabled(False)
|
| 10 |
+
|
| 11 |
+
# init model and tokenizer
|
| 12 |
+
ckpt_path = "internlm/internlm-xcomposer2-4khd-7b"
|
| 13 |
+
model = AutoModel.from_pretrained(ckpt_path, torch_dtype=torch.bfloat16, trust_remote_code=True).cuda().eval()
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(ckpt_path, trust_remote_code=True)
|
| 15 |
+
|
| 16 |
+
###############
|
| 17 |
+
# First Round
|
| 18 |
+
###############
|
| 19 |
+
|
| 20 |
+
query1 = '<ImageHere>Illustrate the fine details present in the image'
|
| 21 |
+
image = './example.webp'
|
| 22 |
+
with torch.cuda.amp.autocast():
|
| 23 |
+
response, his = model.chat(tokenizer, query=query, image=image, hd_num=55, history=[], do_sample=False, num_beams=3)
|
| 24 |
+
print(response)
|
| 25 |
+
# The image is a vibrant and colorful infographic that showcases 7 graphic design trends that will dominate in 2021. The infographic is divided into 7 sections, each representing a different trend.
|
| 26 |
+
# Starting from the top, the first section focuses on "Muted Color Palettes", highlighting the use of muted colors in design.
|
| 27 |
+
# The second section delves into "Simple Data Visualizations", emphasizing the importance of easy-to-understand data visualizations.
|
| 28 |
+
# The third section introduces "Geometric Shapes Everywhere", showcasing the use of geometric shapes in design.
|
| 29 |
+
# The fourth section discusses "Flat Icons and Illustrations", explaining how flat icons and illustrations are being used in design.
|
| 30 |
+
# The fifth section is dedicated to "Classic Serif Fonts", illustrating the resurgence of classic serif fonts in design.
|
| 31 |
+
# The sixth section explores "Social Media Slide Decks", illustrating how slide decks are being used on social media.
|
| 32 |
+
# Finally, the seventh section focuses on "Text Heavy Videos", illustrating the trend of using text-heavy videos in design.
|
| 33 |
+
# Each section is filled with relevant images and text, providing a comprehensive overview of the 7 graphic design trends that will dominate in 2021.
|
| 34 |
+
|
| 35 |
+
###############
|
| 36 |
+
# Second Round
|
| 37 |
+
###############
|
| 38 |
+
query1 = 'what is the detailed explanation of the third part.'
|
| 39 |
+
with torch.cuda.amp.autocast():
|
| 40 |
+
response, _ = model.chat(tokenizer, query=query1, image=image, hd_num=55, history=his, do_sample=False, num_beams=3)
|
| 41 |
+
print(response)
|
| 42 |
+
# The third part of the infographic is about "Geometric Shapes Everywhere". It explains that last year, designers used a lot of
|
| 43 |
+
# flowing and abstract shapes in their designs. However, this year, they have been replaced with rigid, hard-edged geometric
|
| 44 |
+
# shapes and patterns. The hard edges of a geometric shape create a great contrast against muted colors.
|