PSNbst commited on
Commit
0f7a8f6
·
verified ·
1 Parent(s): 81a32a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -28
app.py CHANGED
@@ -45,10 +45,6 @@ def get_species_list(furry_data, top_category, sub_category):
45
  # 3. 合并规则文本
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
 
@@ -74,7 +70,6 @@ RULES_TEXT_FULL = merge_transform_rules_into_prompt(TRANSFORM_DICT)
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",
@@ -84,20 +79,15 @@ transform_map = {
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
@@ -106,15 +96,12 @@ def forced_replace(prompt, direction):
106
  # 5. 核心 GPT/DeepSeek 调用
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."
114
 
115
  if api_mode == "GPT":
116
  base_url = None
117
- model_name = "gpt-3.5-turbo"
118
  else:
119
  base_url = "https://api.deepseek.com"
120
  model_name = "deepseek-chat"
@@ -123,14 +110,12 @@ def generate_transformed_output(prompt, gender_option, top_cat, sub_cat, species
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,10 +126,9 @@ def generate_transformed_output(prompt, gender_option, top_cat, sub_cat, species
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:
148
 
149
  1) Detailed Transform Rules (transform_rules.json):
150
  {RULES_TEXT_FULL}
@@ -180,9 +164,6 @@ Instructions:
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():
@@ -335,14 +316,16 @@ def build_interface():
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
 
 
346
 
347
  user_prompt.submit(
348
  fn=on_generate,
 
45
  # 3. 合并规则文本
46
  ##############################################################################
47
  def merge_transform_rules_into_prompt(rules_json):
 
 
 
 
48
  if not rules_json:
49
  return "(No transform rules loaded)"
50
 
 
70
  # 4. 强制替换:根据 override_conflicting_descriptors
71
  ##############################################################################
72
 
 
73
  transform_map = {
74
  "Trans_to_Male": "female_to_male",
75
  "Trans_to_Female": "male_to_female",
 
79
  }
80
 
81
  def forced_replace(prompt, direction):
 
 
 
 
82
  if not TRANSFORM_DICT:
83
  return prompt
84
 
85
  override_section = TRANSFORM_DICT.get("override_conflicting_descriptors", {})
86
  replacements = override_section.get(direction, {})
87
  if not replacements:
88
+ return prompt
89
 
90
  for old, new in replacements.items():
 
91
  pattern = r"(?i)\b" + re.escape(old) + r"\b"
92
  prompt = re.sub(pattern, new, prompt)
93
  return prompt
 
96
  # 5. 核心 GPT/DeepSeek 调用
97
  ##############################################################################
98
  def generate_transformed_output(prompt, gender_option, top_cat, sub_cat, species_item, api_mode, api_key):
 
 
 
99
  if not api_key:
100
  return "Error: No API Key provided."
101
 
102
  if api_mode == "GPT":
103
  base_url = None
104
+ model_name = "gpt-4o"
105
  else:
106
  base_url = "https://api.deepseek.com"
107
  model_name = "deepseek-chat"
 
110
  if base_url:
111
  client.base_url = base_url
112
 
 
113
  if gender_option == "Trans_to_Furry":
114
  furry_path = f"{top_cat} > {sub_cat} > {species_item}" if (top_cat and sub_cat and species_item) else "unknown"
115
  extra_line = f"\nFurry chosen: {furry_path}\n"
116
  else:
117
  extra_line = ""
118
 
 
119
  gender_specific_rule = ""
120
  if gender_option == "Trans_to_Male":
121
  gender_specific_rule = GENDER_RULES.get("male", "")
 
126
  elif gender_option == "Trans_to_Intersex":
127
  gender_specific_rule = GENDER_RULES.get("intersex", "")
128
 
 
129
  system_prompt = f"""
130
  You are a creative assistant that transforms the user's base prompt
131
+ to reflect correct gender/furry/genderless/intersex transformations. Follow these references:
132
 
133
  1) Detailed Transform Rules (transform_rules.json):
134
  {RULES_TEXT_FULL}
 
164
  # 6. 翻译函数
165
  ##############################################################################
166
  def translate_text(content, lang, api_mode, api_key):
 
 
 
167
  if not api_key:
168
  return "Error: No API Key provided."
169
  if not content.strip():
 
316
  # 1) 找到 override_conflicting_descriptors 的方向
317
  direction = transform_map.get(gender, None)
318
  if direction:
319
+ # 强制替换用户输入文本
320
  prompt = forced_replace(prompt, direction)
321
 
322
+ # 2) 提交到 GPT/DeepSeek 进行生成
323
+ merged_output = generate_transformed_output(prompt, gender, tc, sc, spc, mode, key)
324
+
325
+ # 3) 翻译生成的结果
326
+ translated_output = translate_text(merged_output, lang, mode, key)
327
+
328
+ return merged_output, translated_output
329
 
330
  user_prompt.submit(
331
  fn=on_generate,