Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,3 @@
|
|
| 1 |
-
##############################################
|
| 2 |
-
# app.py
|
| 3 |
-
##############################################
|
| 4 |
import os
|
| 5 |
import json
|
| 6 |
import gradio as gr
|
|
@@ -9,14 +6,12 @@ from openai import OpenAI
|
|
| 9 |
##############################################################################
|
| 10 |
# 1. 读取外部文件: furry_species.json & gender_rules.json
|
| 11 |
##############################################################################
|
| 12 |
-
# 假设 furry_species.json 的结构是多级字典: { "AQUATICS ...": { "Cetaceans": [], ...}, ... }
|
| 13 |
try:
|
| 14 |
with open("furry_species.json", "r", encoding="utf-8") as f:
|
| 15 |
FURRY_DATA = json.load(f)
|
| 16 |
except:
|
| 17 |
FURRY_DATA = {}
|
| 18 |
|
| 19 |
-
# gender_rules.json: { "male": "...", "female": "...", "intersex": "...", "genderless": "..." }
|
| 20 |
try:
|
| 21 |
with open("gender_rules.json", "r", encoding="utf-8") as f:
|
| 22 |
GENDER_RULES = json.load(f)
|
|
@@ -24,83 +19,66 @@ except:
|
|
| 24 |
GENDER_RULES = {}
|
| 25 |
|
| 26 |
##############################################################################
|
| 27 |
-
# 2.
|
| 28 |
##############################################################################
|
| 29 |
def get_top_categories(furry_data):
|
| 30 |
-
"""获取所有顶级分类 (keys)"""
|
| 31 |
return sorted(list(furry_data.keys()))
|
| 32 |
|
| 33 |
def get_sub_categories(furry_data, top_category):
|
| 34 |
-
"""
|
| 35 |
-
根据所选 top_category, 返回二级分类列表
|
| 36 |
-
furry_data[top_category] -> { "Cetaceans": [...], "FishFurs": [...], ... }
|
| 37 |
-
"""
|
| 38 |
if top_category in furry_data:
|
| 39 |
return sorted(list(furry_data[top_category].keys()))
|
| 40 |
return []
|
| 41 |
|
| 42 |
def get_species_list(furry_data, top_category, sub_category):
|
| 43 |
-
|
| 44 |
-
返回最终物种列表
|
| 45 |
-
furry_data[top_category][sub_category] -> list
|
| 46 |
-
"""
|
| 47 |
-
if (
|
| 48 |
-
top_category in furry_data
|
| 49 |
-
and sub_category in furry_data[top_category]
|
| 50 |
-
):
|
| 51 |
return sorted(furry_data[top_category][sub_category])
|
| 52 |
return []
|
| 53 |
|
| 54 |
##############################################################################
|
| 55 |
-
# 3.
|
| 56 |
##############################################################################
|
| 57 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
"""
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
| 63 |
"""
|
| 64 |
if not api_key:
|
| 65 |
return "Error: No API Key provided."
|
| 66 |
-
|
| 67 |
-
#
|
| 68 |
-
tags = {}
|
| 69 |
if gender_option == "Trans_to_Male":
|
| 70 |
-
tags["gender"] = "male"
|
| 71 |
rule_text = GENDER_RULES.get("male", "")
|
| 72 |
elif gender_option == "Trans_to_Female":
|
| 73 |
-
tags["gender"] = "female"
|
| 74 |
rule_text = GENDER_RULES.get("female", "")
|
| 75 |
elif gender_option == "Trans_to_Mannequin":
|
| 76 |
-
tags["gender"] = "genderless"
|
| 77 |
rule_text = GENDER_RULES.get("genderless", "")
|
| 78 |
elif gender_option == "Trans_to_Intersex":
|
| 79 |
-
tags["gender"] = "intersex"
|
| 80 |
rule_text = GENDER_RULES.get("intersex", "")
|
| 81 |
else:
|
| 82 |
# Furry
|
| 83 |
-
|
| 84 |
rule_text = (
|
| 85 |
-
GENDER_RULES.get("male", "") + "\n\n"
|
| 86 |
+ GENDER_RULES.get("female", "") + "\n\n"
|
| 87 |
+ GENDER_RULES.get("intersex", "") + "\n\n"
|
| 88 |
+ GENDER_RULES.get("genderless", "")
|
| 89 |
-
)
|
| 90 |
-
|
| 91 |
-
# 选定物种
|
| 92 |
-
final_species = "unknown"
|
| 93 |
if top_cat and sub_cat and species_item:
|
| 94 |
-
|
| 95 |
-
tags["furry_species"] = final_species
|
| 96 |
-
|
| 97 |
-
# 原始提示词
|
| 98 |
-
tags["base_prompt"] = prompt
|
| 99 |
|
| 100 |
-
#
|
| 101 |
if api_mode == "GPT":
|
| 102 |
base_url = None
|
| 103 |
-
model_name = "gpt-3.5-turbo"
|
| 104 |
else:
|
| 105 |
base_url = "https://api.deepseek.com"
|
| 106 |
model_name = "deepseek-chat"
|
|
@@ -109,40 +87,46 @@ def generate_tags_and_description(prompt, gender_option, top_cat, sub_cat, speci
|
|
| 109 |
if base_url:
|
| 110 |
client.base_url = base_url
|
| 111 |
|
| 112 |
-
#
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
-
# Chat
|
| 125 |
try:
|
| 126 |
resp = client.chat.completions.create(
|
| 127 |
model=model_name,
|
| 128 |
messages=[
|
| 129 |
{"role": "system", "content": system_prompt},
|
| 130 |
-
{
|
| 131 |
-
"role": "user",
|
| 132 |
-
"content": f"Here are the tags:\n{tags_str}\nPlease generate a vivid, imaginative scene description."
|
| 133 |
-
},
|
| 134 |
],
|
| 135 |
)
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
return
|
| 139 |
|
| 140 |
except Exception as e:
|
| 141 |
return f"{api_mode} generation failed. Error: {e}"
|
| 142 |
|
| 143 |
def translate_text(content, lang, api_mode, api_key):
|
| 144 |
"""
|
| 145 |
-
|
| 146 |
"""
|
| 147 |
if not api_key:
|
| 148 |
return "Error: No API Key provided."
|
|
@@ -160,13 +144,18 @@ def translate_text(content, lang, api_mode, api_key):
|
|
| 160 |
if base_url:
|
| 161 |
client.base_url = base_url
|
| 162 |
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
try:
|
| 165 |
resp = client.chat.completions.create(
|
| 166 |
model=model_name,
|
| 167 |
messages=[
|
| 168 |
-
{"role": "system", "content":
|
| 169 |
-
{"role": "user",
|
| 170 |
],
|
| 171 |
)
|
| 172 |
return resp.choices[0].message.content.strip()
|
|
@@ -174,27 +163,30 @@ def translate_text(content, lang, api_mode, api_key):
|
|
| 174 |
return f"{api_mode} translation failed. Error: {e}"
|
| 175 |
|
| 176 |
##############################################################################
|
| 177 |
-
# 4. Gradio
|
| 178 |
##############################################################################
|
| 179 |
def build_interface():
|
| 180 |
with gr.Blocks() as demo:
|
| 181 |
-
gr.Markdown("## Prompt Transformer
|
| 182 |
|
| 183 |
with gr.Row():
|
| 184 |
with gr.Column():
|
|
|
|
| 185 |
api_mode = gr.Radio(
|
| 186 |
-
label="Choose
|
| 187 |
choices=["GPT", "DeepSeek"],
|
| 188 |
value="GPT"
|
| 189 |
)
|
|
|
|
| 190 |
api_key = gr.Textbox(
|
| 191 |
label="API Key",
|
| 192 |
-
type="password"
|
|
|
|
| 193 |
)
|
| 194 |
|
| 195 |
-
#
|
| 196 |
gender_option = gr.Radio(
|
| 197 |
-
label="
|
| 198 |
choices=[
|
| 199 |
"Trans_to_Male",
|
| 200 |
"Trans_to_Female",
|
|
@@ -205,109 +197,113 @@ def build_interface():
|
|
| 205 |
value="Trans_to_Male"
|
| 206 |
)
|
| 207 |
|
| 208 |
-
#
|
| 209 |
top_cat_dd = gr.Dropdown(
|
| 210 |
-
label="Furry:
|
| 211 |
choices=get_top_categories(FURRY_DATA),
|
| 212 |
value=None,
|
| 213 |
visible=False
|
| 214 |
)
|
| 215 |
-
# 二级分类
|
| 216 |
sub_cat_dd = gr.Dropdown(
|
| 217 |
-
label="Furry:
|
| 218 |
choices=[],
|
| 219 |
value=None,
|
| 220 |
visible=False
|
| 221 |
)
|
| 222 |
-
# 物种
|
| 223 |
species_dd = gr.Dropdown(
|
| 224 |
-
label="Furry:
|
| 225 |
choices=[],
|
| 226 |
value=None,
|
| 227 |
visible=False
|
| 228 |
)
|
| 229 |
|
| 230 |
-
#
|
| 231 |
def show_furry_options(chosen):
|
| 232 |
if chosen == "Trans_to_Furry":
|
| 233 |
-
return
|
|
|
|
|
|
|
| 234 |
else:
|
| 235 |
-
return
|
| 236 |
-
|
|
|
|
| 237 |
gender_option.change(
|
| 238 |
fn=show_furry_options,
|
| 239 |
inputs=[gender_option],
|
| 240 |
outputs=[top_cat_dd, sub_cat_dd, species_dd]
|
| 241 |
)
|
| 242 |
|
| 243 |
-
#
|
| 244 |
def on_top_cat_select(selected):
|
| 245 |
subs = get_sub_categories(FURRY_DATA, selected)
|
| 246 |
return gr.update(choices=subs, value=None)
|
| 247 |
-
|
| 248 |
top_cat_dd.change(
|
| 249 |
fn=on_top_cat_select,
|
| 250 |
inputs=[top_cat_dd],
|
| 251 |
outputs=[sub_cat_dd]
|
| 252 |
)
|
| 253 |
|
| 254 |
-
# 子分类 ->
|
| 255 |
def on_sub_cat_select(top_c, sub_c):
|
| 256 |
sp = get_species_list(FURRY_DATA, top_c, sub_c)
|
| 257 |
return gr.update(choices=sp, value=None)
|
| 258 |
-
|
| 259 |
sub_cat_dd.change(
|
| 260 |
fn=on_sub_cat_select,
|
| 261 |
inputs=[top_cat_dd, sub_cat_dd],
|
| 262 |
outputs=[species_dd]
|
| 263 |
)
|
| 264 |
|
|
|
|
| 265 |
with gr.Column():
|
| 266 |
user_prompt = gr.Textbox(
|
| 267 |
-
label="
|
| 268 |
-
lines=
|
|
|
|
| 269 |
)
|
| 270 |
-
|
| 271 |
-
label="(
|
| 272 |
lines=10
|
| 273 |
)
|
| 274 |
|
|
|
|
| 275 |
with gr.Row():
|
| 276 |
translate_lang = gr.Dropdown(
|
| 277 |
-
label="翻译语言
|
| 278 |
choices=["English", "Chinese", "Japanese", "French", "German", "Spanish"],
|
| 279 |
value="English"
|
| 280 |
)
|
| 281 |
-
|
| 282 |
-
label="
|
| 283 |
lines=10
|
| 284 |
)
|
| 285 |
|
| 286 |
######################################################################
|
| 287 |
-
#
|
| 288 |
######################################################################
|
| 289 |
-
def on_generate(prompt, gender, tc, sc,
|
| 290 |
-
# 1)
|
| 291 |
-
|
| 292 |
# 2) 翻译
|
| 293 |
-
|
| 294 |
-
return
|
| 295 |
|
|
|
|
| 296 |
user_prompt.submit(
|
| 297 |
fn=on_generate,
|
| 298 |
inputs=[user_prompt, gender_option, top_cat_dd, sub_cat_dd, species_dd, api_mode, api_key, translate_lang],
|
| 299 |
-
outputs=[
|
| 300 |
)
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
fn=on_generate,
|
| 305 |
inputs=[user_prompt, gender_option, top_cat_dd, sub_cat_dd, species_dd, api_mode, api_key, translate_lang],
|
| 306 |
-
outputs=[
|
| 307 |
)
|
| 308 |
|
| 309 |
return demo
|
| 310 |
|
|
|
|
| 311 |
if __name__ == "__main__":
|
| 312 |
demo = build_interface()
|
| 313 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import gradio as gr
|
|
|
|
| 6 |
##############################################################################
|
| 7 |
# 1. 读取外部文件: furry_species.json & gender_rules.json
|
| 8 |
##############################################################################
|
|
|
|
| 9 |
try:
|
| 10 |
with open("furry_species.json", "r", encoding="utf-8") as f:
|
| 11 |
FURRY_DATA = json.load(f)
|
| 12 |
except:
|
| 13 |
FURRY_DATA = {}
|
| 14 |
|
|
|
|
| 15 |
try:
|
| 16 |
with open("gender_rules.json", "r", encoding="utf-8") as f:
|
| 17 |
GENDER_RULES = json.load(f)
|
|
|
|
| 19 |
GENDER_RULES = {}
|
| 20 |
|
| 21 |
##############################################################################
|
| 22 |
+
# 2. 多级菜单构造函数
|
| 23 |
##############################################################################
|
| 24 |
def get_top_categories(furry_data):
|
|
|
|
| 25 |
return sorted(list(furry_data.keys()))
|
| 26 |
|
| 27 |
def get_sub_categories(furry_data, top_category):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
if top_category in furry_data:
|
| 29 |
return sorted(list(furry_data[top_category].keys()))
|
| 30 |
return []
|
| 31 |
|
| 32 |
def get_species_list(furry_data, top_category, sub_category):
|
| 33 |
+
if top_category in furry_data and sub_category in furry_data[top_category]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
return sorted(furry_data[top_category][sub_category])
|
| 35 |
return []
|
| 36 |
|
| 37 |
##############################################################################
|
| 38 |
+
# 3. 核心调用:GPT 或 DeepSeek
|
| 39 |
##############################################################################
|
| 40 |
+
def generate_transformed_output(
|
| 41 |
+
prompt, # 原始 Prompt,如 "1girl, butterfly, solo..."
|
| 42 |
+
gender_option, # 转换目标
|
| 43 |
+
top_cat, sub_cat, species_item,
|
| 44 |
+
api_mode, api_key
|
| 45 |
+
):
|
| 46 |
"""
|
| 47 |
+
根据指定的性别/物种等规则,让 GPT/DeepSeek 输出仅两段内容:
|
| 48 |
+
(转化后tags)
|
| 49 |
+
|
| 50 |
+
转化后描述 (3~6句)
|
| 51 |
+
不展示原始 prompt/base_prompt/gender: ... 等信息。
|
| 52 |
"""
|
| 53 |
if not api_key:
|
| 54 |
return "Error: No API Key provided."
|
| 55 |
+
|
| 56 |
+
# 根据 gender_option 选对应的 rule
|
|
|
|
| 57 |
if gender_option == "Trans_to_Male":
|
|
|
|
| 58 |
rule_text = GENDER_RULES.get("male", "")
|
| 59 |
elif gender_option == "Trans_to_Female":
|
|
|
|
| 60 |
rule_text = GENDER_RULES.get("female", "")
|
| 61 |
elif gender_option == "Trans_to_Mannequin":
|
|
|
|
| 62 |
rule_text = GENDER_RULES.get("genderless", "")
|
| 63 |
elif gender_option == "Trans_to_Intersex":
|
|
|
|
| 64 |
rule_text = GENDER_RULES.get("intersex", "")
|
| 65 |
else:
|
| 66 |
# Furry
|
| 67 |
+
# 你可以综合 male/female/intersex/genderless,也可以有专门 furry 的说明
|
| 68 |
rule_text = (
|
| 69 |
+
GENDER_RULES.get("male", "") + "\n\n"
|
| 70 |
+ GENDER_RULES.get("female", "") + "\n\n"
|
| 71 |
+ GENDER_RULES.get("intersex", "") + "\n\n"
|
| 72 |
+ GENDER_RULES.get("genderless", "")
|
| 73 |
+
)
|
| 74 |
+
# 如果想在规则中附加选定的物种信息:
|
|
|
|
|
|
|
| 75 |
if top_cat and sub_cat and species_item:
|
| 76 |
+
rule_text += f"\nFurry species: {top_cat} > {sub_cat} > {species_item}\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
# 选定 GPT or DeepSeek
|
| 79 |
if api_mode == "GPT":
|
| 80 |
base_url = None
|
| 81 |
+
model_name = "gpt-3.5-turbo" # 可改成 "gpt-4"
|
| 82 |
else:
|
| 83 |
base_url = "https://api.deepseek.com"
|
| 84 |
model_name = "deepseek-chat"
|
|
|
|
| 87 |
if base_url:
|
| 88 |
client.base_url = base_url
|
| 89 |
|
| 90 |
+
# 构造 System Prompt:要求只输出两段;第一行(转化后tags),空行,随后3~6句描述
|
| 91 |
+
# 让它把 prompt 中的 tags 进行「合并、替换、去重、增加」等处理
|
| 92 |
+
system_prompt = f"""
|
| 93 |
+
You are a creative assistant that modifies the user's base prompt tags
|
| 94 |
+
to reflect the correct gender/furry transformations, following these rules:
|
| 95 |
+
|
| 96 |
+
{rule_text}
|
| 97 |
+
|
| 98 |
+
Steps:
|
| 99 |
+
1) The original prompt tags are: {prompt}
|
| 100 |
+
2) Convert them into NEW combined tags that reflect the requested transformation.
|
| 101 |
+
(Remove or replace conflicting tags, unify synonyms, add any essential tags
|
| 102 |
+
for {gender_option} or for the selected furry species.)
|
| 103 |
+
3) Output EXACTLY two parts:
|
| 104 |
+
- First line: the final, consolidated tags in parentheses (e.g. (male, solo, ...)).
|
| 105 |
+
- Then a blank line.
|
| 106 |
+
- Then a short imaginative scene description (3 to 6 sentences).
|
| 107 |
+
4) Do NOT include 'gender:' or 'base_prompt:' or any headings or extra lines.
|
| 108 |
+
5) Output everything in English.
|
| 109 |
+
6) Do not reference these steps in the final answer.
|
| 110 |
+
""".strip()
|
| 111 |
|
|
|
|
| 112 |
try:
|
| 113 |
resp = client.chat.completions.create(
|
| 114 |
model=model_name,
|
| 115 |
messages=[
|
| 116 |
{"role": "system", "content": system_prompt},
|
| 117 |
+
{"role": "user", "content": "Generate the final tags and description now."},
|
|
|
|
|
|
|
|
|
|
| 118 |
],
|
| 119 |
)
|
| 120 |
+
# 结果中仅包含 (tags)\n\n(description)
|
| 121 |
+
result = resp.choices[0].message.content.strip()
|
| 122 |
+
return result
|
| 123 |
|
| 124 |
except Exception as e:
|
| 125 |
return f"{api_mode} generation failed. Error: {e}"
|
| 126 |
|
| 127 |
def translate_text(content, lang, api_mode, api_key):
|
| 128 |
"""
|
| 129 |
+
对上一步的 (tags)\n\n(description) 做翻译,保持格式
|
| 130 |
"""
|
| 131 |
if not api_key:
|
| 132 |
return "Error: No API Key provided."
|
|
|
|
| 144 |
if base_url:
|
| 145 |
client.base_url = base_url
|
| 146 |
|
| 147 |
+
translate_system_prompt = f"""
|
| 148 |
+
You are a translator. Translate the following text to {lang},
|
| 149 |
+
preserving the parentheses line and blank lines if present.
|
| 150 |
+
Do not add extra headings.
|
| 151 |
+
""".strip()
|
| 152 |
+
|
| 153 |
try:
|
| 154 |
resp = client.chat.completions.create(
|
| 155 |
model=model_name,
|
| 156 |
messages=[
|
| 157 |
+
{"role": "system", "content": translate_system_prompt},
|
| 158 |
+
{"role": "user", "content": content},
|
| 159 |
],
|
| 160 |
)
|
| 161 |
return resp.choices[0].message.content.strip()
|
|
|
|
| 163 |
return f"{api_mode} translation failed. Error: {e}"
|
| 164 |
|
| 165 |
##############################################################################
|
| 166 |
+
# 4. Gradio 前端
|
| 167 |
##############################################################################
|
| 168 |
def build_interface():
|
| 169 |
with gr.Blocks() as demo:
|
| 170 |
+
gr.Markdown("## Prompt Transformer - 不显示原Prompt与gender/base_prompt,只输出新tags和描述 (GPT/DeepSeek)")
|
| 171 |
|
| 172 |
with gr.Row():
|
| 173 |
with gr.Column():
|
| 174 |
+
# 选择 GPT/DeepSeek
|
| 175 |
api_mode = gr.Radio(
|
| 176 |
+
label="Choose API (GPT or DeepSeek)",
|
| 177 |
choices=["GPT", "DeepSeek"],
|
| 178 |
value="GPT"
|
| 179 |
)
|
| 180 |
+
# 输入 API Key
|
| 181 |
api_key = gr.Textbox(
|
| 182 |
label="API Key",
|
| 183 |
+
type="password",
|
| 184 |
+
placeholder="Enter your GPT or DeepSeek Key here"
|
| 185 |
)
|
| 186 |
|
| 187 |
+
# 性别/Furry
|
| 188 |
gender_option = gr.Radio(
|
| 189 |
+
label="Gender / Furry Conversion",
|
| 190 |
choices=[
|
| 191 |
"Trans_to_Male",
|
| 192 |
"Trans_to_Female",
|
|
|
|
| 197 |
value="Trans_to_Male"
|
| 198 |
)
|
| 199 |
|
| 200 |
+
# 若选 Furry -> 多级菜单
|
| 201 |
top_cat_dd = gr.Dropdown(
|
| 202 |
+
label="Furry: Top Category",
|
| 203 |
choices=get_top_categories(FURRY_DATA),
|
| 204 |
value=None,
|
| 205 |
visible=False
|
| 206 |
)
|
|
|
|
| 207 |
sub_cat_dd = gr.Dropdown(
|
| 208 |
+
label="Furry: Sub Category",
|
| 209 |
choices=[],
|
| 210 |
value=None,
|
| 211 |
visible=False
|
| 212 |
)
|
|
|
|
| 213 |
species_dd = gr.Dropdown(
|
| 214 |
+
label="Furry: Species",
|
| 215 |
choices=[],
|
| 216 |
value=None,
|
| 217 |
visible=False
|
| 218 |
)
|
| 219 |
|
| 220 |
+
# 当性别选项切到 Furry 时,显示下拉,否则隐藏
|
| 221 |
def show_furry_options(chosen):
|
| 222 |
if chosen == "Trans_to_Furry":
|
| 223 |
+
return (gr.update(visible=True),
|
| 224 |
+
gr.update(visible=True),
|
| 225 |
+
gr.update(visible=True))
|
| 226 |
else:
|
| 227 |
+
return (gr.update(visible=False),
|
| 228 |
+
gr.update(visible=False),
|
| 229 |
+
gr.update(visible=False))
|
| 230 |
gender_option.change(
|
| 231 |
fn=show_furry_options,
|
| 232 |
inputs=[gender_option],
|
| 233 |
outputs=[top_cat_dd, sub_cat_dd, species_dd]
|
| 234 |
)
|
| 235 |
|
| 236 |
+
# 主分类 -> 子分类
|
| 237 |
def on_top_cat_select(selected):
|
| 238 |
subs = get_sub_categories(FURRY_DATA, selected)
|
| 239 |
return gr.update(choices=subs, value=None)
|
|
|
|
| 240 |
top_cat_dd.change(
|
| 241 |
fn=on_top_cat_select,
|
| 242 |
inputs=[top_cat_dd],
|
| 243 |
outputs=[sub_cat_dd]
|
| 244 |
)
|
| 245 |
|
| 246 |
+
# 子分类 -> 物种
|
| 247 |
def on_sub_cat_select(top_c, sub_c):
|
| 248 |
sp = get_species_list(FURRY_DATA, top_c, sub_c)
|
| 249 |
return gr.update(choices=sp, value=None)
|
|
|
|
| 250 |
sub_cat_dd.change(
|
| 251 |
fn=on_sub_cat_select,
|
| 252 |
inputs=[top_cat_dd, sub_cat_dd],
|
| 253 |
outputs=[species_dd]
|
| 254 |
)
|
| 255 |
|
| 256 |
+
# 输入 prompt & 输出
|
| 257 |
with gr.Column():
|
| 258 |
user_prompt = gr.Textbox(
|
| 259 |
+
label="原始 Prompt (Base Tags)",
|
| 260 |
+
lines=5,
|
| 261 |
+
placeholder="e.g. 1girl, butterfly, solo, flower..."
|
| 262 |
)
|
| 263 |
+
final_output = gr.Textbox(
|
| 264 |
+
label="(转化后Tags)\n\n(转化后描述)",
|
| 265 |
lines=10
|
| 266 |
)
|
| 267 |
|
| 268 |
+
# 翻译
|
| 269 |
with gr.Row():
|
| 270 |
translate_lang = gr.Dropdown(
|
| 271 |
+
label="翻译语言",
|
| 272 |
choices=["English", "Chinese", "Japanese", "French", "German", "Spanish"],
|
| 273 |
value="English"
|
| 274 |
)
|
| 275 |
+
translated_result = gr.Textbox(
|
| 276 |
+
label="翻译后的结果",
|
| 277 |
lines=10
|
| 278 |
)
|
| 279 |
|
| 280 |
######################################################################
|
| 281 |
+
# 事件
|
| 282 |
######################################################################
|
| 283 |
+
def on_generate(prompt, gender, tc, sc, sp, mode, key, lang):
|
| 284 |
+
# 1) 生成新的 (tags) + 描述
|
| 285 |
+
merged = generate_transformed_output(prompt, gender, tc, sc, sp, mode, key)
|
| 286 |
# 2) 翻译
|
| 287 |
+
trans = translate_text(merged, lang, mode, key)
|
| 288 |
+
return merged, trans
|
| 289 |
|
| 290 |
+
# 回车提交
|
| 291 |
user_prompt.submit(
|
| 292 |
fn=on_generate,
|
| 293 |
inputs=[user_prompt, gender_option, top_cat_dd, sub_cat_dd, species_dd, api_mode, api_key, translate_lang],
|
| 294 |
+
outputs=[final_output, translated_result]
|
| 295 |
)
|
| 296 |
+
# 点击按钮
|
| 297 |
+
btn = gr.Button("生成 / Generate")
|
| 298 |
+
btn.click(
|
| 299 |
fn=on_generate,
|
| 300 |
inputs=[user_prompt, gender_option, top_cat_dd, sub_cat_dd, species_dd, api_mode, api_key, translate_lang],
|
| 301 |
+
outputs=[final_output, translated_result]
|
| 302 |
)
|
| 303 |
|
| 304 |
return demo
|
| 305 |
|
| 306 |
+
|
| 307 |
if __name__ == "__main__":
|
| 308 |
demo = build_interface()
|
| 309 |
demo.launch()
|