Spaces:
Runtime error
Runtime error
Create run_consistentId_sdxl.py
Browse files- run_consistentId_sdxl.py +149 -0
run_consistentId_sdxl.py
ADDED
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
import shutil
|
3 |
+
from gradio_client import Client, handle_file
|
4 |
+
from tqdm import tqdm
|
5 |
+
import re
|
6 |
+
import os
|
7 |
+
import pathlib
|
8 |
+
from datasets import Dataset, Image as HuggingFaceImage
|
9 |
+
from PIL import Image
|
10 |
+
|
11 |
+
prefix_quick_list = [
|
12 |
+
"detailed photo of",
|
13 |
+
"amateur photo of",
|
14 |
+
"flicker 2008 photo of",
|
15 |
+
"fantastic artwork of",
|
16 |
+
"vintage photograph of",
|
17 |
+
"Unreal 5 render of",
|
18 |
+
"surrealist painting of",
|
19 |
+
"professional advertising design of"
|
20 |
+
]
|
21 |
+
|
22 |
+
subject_quick_list = [
|
23 |
+
"a handsome man img"
|
24 |
+
]
|
25 |
+
|
26 |
+
illumination_environment_quick_list = [
|
27 |
+
"sunshine from window", "neon light, city", "sunset over sea", "golden time", "sci-fi RGB glowing, cyberpunk", "natural lighting",
|
28 |
+
"warm atmosphere, at home, bedroom", "magic lit", "evil, gothic, in a cave", "light and shadow", "shadow from window", "soft studio lighting",
|
29 |
+
"home atmosphere, cozy bedroom illumination", "neon, Wong Kar-wai, warm", "moonlight through curtains", "stormy sky lighting",
|
30 |
+
"underwater glow, deep sea", "foggy forest at dawn", "golden hour in a meadow", "rainbow reflections, neon", "cozy candlelight",
|
31 |
+
"apocalyptic, smoky atmosphere", "red glow, emergency lights", "mystical glow, enchanted forest", "campfire light", "harsh, industrial lighting",
|
32 |
+
"sunrise in the mountains", "evening glow in the desert", "moonlight in a dark alley", "golden glow at a fairground",
|
33 |
+
"midnight in the forest", "purple and pink hues at twilight", "foggy morning, muted light", "candle-lit room, rustic vibe",
|
34 |
+
"fluorescent office lighting", "lightning flash in storm", "night, cozy warm light from fireplace", "ethereal glow, magical forest",
|
35 |
+
"dusky evening on a beach", "afternoon light filtering through trees", "blue neon light, urban street", "red and blue police lights in rain",
|
36 |
+
"aurora borealis glow, arctic landscape", "sunrise through foggy mountains", "golden hour on a city skyline", "mysterious twilight, heavy mist",
|
37 |
+
"early morning rays, forest clearing", "colorful lantern light at festival", "soft glow through stained glass", "harsh spotlight in dark room",
|
38 |
+
"mellow evening glow on a lake", "crystal reflections in a cave", "vibrant autumn lighting in a forest", "gentle snowfall at dusk",
|
39 |
+
"hazy light of a winter morning", "soft, diffused foggy glow", "underwater luminescence", "rain-soaked reflections in city lights",
|
40 |
+
"golden sunlight streaming through trees", "fireflies lighting up a summer night", "glowing embers from a forge", "dim candlelight in a gothic castle",
|
41 |
+
"midnight sky with bright starlight", "warm sunset in a rural village", "flickering light in a haunted house", "desert sunset with mirage-like glow",
|
42 |
+
"golden beams piercing through storm clouds"
|
43 |
+
]
|
44 |
+
|
45 |
+
background_quick_list = [
|
46 |
+
"cars and people", "a cozy bed and a lamp", "a forest clearing with mist", "a bustling marketplace", "a quiet beach at dusk",
|
47 |
+
"an old, cobblestone street", "a futuristic cityscape", "a tranquil lake with mountains", "a mysterious cave entrance",
|
48 |
+
"bookshelves and plants in the background", "an ancient temple in ruins", "tall skyscrapers and neon signs", "a starry sky over a desert",
|
49 |
+
"a bustling cafΓ©", "rolling hills and farmland", "a modern living room with a fireplace", "an abandoned warehouse",
|
50 |
+
"a picturesque mountain range", "a starry night sky", "the interior of a futuristic spaceship", "the cluttered workshop of an inventor",
|
51 |
+
"the glowing embers of a bonfire", "a misty lake surrounded by trees", "an ornate palace hall", "a busy street market", "a vast desert landscape",
|
52 |
+
"a peaceful library corner", "bustling train station", "a mystical, enchanted forest", "an underwater reef with colorful fish", "a quiet rural village",
|
53 |
+
"a sandy beach with palm trees", "a vibrant coral reef, teeming with life", "snow-capped mountains in distance", "a stormy ocean, waves crashing",
|
54 |
+
"a rustic barn in open fields", "a futuristic lab with glowing screens", "a dark, abandoned castle", "the ruins of an ancient civilization",
|
55 |
+
"a bustling urban street in rain", "an elegant grand ballroom", "a sprawling field of wildflowers", "a dense jungle with sunlight filtering through",
|
56 |
+
"a dimly lit, vintage bar", "an ice cave with sparkling crystals", "a serene riverbank at sunset", "a narrow alley with graffiti walls",
|
57 |
+
"a peaceful zen garden with koi pond", "a high-tech control room", "a quiet mountain village at dawn", "a lighthouse on a rocky coast",
|
58 |
+
"a rainy street with flickering lights", "a frozen lake with ice formations", "an abandoned theme park", "a small fishing village on a pier",
|
59 |
+
"rolling sand dunes in a desert", "a dense forest with towering redwoods", "a snowy cabin in the mountains", "a mystical cave with bioluminescent plants",
|
60 |
+
"a castle courtyard under moonlight", "a bustling open-air night market", "an old train station with steam", "a tranquil waterfall surrounded by trees",
|
61 |
+
"a vineyard in the countryside", "a quaint medieval village", "a bustling harbor with boats", "a high-tech futuristic mall",
|
62 |
+
"a lush tropical rainforest"
|
63 |
+
]
|
64 |
+
|
65 |
+
style_list = ['(No style)', 'Cinematic', 'Disney Charactor', 'Digital Art', 'Photographic (Default)', 'Fantasy art', 'Neonpunk', 'Enhance', 'Comic book', 'Lowpoly', 'Line art']
|
66 |
+
|
67 |
+
def clean_filename(prompt):
|
68 |
+
# Replace spaces with underscores
|
69 |
+
filename = prompt.replace(' ', '_')
|
70 |
+
# Remove or replace non-alphanumeric characters (except underscores)
|
71 |
+
filename = re.sub(r'[^a-zA-Z0-9_]', '', filename)
|
72 |
+
return filename + '.png'
|
73 |
+
|
74 |
+
output_dir = 'phm2_output_images'
|
75 |
+
if not os.path.exists(output_dir):
|
76 |
+
os.makedirs(output_dir)
|
77 |
+
|
78 |
+
def generate_unique_random_string(used_prompts):
|
79 |
+
while True:
|
80 |
+
prefix = random.choice(prefix_quick_list)
|
81 |
+
subject = random.choice(subject_quick_list)
|
82 |
+
illumination = random.choice(illumination_environment_quick_list)
|
83 |
+
background = random.choice(background_quick_list)
|
84 |
+
|
85 |
+
prompt = f"{prefix} {subject}. {illumination}. {background} in the background."
|
86 |
+
if prompt not in used_prompts:
|
87 |
+
used_prompts.add(prompt)
|
88 |
+
return {
|
89 |
+
"prompt": prompt,
|
90 |
+
"prefix": prefix,
|
91 |
+
"subject": subject,
|
92 |
+
"illumination": illumination,
|
93 |
+
"background": background
|
94 |
+
}
|
95 |
+
|
96 |
+
num_images = 10
|
97 |
+
used_prompts = set()
|
98 |
+
|
99 |
+
dataset_dict = {
|
100 |
+
"image": [],
|
101 |
+
"prefix": [],
|
102 |
+
"subject": [],
|
103 |
+
"illumination": [],
|
104 |
+
"background": [],
|
105 |
+
"prompt": [],
|
106 |
+
#"style_name": []
|
107 |
+
}
|
108 |
+
|
109 |
+
template_image_path = '/home/featurize/ConsistentID-SDXL/xiangxiang_IMG.jpeg'
|
110 |
+
template_image = handle_file(template_image_path)
|
111 |
+
|
112 |
+
client = Client("http://127.0.0.1:7860")
|
113 |
+
|
114 |
+
for _ in tqdm(range(num_images), desc="Processing prompts"):
|
115 |
+
prompt_details = generate_unique_random_string(used_prompts)
|
116 |
+
style_name = random.choice(style_list)
|
117 |
+
|
118 |
+
result = client.predict(
|
119 |
+
selected_template_images=template_image_path,
|
120 |
+
costum_image=template_image,
|
121 |
+
prompt=prompt_details["prompt"],
|
122 |
+
negative_prompt="nsfw, lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry",
|
123 |
+
prompt_selected='A young, handsome boy in a wedding suit, with a slender figure and a cute face',
|
124 |
+
retouching=False,
|
125 |
+
width=864,
|
126 |
+
height=1152,
|
127 |
+
merge_steps=30,
|
128 |
+
seed_set=2024,
|
129 |
+
api_name="/process"
|
130 |
+
)
|
131 |
+
|
132 |
+
# Save image and update dataset_dict
|
133 |
+
image_path = result
|
134 |
+
|
135 |
+
image = Image.open(image_path)
|
136 |
+
|
137 |
+
dataset_dict["image"].append(image)
|
138 |
+
dataset_dict["prefix"].append(prompt_details["prefix"])
|
139 |
+
dataset_dict["subject"].append(prompt_details["subject"])
|
140 |
+
dataset_dict["illumination"].append(prompt_details["illumination"])
|
141 |
+
dataset_dict["background"].append(prompt_details["background"])
|
142 |
+
dataset_dict["prompt"].append(prompt_details["prompt"])
|
143 |
+
#dataset_dict["style_name"].append(style_name)
|
144 |
+
|
145 |
+
dataset = Dataset.from_dict(dataset_dict)
|
146 |
+
dataset.save_to_disk("csdxl_dataset")
|
147 |
+
|
148 |
+
from datasets import load_from_disk
|
149 |
+
load_from_disk("csdxl_dataset").push_to_hub("svjack/Prince_Xiang_ConsistentID_SDXL_10")
|