Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -10,12 +10,15 @@ from dotenv import load_dotenv
|
|
10 |
load_dotenv()
|
11 |
|
12 |
# ========== 默认选项和数据 ==========
|
13 |
-
EXPRESSIONS = ["smiling", "determined", "surprised", "serene"]
|
14 |
-
ITEMS = ["magic wand", "sword", "flower", "book of spells", "ancient scroll", "music instrument"]
|
15 |
-
OTHER_DETAILS = ["sparkles", "magical aura", "lens flare", "fireworks in the background"]
|
16 |
-
SCENES = ["sunset beach", "rainy city street at night", "fantasy forest with glowing mushrooms", "futuristic skyline at dawn"]
|
17 |
-
CAMERA_ANGLES = ["low-angle shot", "close-up shot", "bird's-eye view", "wide-angle shot"]
|
18 |
-
QUALITY_PROMPTS = ["
|
|
|
|
|
|
|
19 |
|
20 |
# ========== 工具函数 ==========
|
21 |
def load_candidates_from_files(files):
|
@@ -36,12 +39,12 @@ def get_random_items(candidates, num_items=1):
|
|
36 |
"""
|
37 |
return random.sample(candidates, min(num_items, len(candidates))) if candidates else []
|
38 |
|
39 |
-
def load_dtr_from_huggingface(
|
40 |
"""
|
41 |
-
从 Hugging Face
|
42 |
"""
|
43 |
try:
|
44 |
-
response = requests.get(
|
45 |
response.raise_for_status()
|
46 |
return response.text.splitlines()
|
47 |
except Exception as e:
|
@@ -84,33 +87,33 @@ def generate_natural_language_description(tags, api_key=None):
|
|
84 |
return f"GPT generation failed. Error: {e}"
|
85 |
|
86 |
def generate_prompt(
|
87 |
-
action_file, style_file, artist_files, character_files, dtr_enabled,
|
88 |
-
expression_count, item_count, detail_count, scene_count, angle_count, quality_count
|
89 |
):
|
90 |
"""
|
91 |
生成随机提示词和描述。
|
92 |
"""
|
93 |
-
actions = load_candidates_from_files([action_file]) if action_file else []
|
94 |
-
styles = load_candidates_from_files([style_file]) if style_file else []
|
95 |
-
artists = load_candidates_from_files(artist_files) if artist_files else []
|
96 |
-
characters = load_candidates_from_files(character_files) if character_files else []
|
97 |
-
dtr_candidates = load_dtr_from_huggingface(
|
98 |
|
99 |
number_of_characters = ", ".join(selected_categories) if selected_categories else random.choice(["1girl", "1boy"])
|
100 |
|
101 |
tags = {
|
102 |
"number_of_characters": number_of_characters,
|
103 |
-
"character_name":
|
104 |
-
"artist_prompt": f"(artist:{
|
105 |
-
"style":
|
106 |
"scene": get_random_items(SCENES, scene_count),
|
107 |
"camera_angle": get_random_items(CAMERA_ANGLES, angle_count),
|
108 |
-
"action":
|
109 |
"expression": get_random_items(EXPRESSIONS, expression_count),
|
110 |
"items": get_random_items(ITEMS, item_count),
|
111 |
"other_details": get_random_items(OTHER_DETAILS, detail_count),
|
112 |
"quality_prompts": get_random_items(QUALITY_PROMPTS, quality_count),
|
113 |
-
"dtr":
|
114 |
}
|
115 |
|
116 |
description = generate_natural_language_description(tags, api_key)
|
@@ -143,12 +146,7 @@ def gradio_interface():
|
|
143 |
artist_files = gr.Files(label="Upload Artist Files (Multiple Allowed)", file_types=[".txt"])
|
144 |
character_files = gr.Files(label="Upload Character Files (Multiple Allowed)", file_types=[".txt"])
|
145 |
|
146 |
-
dtr_enabled = gr.Checkbox(label="Enable DTR
|
147 |
-
dtr_url = gr.Textbox(
|
148 |
-
label="Hugging Face DTR Dataset URL",
|
149 |
-
placeholder="https://huggingface.co/datasets/X779/Danbooruwildcards/tree/main",
|
150 |
-
value="https://huggingface.co/datasets/X779/Danbooruwildcards/tree/main"
|
151 |
-
)
|
152 |
|
153 |
selected_categories = gr.CheckboxGroup(
|
154 |
["1boy", "1girl", "furry", "mecha", "fantasy monster", "animal", "still life"],
|
@@ -158,14 +156,14 @@ def gradio_interface():
|
|
158 |
with gr.Row():
|
159 |
expression_count = gr.Slider(label="Number of Expressions", minimum=1, maximum=5, step=1, value=1)
|
160 |
item_count = gr.Slider(label="Number of Items", minimum=1, maximum=5, step=1, value=1)
|
161 |
-
|
162 |
-
with gr.Row():
|
163 |
detail_count = gr.Slider(label="Number of Other Details", minimum=1, maximum=5, step=1, value=1)
|
164 |
scene_count = gr.Slider(label="Number of Scenes", minimum=1, maximum=5, step=1, value=1)
|
165 |
|
166 |
with gr.Row():
|
167 |
angle_count = gr.Slider(label="Number of Camera Angles", minimum=1, maximum=5, step=1, value=1)
|
168 |
quality_count = gr.Slider(label="Number of Quality Prompts", minimum=1, maximum=5, step=1, value=1)
|
|
|
|
|
169 |
|
170 |
with gr.Row():
|
171 |
tags_output = gr.Textbox(label="Generated Tags")
|
@@ -177,8 +175,8 @@ def gradio_interface():
|
|
177 |
generate_button.click(
|
178 |
generate_prompt,
|
179 |
inputs=[
|
180 |
-
action_file, style_file, artist_files, character_files, dtr_enabled,
|
181 |
-
expression_count, item_count, detail_count, scene_count, angle_count, quality_count
|
182 |
],
|
183 |
outputs=[tags_output, description_output, combined_output],
|
184 |
)
|
|
|
10 |
load_dotenv()
|
11 |
|
12 |
# ========== 默认选项和数据 ==========
|
13 |
+
EXPRESSIONS = ["smiling", "determined", "surprised", "serene", "laughing", "angry", "pensive", "confident"]
|
14 |
+
ITEMS = ["magic wand", "sword", "flower", "book of spells", "ancient scroll", "music instrument", "shield", "dagger", "staff", "potion"]
|
15 |
+
OTHER_DETAILS = ["sparkles", "magical aura", "lens flare", "fireworks in the background", "smoke effects", "light trails", "falling leaves", "glowing embers"]
|
16 |
+
SCENES = ["sunset beach", "rainy city street at night", "fantasy forest with glowing mushrooms", "futuristic skyline at dawn", "abandoned castle", "snowy mountain peak", "desert ruins", "underwater city"]
|
17 |
+
CAMERA_ANGLES = ["low-angle shot", "close-up shot", "bird's-eye view", "wide-angle shot", "over-the-shoulder shot", "extreme close-up", "panoramic view", "dynamic tracking shot"]
|
18 |
+
QUALITY_PROMPTS = ["cinematic lighting", "award-winning", "masterpiece", "vivid colors", "high dynamic range", "immersive", "studio quality", "fine art", "dreamlike", "8K", "HD", "high quality", "best quality"]
|
19 |
+
|
20 |
+
# Hugging Face DTR 数据集路径
|
21 |
+
DTR_DATASET_PATTERN = "https://huggingface.co/datasets/X779/Danbooruwildcards/resolve/main/*DTR*.txt"
|
22 |
|
23 |
# ========== 工具函数 ==========
|
24 |
def load_candidates_from_files(files):
|
|
|
39 |
"""
|
40 |
return random.sample(candidates, min(num_items, len(candidates))) if candidates else []
|
41 |
|
42 |
+
def load_dtr_from_huggingface():
|
43 |
"""
|
44 |
+
从 Hugging Face 数据集中加载所有包含 "DTR" 的文件内容。
|
45 |
"""
|
46 |
try:
|
47 |
+
response = requests.get(DTR_DATASET_PATTERN)
|
48 |
response.raise_for_status()
|
49 |
return response.text.splitlines()
|
50 |
except Exception as e:
|
|
|
87 |
return f"GPT generation failed. Error: {e}"
|
88 |
|
89 |
def generate_prompt(
|
90 |
+
action_file, style_file, artist_files, character_files, dtr_enabled, api_key, selected_categories,
|
91 |
+
expression_count, item_count, detail_count, scene_count, angle_count, quality_count, action_count, style_count
|
92 |
):
|
93 |
"""
|
94 |
生成随机提示词和描述。
|
95 |
"""
|
96 |
+
actions = get_random_items(load_candidates_from_files([action_file]) if action_file else [], action_count)
|
97 |
+
styles = get_random_items(load_candidates_from_files([style_file]) if style_file else [], style_count)
|
98 |
+
artists = get_random_items(load_candidates_from_files(artist_files) if artist_files else [], 1)
|
99 |
+
characters = get_random_items(load_candidates_from_files(character_files) if character_files else [], 1)
|
100 |
+
dtr_candidates = get_random_items(load_dtr_from_huggingface() if dtr_enabled else [], 1)
|
101 |
|
102 |
number_of_characters = ", ".join(selected_categories) if selected_categories else random.choice(["1girl", "1boy"])
|
103 |
|
104 |
tags = {
|
105 |
"number_of_characters": number_of_characters,
|
106 |
+
"character_name": characters,
|
107 |
+
"artist_prompt": f"(artist:{artists})",
|
108 |
+
"style": styles,
|
109 |
"scene": get_random_items(SCENES, scene_count),
|
110 |
"camera_angle": get_random_items(CAMERA_ANGLES, angle_count),
|
111 |
+
"action": actions,
|
112 |
"expression": get_random_items(EXPRESSIONS, expression_count),
|
113 |
"items": get_random_items(ITEMS, item_count),
|
114 |
"other_details": get_random_items(OTHER_DETAILS, detail_count),
|
115 |
"quality_prompts": get_random_items(QUALITY_PROMPTS, quality_count),
|
116 |
+
"dtr": dtr_candidates
|
117 |
}
|
118 |
|
119 |
description = generate_natural_language_description(tags, api_key)
|
|
|
146 |
artist_files = gr.Files(label="Upload Artist Files (Multiple Allowed)", file_types=[".txt"])
|
147 |
character_files = gr.Files(label="Upload Character Files (Multiple Allowed)", file_types=[".txt"])
|
148 |
|
149 |
+
dtr_enabled = gr.Checkbox(label="Enable DTR")
|
|
|
|
|
|
|
|
|
|
|
150 |
|
151 |
selected_categories = gr.CheckboxGroup(
|
152 |
["1boy", "1girl", "furry", "mecha", "fantasy monster", "animal", "still life"],
|
|
|
156 |
with gr.Row():
|
157 |
expression_count = gr.Slider(label="Number of Expressions", minimum=1, maximum=5, step=1, value=1)
|
158 |
item_count = gr.Slider(label="Number of Items", minimum=1, maximum=5, step=1, value=1)
|
|
|
|
|
159 |
detail_count = gr.Slider(label="Number of Other Details", minimum=1, maximum=5, step=1, value=1)
|
160 |
scene_count = gr.Slider(label="Number of Scenes", minimum=1, maximum=5, step=1, value=1)
|
161 |
|
162 |
with gr.Row():
|
163 |
angle_count = gr.Slider(label="Number of Camera Angles", minimum=1, maximum=5, step=1, value=1)
|
164 |
quality_count = gr.Slider(label="Number of Quality Prompts", minimum=1, maximum=5, step=1, value=1)
|
165 |
+
action_count = gr.Slider(label="Number of Actions", minimum=1, maximum=5, step=1, value=1)
|
166 |
+
style_count = gr.Slider(label="Number of Styles", minimum=1, maximum=5, step=1, value=1)
|
167 |
|
168 |
with gr.Row():
|
169 |
tags_output = gr.Textbox(label="Generated Tags")
|
|
|
175 |
generate_button.click(
|
176 |
generate_prompt,
|
177 |
inputs=[
|
178 |
+
action_file, style_file, artist_files, character_files, dtr_enabled, api_key_input, selected_categories,
|
179 |
+
expression_count, item_count, detail_count, scene_count, angle_count, quality_count, action_count, style_count
|
180 |
],
|
181 |
outputs=[tags_output, description_output, combined_output],
|
182 |
)
|