Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -46,28 +46,23 @@ def get_species_list(furry_data, top_category, sub_category):
|
|
46 |
##############################################################################
|
47 |
def merge_transform_rules_into_prompt(rules_json):
|
48 |
"""
|
49 |
-
将 transform_rules.json
|
50 |
-
|
51 |
"""
|
52 |
if not rules_json:
|
53 |
return "(No transform rules loaded)"
|
54 |
|
55 |
-
# 1) 读取 gender_transform
|
56 |
gt = rules_json.get("gender_transform", {})
|
57 |
-
|
58 |
-
|
59 |
-
text_parts = []
|
60 |
|
|
|
61 |
text_parts.append("==== GENDER TRANSFORM RULES ====")
|
62 |
-
text_parts.append(str(gt))
|
63 |
|
64 |
-
# 2) shared_preferences
|
65 |
-
sp = rules_json.get("shared_preferences", {})
|
66 |
text_parts.append("==== SHARED PREFERENCES ====")
|
67 |
text_parts.append(str(sp))
|
68 |
|
69 |
-
# 3) table_details
|
70 |
-
td = rules_json.get("table_details", {})
|
71 |
text_parts.append("==== TABLE DETAILS (PRO ACTIONS) ====")
|
72 |
text_parts.append(str(td))
|
73 |
|
@@ -76,33 +71,33 @@ def merge_transform_rules_into_prompt(rules_json):
|
|
76 |
RULES_TEXT_FULL = merge_transform_rules_into_prompt(TRANSFORM_DICT)
|
77 |
|
78 |
##############################################################################
|
79 |
-
# 4.
|
80 |
##############################################################################
|
81 |
|
82 |
-
#
|
83 |
transform_map = {
|
84 |
"Trans_to_Male": "female_to_male",
|
85 |
"Trans_to_Female": "male_to_female",
|
86 |
"Trans_to_Mannequin": "any_to_genderless",
|
87 |
"Trans_to_Intersex": "any_to_intersex",
|
88 |
-
"Trans_to_Furry": "trans_to_furry"
|
89 |
}
|
90 |
|
91 |
def forced_replace(prompt, direction):
|
92 |
"""
|
93 |
-
|
94 |
-
|
95 |
"""
|
96 |
-
|
|
|
|
|
97 |
override_section = TRANSFORM_DICT.get("override_conflicting_descriptors", {})
|
98 |
replacements = override_section.get(direction, {})
|
99 |
if not replacements:
|
100 |
-
#
|
101 |
-
return prompt
|
102 |
|
103 |
-
# 逐条用正则整词替换
|
104 |
for old, new in replacements.items():
|
105 |
-
#
|
106 |
pattern = r"(?i)\b" + re.escape(old) + r"\b"
|
107 |
prompt = re.sub(pattern, new, prompt)
|
108 |
return prompt
|
@@ -112,8 +107,7 @@ def forced_replace(prompt, direction):
|
|
112 |
##############################################################################
|
113 |
def generate_transformed_output(prompt, gender_option, top_cat, sub_cat, species_item, api_mode, api_key):
|
114 |
"""
|
115 |
-
|
116 |
-
只输出两段:(tags)\n\n(description)
|
117 |
"""
|
118 |
if not api_key:
|
119 |
return "Error: No API Key provided."
|
@@ -129,14 +123,14 @@ def generate_transformed_output(prompt, gender_option, top_cat, sub_cat, species
|
|
129 |
if base_url:
|
130 |
client.base_url = base_url
|
131 |
|
132 |
-
#
|
133 |
if gender_option == "Trans_to_Furry":
|
134 |
furry_path = f"{top_cat} > {sub_cat} > {species_item}" if (top_cat and sub_cat and species_item) else "unknown"
|
135 |
extra_line = f"\nFurry chosen: {furry_path}\n"
|
136 |
else:
|
137 |
extra_line = ""
|
138 |
|
139 |
-
# 根据
|
140 |
gender_specific_rule = ""
|
141 |
if gender_option == "Trans_to_Male":
|
142 |
gender_specific_rule = GENDER_RULES.get("male", "")
|
@@ -147,6 +141,7 @@ def generate_transformed_output(prompt, gender_option, top_cat, sub_cat, species
|
|
147 |
elif gender_option == "Trans_to_Intersex":
|
148 |
gender_specific_rule = GENDER_RULES.get("intersex", "")
|
149 |
|
|
|
150 |
system_prompt = f"""
|
151 |
You are a creative assistant that transforms the user's base prompt
|
152 |
to reflect correct gender/furry transformations. Follow these references:
|
@@ -174,11 +169,10 @@ Instructions:
|
|
174 |
model=model_name,
|
175 |
messages=[
|
176 |
{"role": "system", "content": system_prompt},
|
177 |
-
{"role": "user",
|
178 |
],
|
179 |
)
|
180 |
return resp.choices[0].message.content.strip()
|
181 |
-
|
182 |
except Exception as e:
|
183 |
return f"{api_mode} generation failed. Error: {e}"
|
184 |
|
@@ -186,6 +180,9 @@ Instructions:
|
|
186 |
# 6. 翻译函数
|
187 |
##############################################################################
|
188 |
def translate_text(content, lang, api_mode, api_key):
|
|
|
|
|
|
|
189 |
if not api_key:
|
190 |
return "Error: No API Key provided."
|
191 |
if not content.strip():
|
@@ -320,8 +317,10 @@ def build_interface():
|
|
320 |
with gr.Row():
|
321 |
translate_lang = gr.Dropdown(
|
322 |
label="Translate to Language 翻译语言",
|
323 |
-
choices=[
|
324 |
-
|
|
|
|
|
325 |
value="English"
|
326 |
)
|
327 |
translated_text = gr.Textbox(
|
@@ -333,15 +332,14 @@ def build_interface():
|
|
333 |
# 生成
|
334 |
######################################################################
|
335 |
def on_generate(prompt, gender, tc, sc, spc, mode, key, lang):
|
336 |
-
# 1)
|
337 |
direction = transform_map.get(gender, None)
|
338 |
if direction:
|
339 |
-
#
|
340 |
prompt = forced_replace(prompt, direction)
|
341 |
|
342 |
-
# 2)
|
343 |
merged = generate_transformed_output(prompt, gender, tc, sc, spc, mode, key)
|
344 |
-
|
345 |
# 3) 翻译
|
346 |
trans = translate_text(merged, lang, mode, key)
|
347 |
return merged, trans
|
|
|
46 |
##############################################################################
|
47 |
def merge_transform_rules_into_prompt(rules_json):
|
48 |
"""
|
49 |
+
将 transform_rules.json 中的相关字段转为统一文本,
|
50 |
+
供 system_prompt 里参考。
|
51 |
"""
|
52 |
if not rules_json:
|
53 |
return "(No transform rules loaded)"
|
54 |
|
|
|
55 |
gt = rules_json.get("gender_transform", {})
|
56 |
+
sp = rules_json.get("shared_preferences", {})
|
57 |
+
td = rules_json.get("table_details", {})
|
|
|
58 |
|
59 |
+
text_parts = []
|
60 |
text_parts.append("==== GENDER TRANSFORM RULES ====")
|
61 |
+
text_parts.append(str(gt))
|
62 |
|
|
|
|
|
63 |
text_parts.append("==== SHARED PREFERENCES ====")
|
64 |
text_parts.append(str(sp))
|
65 |
|
|
|
|
|
66 |
text_parts.append("==== TABLE DETAILS (PRO ACTIONS) ====")
|
67 |
text_parts.append(str(td))
|
68 |
|
|
|
71 |
RULES_TEXT_FULL = merge_transform_rules_into_prompt(TRANSFORM_DICT)
|
72 |
|
73 |
##############################################################################
|
74 |
+
# 4. 强制替换:根据 override_conflicting_descriptors
|
75 |
##############################################################################
|
76 |
|
77 |
+
# 前端选项 -> transform_rules.json 里的 override_conflicting_descriptors key
|
78 |
transform_map = {
|
79 |
"Trans_to_Male": "female_to_male",
|
80 |
"Trans_to_Female": "male_to_female",
|
81 |
"Trans_to_Mannequin": "any_to_genderless",
|
82 |
"Trans_to_Intersex": "any_to_intersex",
|
83 |
+
"Trans_to_Furry": "trans_to_furry"
|
84 |
}
|
85 |
|
86 |
def forced_replace(prompt, direction):
|
87 |
"""
|
88 |
+
读取 transform_rules.json["override_conflicting_descriptors"][direction] 的键值,
|
89 |
+
用正则整词替换 prompt 中出现的 old->new。
|
90 |
"""
|
91 |
+
if not TRANSFORM_DICT:
|
92 |
+
return prompt
|
93 |
+
|
94 |
override_section = TRANSFORM_DICT.get("override_conflicting_descriptors", {})
|
95 |
replacements = override_section.get(direction, {})
|
96 |
if not replacements:
|
97 |
+
return prompt # 该方向没有映射表,直接返回
|
|
|
98 |
|
|
|
99 |
for old, new in replacements.items():
|
100 |
+
# (?i)不分大小写, \b表示单词边界
|
101 |
pattern = r"(?i)\b" + re.escape(old) + r"\b"
|
102 |
prompt = re.sub(pattern, new, prompt)
|
103 |
return prompt
|
|
|
107 |
##############################################################################
|
108 |
def generate_transformed_output(prompt, gender_option, top_cat, sub_cat, species_item, api_mode, api_key):
|
109 |
"""
|
110 |
+
最终在 GPT/DeepSeek 中生成 (tags)\n\n(description)。
|
|
|
111 |
"""
|
112 |
if not api_key:
|
113 |
return "Error: No API Key provided."
|
|
|
123 |
if base_url:
|
124 |
client.base_url = base_url
|
125 |
|
126 |
+
# 如果用户选 Furry, 记录一下当前选到的物种路径
|
127 |
if gender_option == "Trans_to_Furry":
|
128 |
furry_path = f"{top_cat} > {sub_cat} > {species_item}" if (top_cat and sub_cat and species_item) else "unknown"
|
129 |
extra_line = f"\nFurry chosen: {furry_path}\n"
|
130 |
else:
|
131 |
extra_line = ""
|
132 |
|
133 |
+
# 根据 user 选择加载 gender_rules.json 里的东西
|
134 |
gender_specific_rule = ""
|
135 |
if gender_option == "Trans_to_Male":
|
136 |
gender_specific_rule = GENDER_RULES.get("male", "")
|
|
|
141 |
elif gender_option == "Trans_to_Intersex":
|
142 |
gender_specific_rule = GENDER_RULES.get("intersex", "")
|
143 |
|
144 |
+
# 组装 System Prompt
|
145 |
system_prompt = f"""
|
146 |
You are a creative assistant that transforms the user's base prompt
|
147 |
to reflect correct gender/furry transformations. Follow these references:
|
|
|
169 |
model=model_name,
|
170 |
messages=[
|
171 |
{"role": "system", "content": system_prompt},
|
172 |
+
{"role": "user", "content": "Generate final tags and description now."}
|
173 |
],
|
174 |
)
|
175 |
return resp.choices[0].message.content.strip()
|
|
|
176 |
except Exception as e:
|
177 |
return f"{api_mode} generation failed. Error: {e}"
|
178 |
|
|
|
180 |
# 6. 翻译函数
|
181 |
##############################################################################
|
182 |
def translate_text(content, lang, api_mode, api_key):
|
183 |
+
"""
|
184 |
+
后期翻译, 不更改上方生成逻辑.
|
185 |
+
"""
|
186 |
if not api_key:
|
187 |
return "Error: No API Key provided."
|
188 |
if not content.strip():
|
|
|
317 |
with gr.Row():
|
318 |
translate_lang = gr.Dropdown(
|
319 |
label="Translate to Language 翻译语言",
|
320 |
+
choices=[
|
321 |
+
"English", "Chinese", "Japanese", "French", "German",
|
322 |
+
"Italian", "Spanish", "Russian", "Dutch", "Persian", "Arabic", "Thai"
|
323 |
+
],
|
324 |
value="English"
|
325 |
)
|
326 |
translated_text = gr.Textbox(
|
|
|
332 |
# 生成
|
333 |
######################################################################
|
334 |
def on_generate(prompt, gender, tc, sc, spc, mode, key, lang):
|
335 |
+
# 1) 找到 override_conflicting_descriptors 的方向
|
336 |
direction = transform_map.get(gender, None)
|
337 |
if direction:
|
338 |
+
# 先做强制替换
|
339 |
prompt = forced_replace(prompt, direction)
|
340 |
|
341 |
+
# 2) 再执行原先逻辑
|
342 |
merged = generate_transformed_output(prompt, gender, tc, sc, spc, mode, key)
|
|
|
343 |
# 3) 翻译
|
344 |
trans = translate_text(merged, lang, mode, key)
|
345 |
return merged, trans
|