Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,22 +4,28 @@ import gradio as gr
|
|
4 |
from openai import OpenAI
|
5 |
|
6 |
##############################################################################
|
7 |
-
# 1.
|
8 |
##############################################################################
|
9 |
try:
|
10 |
-
with open("furry_species.json", "r", encoding="utf-8") as
|
11 |
-
FURRY_DATA = json.load(
|
12 |
except:
|
13 |
FURRY_DATA = {}
|
14 |
|
15 |
try:
|
16 |
-
with open("gender_rules.json", "r", encoding="utf-8") as
|
17 |
-
GENDER_RULES = json.load(
|
18 |
except:
|
19 |
GENDER_RULES = {}
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
##############################################################################
|
22 |
-
# 2.
|
23 |
##############################################################################
|
24 |
def get_top_categories(furry_data):
|
25 |
return sorted(list(furry_data.keys()))
|
@@ -35,50 +41,58 @@ def get_species_list(furry_data, top_category, sub_category):
|
|
35 |
return []
|
36 |
|
37 |
##############################################################################
|
38 |
-
# 3.
|
39 |
##############################################################################
|
40 |
-
def
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
api_mode, api_key
|
45 |
-
):
|
46 |
"""
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
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"
|
82 |
else:
|
83 |
base_url = "https://api.deepseek.com"
|
84 |
model_name = "deepseek-chat"
|
@@ -87,26 +101,46 @@ def generate_transformed_output(
|
|
87 |
if base_url:
|
88 |
client.base_url = base_url
|
89 |
|
90 |
-
#
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
system_prompt = f"""
|
93 |
-
You are a creative assistant that
|
94 |
-
to reflect
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
|
|
|
|
110 |
""".strip()
|
111 |
|
112 |
try:
|
@@ -114,20 +148,14 @@ Steps:
|
|
114 |
model=model_name,
|
115 |
messages=[
|
116 |
{"role": "system", "content": system_prompt},
|
117 |
-
{"role": "user", "content": "Generate
|
118 |
],
|
119 |
)
|
120 |
-
|
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."
|
133 |
if not content.strip():
|
@@ -146,8 +174,8 @@ def translate_text(content, lang, api_mode, api_key):
|
|
146 |
|
147 |
translate_system_prompt = f"""
|
148 |
You are a translator. Translate the following text to {lang},
|
149 |
-
|
150 |
-
|
151 |
""".strip()
|
152 |
|
153 |
try:
|
@@ -155,7 +183,7 @@ Do not add extra headings.
|
|
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,30 +191,27 @@ Do not add extra headings.
|
|
163 |
return f"{api_mode} translation failed. Error: {e}"
|
164 |
|
165 |
##############################################################################
|
166 |
-
#
|
167 |
##############################################################################
|
168 |
def build_interface():
|
169 |
with gr.Blocks() as demo:
|
170 |
-
gr.Markdown("## Prompt Transformer
|
171 |
|
172 |
with gr.Row():
|
173 |
with gr.Column():
|
174 |
-
# 选择 GPT/DeepSeek
|
175 |
api_mode = gr.Radio(
|
176 |
-
label="
|
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="
|
185 |
)
|
186 |
|
187 |
-
# 性别/Furry
|
188 |
gender_option = gr.Radio(
|
189 |
-
label="Gender / Furry
|
190 |
choices=[
|
191 |
"Trans_to_Male",
|
192 |
"Trans_to_Female",
|
@@ -197,7 +222,6 @@ def build_interface():
|
|
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),
|
@@ -217,9 +241,8 @@ def build_interface():
|
|
217 |
visible=False
|
218 |
)
|
219 |
|
220 |
-
|
221 |
-
|
222 |
-
if chosen == "Trans_to_Furry":
|
223 |
return (gr.update(visible=True),
|
224 |
gr.update(visible=True),
|
225 |
gr.update(visible=True))
|
@@ -227,83 +250,77 @@ def build_interface():
|
|
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="
|
260 |
-
lines=5
|
261 |
-
placeholder="Your Prompts"
|
262 |
)
|
263 |
final_output = gr.Textbox(
|
264 |
-
label="(
|
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 |
-
|
276 |
-
label="
|
277 |
lines=10
|
278 |
)
|
279 |
|
280 |
######################################################################
|
281 |
-
#
|
282 |
######################################################################
|
283 |
-
def on_generate(prompt, gender, tc, sc,
|
284 |
-
|
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,
|
295 |
)
|
296 |
-
|
297 |
-
|
298 |
-
|
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,
|
302 |
)
|
303 |
|
304 |
return demo
|
305 |
|
306 |
-
|
307 |
if __name__ == "__main__":
|
308 |
demo = build_interface()
|
309 |
demo.launch()
|
|
|
4 |
from openai import OpenAI
|
5 |
|
6 |
##############################################################################
|
7 |
+
# 1. 读取外部文件
|
8 |
##############################################################################
|
9 |
try:
|
10 |
+
with open("furry_species.json", "r", encoding="utf-8") as ff:
|
11 |
+
FURRY_DATA = json.load(ff)
|
12 |
except:
|
13 |
FURRY_DATA = {}
|
14 |
|
15 |
try:
|
16 |
+
with open("gender_rules.json", "r", encoding="utf-8") as gf:
|
17 |
+
GENDER_RULES = json.load(gf)
|
18 |
except:
|
19 |
GENDER_RULES = {}
|
20 |
|
21 |
+
try:
|
22 |
+
with open("transform_rules.json", "r", encoding="utf-8") as tf:
|
23 |
+
TRANSFORM_DICT = json.load(tf)
|
24 |
+
except:
|
25 |
+
TRANSFORM_DICT = {}
|
26 |
+
|
27 |
##############################################################################
|
28 |
+
# 2. 多级菜单函数
|
29 |
##############################################################################
|
30 |
def get_top_categories(furry_data):
|
31 |
return sorted(list(furry_data.keys()))
|
|
|
41 |
return []
|
42 |
|
43 |
##############################################################################
|
44 |
+
# 3. 合并规则文本
|
45 |
##############################################################################
|
46 |
+
def merge_transform_rules_into_prompt(rules_json):
|
47 |
+
"""
|
48 |
+
将 transform_rules.json 中的相关字段转为统一文本,便于放到 system_prompt。
|
49 |
+
你也可以分段加入。
|
|
|
|
|
50 |
"""
|
51 |
+
if not rules_json:
|
52 |
+
return "(No transform rules loaded)"
|
53 |
+
|
54 |
+
# 1) 读取 gender_transform
|
55 |
+
gt = rules_json.get("gender_transform", {})
|
56 |
+
male_tag_rules = gt.get("male_tag_rules", {})
|
57 |
+
replacements = gt.get("tag_replacements", {})
|
58 |
+
additional = gt.get("additional_reminders", {})
|
59 |
+
|
60 |
+
# 2) shared_preferences
|
61 |
+
sp = rules_json.get("shared_preferences", {})
|
62 |
+
|
63 |
+
# 3) table_details
|
64 |
+
td = rules_json.get("table_details", {})
|
65 |
+
|
66 |
+
# 这只是一个简单示例,将 key-value 拼起来,真实项目可更精细
|
67 |
+
text_parts = []
|
68 |
+
|
69 |
+
text_parts.append("==== GENDER TRANSFORM RULES ====")
|
70 |
+
text_parts.append(str(gt)) # 直接转为字符串或更有条理地拼写
|
71 |
+
|
72 |
+
text_parts.append("==== SHARED PREFERENCES ====")
|
73 |
+
text_parts.append(str(sp))
|
74 |
+
|
75 |
+
text_parts.append("==== TABLE DETAILS (PRO ACTIONS) ====")
|
76 |
+
text_parts.append(str(td))
|
77 |
+
|
78 |
+
return "\n".join(text_parts)
|
79 |
+
|
80 |
+
RULES_TEXT_FULL = merge_transform_rules_into_prompt(TRANSFORM_DICT)
|
81 |
|
82 |
+
##############################################################################
|
83 |
+
# 4. 核心 GPT/DeepSeek 调用
|
84 |
+
##############################################################################
|
85 |
+
def generate_transformed_output(prompt, gender_option, top_cat, sub_cat, species_item, api_mode, api_key):
|
86 |
+
"""
|
87 |
+
读取 transform_rules.json / GENDER_RULES / FurryData:
|
88 |
+
只输出两段:(tags)\n\n(description)
|
89 |
"""
|
90 |
if not api_key:
|
91 |
return "Error: No API Key provided."
|
92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
if api_mode == "GPT":
|
94 |
base_url = None
|
95 |
+
model_name = "gpt-3.5-turbo"
|
96 |
else:
|
97 |
base_url = "https://api.deepseek.com"
|
98 |
model_name = "deepseek-chat"
|
|
|
101 |
if base_url:
|
102 |
client.base_url = base_url
|
103 |
|
104 |
+
# 如果选 Furry:
|
105 |
+
if gender_option == "Trans_to_Furry":
|
106 |
+
# 在 system prompt 内也可写点提示:
|
107 |
+
furry_path = f"{top_cat} > {sub_cat} > {species_item}" if (top_cat and sub_cat and species_item) else "unknown"
|
108 |
+
extra_line = f"\nFurry chosen: {furry_path}\n"
|
109 |
+
else:
|
110 |
+
extra_line = ""
|
111 |
+
|
112 |
+
# 这里把 "RULES_TEXT_FULL" (来自 transform_rules.json) + GENDER_RULES +
|
113 |
+
# "extra_line" 一并放到 system_prompt
|
114 |
+
gender_specific_rule = ""
|
115 |
+
if gender_option == "Trans_to_Male":
|
116 |
+
gender_specific_rule = GENDER_RULES.get("male", "")
|
117 |
+
elif gender_option == "Trans_to_Female":
|
118 |
+
gender_specific_rule = GENDER_RULES.get("female", "")
|
119 |
+
elif gender_option == "Trans_to_Mannequin":
|
120 |
+
gender_specific_rule = GENDER_RULES.get("genderless", "")
|
121 |
+
elif gender_option == "Trans_to_Intersex":
|
122 |
+
gender_specific_rule = GENDER_RULES.get("intersex", "")
|
123 |
+
|
124 |
system_prompt = f"""
|
125 |
+
You are a creative assistant that transforms the user's base prompt
|
126 |
+
to reflect correct gender/furry transformations. Follow these references:
|
127 |
+
|
128 |
+
1) Detailed Transform Rules (transform_rules.json):
|
129 |
+
{RULES_TEXT_FULL}
|
130 |
+
|
131 |
+
2) Additional short gender rules (gender_rules.json):
|
132 |
+
{gender_specific_rule}
|
133 |
+
|
134 |
+
{extra_line}
|
135 |
+
Instructions:
|
136 |
+
- Original prompt tags: {prompt}
|
137 |
+
- Convert them into NEW combined tags, removing or replacing conflicting ones.
|
138 |
+
- Only output two parts:
|
139 |
+
1) One line of final tags in parentheses, e.g. (male, short hair, dynamic pose, ...)
|
140 |
+
2) A blank line.
|
141 |
+
3) Then 3~6 sentences of imaginative scene description in English.
|
142 |
+
- No extra lines, no headings, no 'gender:' or 'base_prompt:'.
|
143 |
+
- End of instructions.
|
144 |
""".strip()
|
145 |
|
146 |
try:
|
|
|
148 |
model=model_name,
|
149 |
messages=[
|
150 |
{"role": "system", "content": system_prompt},
|
151 |
+
{"role": "user", "content": "Generate final tags and description now."}
|
152 |
],
|
153 |
)
|
154 |
+
return resp.choices[0].message.content.strip()
|
|
|
|
|
|
|
155 |
except Exception as e:
|
156 |
return f"{api_mode} generation failed. Error: {e}"
|
157 |
|
158 |
def translate_text(content, lang, api_mode, api_key):
|
|
|
|
|
|
|
159 |
if not api_key:
|
160 |
return "Error: No API Key provided."
|
161 |
if not content.strip():
|
|
|
174 |
|
175 |
translate_system_prompt = f"""
|
176 |
You are a translator. Translate the following text to {lang},
|
177 |
+
keeping parentheses line and blank line if present.
|
178 |
+
No extra headings.
|
179 |
""".strip()
|
180 |
|
181 |
try:
|
|
|
183 |
model=model_name,
|
184 |
messages=[
|
185 |
{"role": "system", "content": translate_system_prompt},
|
186 |
+
{"role": "user", "content": content}
|
187 |
],
|
188 |
)
|
189 |
return resp.choices[0].message.content.strip()
|
|
|
191 |
return f"{api_mode} translation failed. Error: {e}"
|
192 |
|
193 |
##############################################################################
|
194 |
+
# 5. Gradio 界面
|
195 |
##############################################################################
|
196 |
def build_interface():
|
197 |
with gr.Blocks() as demo:
|
198 |
+
gr.Markdown("## Prompt Transformer - (male/female/furry) & Scene Design with transform_rules.json")
|
199 |
|
200 |
with gr.Row():
|
201 |
with gr.Column():
|
|
|
202 |
api_mode = gr.Radio(
|
203 |
+
label="Select API (GPT/DeepSeek)",
|
204 |
choices=["GPT", "DeepSeek"],
|
205 |
value="GPT"
|
206 |
)
|
|
|
207 |
api_key = gr.Textbox(
|
208 |
+
label="API Key",
|
209 |
type="password",
|
210 |
+
placeholder="Input your GPT or DeepSeek Key"
|
211 |
)
|
212 |
|
|
|
213 |
gender_option = gr.Radio(
|
214 |
+
label="Gender / Furry Option",
|
215 |
choices=[
|
216 |
"Trans_to_Male",
|
217 |
"Trans_to_Female",
|
|
|
222 |
value="Trans_to_Male"
|
223 |
)
|
224 |
|
|
|
225 |
top_cat_dd = gr.Dropdown(
|
226 |
label="Furry: Top Category",
|
227 |
choices=get_top_categories(FURRY_DATA),
|
|
|
241 |
visible=False
|
242 |
)
|
243 |
|
244 |
+
def show_furry_options(opt):
|
245 |
+
if opt == "Trans_to_Furry":
|
|
|
246 |
return (gr.update(visible=True),
|
247 |
gr.update(visible=True),
|
248 |
gr.update(visible=True))
|
|
|
250 |
return (gr.update(visible=False),
|
251 |
gr.update(visible=False),
|
252 |
gr.update(visible=False))
|
253 |
+
|
254 |
gender_option.change(
|
255 |
fn=show_furry_options,
|
256 |
inputs=[gender_option],
|
257 |
outputs=[top_cat_dd, sub_cat_dd, species_dd]
|
258 |
)
|
259 |
|
|
|
260 |
def on_top_cat_select(selected):
|
261 |
subs = get_sub_categories(FURRY_DATA, selected)
|
262 |
return gr.update(choices=subs, value=None)
|
263 |
+
|
264 |
top_cat_dd.change(
|
265 |
fn=on_top_cat_select,
|
266 |
inputs=[top_cat_dd],
|
267 |
outputs=[sub_cat_dd]
|
268 |
)
|
269 |
|
|
|
270 |
def on_sub_cat_select(top_c, sub_c):
|
271 |
sp = get_species_list(FURRY_DATA, top_c, sub_c)
|
272 |
return gr.update(choices=sp, value=None)
|
273 |
+
|
274 |
sub_cat_dd.change(
|
275 |
fn=on_sub_cat_select,
|
276 |
inputs=[top_cat_dd, sub_cat_dd],
|
277 |
outputs=[species_dd]
|
278 |
)
|
279 |
|
|
|
280 |
with gr.Column():
|
281 |
user_prompt = gr.Textbox(
|
282 |
+
label="Original Prompt (e.g. 1girl, butterfly, solo, ...)",
|
283 |
+
lines=5
|
|
|
284 |
)
|
285 |
final_output = gr.Textbox(
|
286 |
+
label="Transformed Output (tags + description)",
|
287 |
lines=10
|
288 |
)
|
289 |
|
|
|
290 |
with gr.Row():
|
291 |
translate_lang = gr.Dropdown(
|
292 |
+
label="Translate to Language",
|
293 |
choices=["English", "Chinese", "Japanese", "French", "German", "Spanish"],
|
294 |
value="English"
|
295 |
)
|
296 |
+
translated_text = gr.Textbox(
|
297 |
+
label="Translated Result",
|
298 |
lines=10
|
299 |
)
|
300 |
|
301 |
######################################################################
|
302 |
+
# 生成
|
303 |
######################################################################
|
304 |
+
def on_generate(prompt, gender, tc, sc, spc, mode, key, lang):
|
305 |
+
merged = generate_transformed_output(prompt, gender, tc, sc, spc, mode, key)
|
|
|
|
|
306 |
trans = translate_text(merged, lang, mode, key)
|
307 |
return merged, trans
|
308 |
|
|
|
309 |
user_prompt.submit(
|
310 |
fn=on_generate,
|
311 |
inputs=[user_prompt, gender_option, top_cat_dd, sub_cat_dd, species_dd, api_mode, api_key, translate_lang],
|
312 |
+
outputs=[final_output, translated_text]
|
313 |
)
|
314 |
+
|
315 |
+
gen_btn = gr.Button("Generate")
|
316 |
+
gen_btn.click(
|
317 |
fn=on_generate,
|
318 |
inputs=[user_prompt, gender_option, top_cat_dd, sub_cat_dd, species_dd, api_mode, api_key, translate_lang],
|
319 |
+
outputs=[final_output, translated_text]
|
320 |
)
|
321 |
|
322 |
return demo
|
323 |
|
|
|
324 |
if __name__ == "__main__":
|
325 |
demo = build_interface()
|
326 |
demo.launch()
|