Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,138 +4,151 @@
|
|
4 |
import os
|
5 |
import json
|
6 |
import gradio as gr
|
7 |
-
import requests
|
8 |
-
|
9 |
-
# 新版 openai>=1.0.0
|
10 |
from openai import OpenAI
|
11 |
|
12 |
##############################################################################
|
13 |
-
# 1.
|
14 |
##############################################################################
|
15 |
-
|
16 |
-
# 读取 Furry 物种分类的 JSON 文件
|
17 |
-
# 假设文件名为 furry_species.json,结构示例:
|
18 |
-
# {
|
19 |
-
# "CANIDS": ["Dogs", "Wolves", "Foxes"],
|
20 |
-
# "FELINES": ["Lions", "Tigers", "Cheetahs"],
|
21 |
-
# ...
|
22 |
-
# }
|
23 |
try:
|
24 |
-
with open("furry_species.json", "r", encoding="utf-8") as
|
25 |
-
|
26 |
except:
|
27 |
-
|
28 |
-
|
29 |
-
def flatten_furry_species(map_data):
|
30 |
-
"""
|
31 |
-
将 {分类: [物种列表]} 展开为 ["分类 - 物种"] 形式的扁平列表
|
32 |
-
"""
|
33 |
-
result = []
|
34 |
-
for category, species_list in map_data.items():
|
35 |
-
for sp in species_list:
|
36 |
-
result.append(f"{category} - {sp}")
|
37 |
-
return sorted(result)
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
# 读取 Gender 细节长文本 (记忆库)
|
42 |
try:
|
43 |
-
with open("
|
44 |
-
|
45 |
except:
|
46 |
-
|
47 |
-
"Gender conversion rules are missing. Please provide gender_details.txt."
|
48 |
-
)
|
49 |
|
50 |
##############################################################################
|
51 |
-
# 2.
|
52 |
##############################################################################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
-
def
|
55 |
"""
|
56 |
-
|
57 |
-
|
58 |
-
3) 返回 (tags + 自然语言描述) 的输出形式。
|
59 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
if not api_key:
|
62 |
return "Error: No API Key provided."
|
63 |
-
|
64 |
-
#
|
65 |
tags = {}
|
66 |
if gender_option == "Trans_to_Male":
|
67 |
tags["gender"] = "male"
|
|
|
68 |
elif gender_option == "Trans_to_Female":
|
69 |
tags["gender"] = "female"
|
|
|
70 |
elif gender_option == "Trans_to_Mannequin":
|
71 |
tags["gender"] = "genderless"
|
|
|
72 |
elif gender_option == "Trans_to_Intersex":
|
73 |
tags["gender"] = "intersex"
|
74 |
-
|
|
|
|
|
75 |
tags["gender"] = "furry"
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
tags["base_prompt"] = prompt
|
79 |
|
80 |
-
#
|
81 |
if api_mode == "GPT":
|
82 |
base_url = None
|
83 |
-
model_name = "gpt-3.5-turbo"
|
84 |
else:
|
85 |
-
# DeepSeek
|
86 |
base_url = "https://api.deepseek.com"
|
87 |
model_name = "deepseek-chat"
|
88 |
|
89 |
-
# 创建 OpenAI Client
|
90 |
client = OpenAI(api_key=api_key)
|
91 |
if base_url:
|
92 |
client.base_url = base_url
|
93 |
|
94 |
-
# 将tags
|
95 |
tags_str = "\n".join([f"{k}: {v}" for k, v in tags.items() if v])
|
96 |
|
97 |
-
#
|
98 |
system_prompt = (
|
99 |
"You are a creative assistant that generates detailed and imaginative scene descriptions "
|
100 |
"for AI generation prompts. Focus on the details provided, incorporate them into a cohesive narrative, "
|
101 |
-
"and
|
102 |
-
f"{
|
103 |
-
"
|
104 |
)
|
105 |
|
|
|
106 |
try:
|
107 |
-
|
108 |
model=model_name,
|
109 |
messages=[
|
110 |
{"role": "system", "content": system_prompt},
|
111 |
{
|
112 |
"role": "user",
|
113 |
-
"content":
|
114 |
-
f"Here are the tags:\n{tags_str}\n"
|
115 |
-
f"Please generate a vivid, imaginative scene description."
|
116 |
-
),
|
117 |
},
|
118 |
],
|
119 |
)
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
output_text = f"=== Tags ===\n{tags_str}\n\n=== Description ===\n{description}"
|
124 |
-
return output_text
|
125 |
|
126 |
except Exception as e:
|
127 |
return f"{api_mode} generation failed. Error: {e}"
|
128 |
|
129 |
-
def translate_text(
|
130 |
"""
|
131 |
-
|
132 |
"""
|
133 |
if not api_key:
|
134 |
return "Error: No API Key provided."
|
135 |
-
if not
|
136 |
return ""
|
137 |
|
138 |
-
# GPT vs DeepSeek
|
139 |
if api_mode == "GPT":
|
140 |
base_url = None
|
141 |
model_name = "gpt-3.5-turbo"
|
@@ -147,40 +160,39 @@ def translate_text(text, translate_language, api_mode, api_key):
|
|
147 |
if base_url:
|
148 |
client.base_url = base_url
|
149 |
|
150 |
-
system_prompt = f"You are a
|
151 |
try:
|
152 |
resp = client.chat.completions.create(
|
153 |
model=model_name,
|
154 |
messages=[
|
155 |
{"role": "system", "content": system_prompt},
|
156 |
-
{"role": "user", "content":
|
157 |
-
]
|
158 |
)
|
159 |
return resp.choices[0].message.content.strip()
|
160 |
except Exception as e:
|
161 |
return f"{api_mode} translation failed. Error: {e}"
|
162 |
|
163 |
##############################################################################
|
164 |
-
#
|
165 |
##############################################################################
|
166 |
def build_interface():
|
167 |
with gr.Blocks() as demo:
|
168 |
-
gr.Markdown("## Prompt Transformer (GPT / DeepSeek)
|
169 |
|
170 |
with gr.Row():
|
171 |
with gr.Column():
|
172 |
-
# 选择 API
|
173 |
api_mode = gr.Radio(
|
174 |
label="选择API (GPT or DeepSeek)",
|
175 |
choices=["GPT", "DeepSeek"],
|
176 |
-
value="GPT"
|
177 |
)
|
178 |
api_key = gr.Textbox(
|
179 |
-
label="API
|
180 |
-
type="password"
|
181 |
-
placeholder="在此输入 GPT / DeepSeek 的API Key"
|
182 |
)
|
183 |
-
|
|
|
184 |
gender_option = gr.Radio(
|
185 |
label="转换目标",
|
186 |
choices=[
|
@@ -188,62 +200,110 @@ def build_interface():
|
|
188 |
"Trans_to_Female",
|
189 |
"Trans_to_Mannequin",
|
190 |
"Trans_to_Intersex",
|
191 |
-
"Trans_to_Furry"
|
192 |
],
|
193 |
-
value="Trans_to_Male"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
)
|
195 |
-
|
196 |
-
|
197 |
-
|
|
|
198 |
value=None,
|
199 |
visible=False
|
200 |
)
|
201 |
|
202 |
-
|
203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
|
205 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
|
207 |
with gr.Column():
|
208 |
user_prompt = gr.Textbox(
|
209 |
label="提示词 (Prompt)",
|
210 |
-
lines=4
|
211 |
-
placeholder="示例:一位穿着蓝色长裙的少女,坐在海边..."
|
212 |
)
|
213 |
-
|
214 |
label="(tags + 自然语言描述)",
|
215 |
lines=10
|
216 |
)
|
217 |
|
218 |
with gr.Row():
|
219 |
-
|
220 |
-
label="翻译语言
|
221 |
choices=["English", "Chinese", "Japanese", "French", "German", "Spanish"],
|
222 |
value="English"
|
223 |
)
|
224 |
-
|
225 |
label="翻译结果",
|
226 |
lines=10
|
227 |
)
|
228 |
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
#
|
234 |
-
|
235 |
-
|
|
|
|
|
236 |
|
237 |
user_prompt.submit(
|
238 |
fn=on_generate,
|
239 |
-
inputs=[user_prompt, gender_option,
|
240 |
-
outputs=[
|
241 |
)
|
242 |
-
|
243 |
-
|
|
|
244 |
fn=on_generate,
|
245 |
-
inputs=[user_prompt, gender_option,
|
246 |
-
outputs=[
|
247 |
)
|
248 |
|
249 |
return demo
|
|
|
4 |
import os
|
5 |
import json
|
6 |
import gradio as gr
|
|
|
|
|
|
|
7 |
from openai import OpenAI
|
8 |
|
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)
|
23 |
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. 调用逻辑:GPT 或 DeepSeek
|
56 |
+
##############################################################################
|
57 |
+
def generate_tags_and_description(prompt, gender_option, top_cat, sub_cat, species_item, api_mode, api_key):
|
58 |
+
"""
|
59 |
+
1) 构造 tags: gender, base_prompt, furry_species
|
60 |
+
2) 读取 gender_rules 并拼入 system prompt
|
61 |
+
3) 调用 GPT/DeepSeek
|
62 |
+
4) 输出 (tags + 自然语言描述)
|
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 |
tags["gender"] = "furry"
|
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 |
+
) # 或者只给一个简要 furry rule
|
90 |
+
|
91 |
+
# 选定物种
|
92 |
+
final_species = "unknown"
|
93 |
+
if top_cat and sub_cat and species_item:
|
94 |
+
final_species = f"{top_cat} > {sub_cat} > {species_item}"
|
95 |
+
tags["furry_species"] = final_species
|
96 |
+
|
97 |
+
# 原始提示词
|
98 |
tags["base_prompt"] = prompt
|
99 |
|
100 |
+
# BaseURL & 模型
|
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"
|
107 |
|
|
|
108 |
client = OpenAI(api_key=api_key)
|
109 |
if base_url:
|
110 |
client.base_url = base_url
|
111 |
|
112 |
+
# 将 tags 拼为字符串
|
113 |
tags_str = "\n".join([f"{k}: {v}" for k, v in tags.items() if v])
|
114 |
|
115 |
+
# system prompt 带上Gender Rules
|
116 |
system_prompt = (
|
117 |
"You are a creative assistant that generates detailed and imaginative scene descriptions "
|
118 |
"for AI generation prompts. Focus on the details provided, incorporate them into a cohesive narrative, "
|
119 |
+
"and follow these gender/furry rules:\n\n"
|
120 |
+
f"{rule_text}\n\n"
|
121 |
+
"When you respond, do not exceed five sentences. Return your final text in English or relevant language.\n"
|
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 |
+
desc_text = resp.choices[0].message.content.strip()
|
137 |
+
# 输出 (tags + desc)
|
138 |
+
return f"=== Tags ===\n{tags_str}\n\n=== Description ===\n{desc_text}"
|
|
|
|
|
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 |
+
调用 GPT 或 DeepSeek 做翻译
|
146 |
"""
|
147 |
if not api_key:
|
148 |
return "Error: No API Key provided."
|
149 |
+
if not content.strip():
|
150 |
return ""
|
151 |
|
|
|
152 |
if api_mode == "GPT":
|
153 |
base_url = None
|
154 |
model_name = "gpt-3.5-turbo"
|
|
|
160 |
if base_url:
|
161 |
client.base_url = base_url
|
162 |
|
163 |
+
system_prompt = f"You are a translator. Translate the following text to {lang}:"
|
164 |
try:
|
165 |
resp = client.chat.completions.create(
|
166 |
model=model_name,
|
167 |
messages=[
|
168 |
{"role": "system", "content": system_prompt},
|
169 |
+
{"role": "user", "content": content},
|
170 |
+
],
|
171 |
)
|
172 |
return resp.choices[0].message.content.strip()
|
173 |
except Exception as e:
|
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 Furry/Gender Transformer (GPT / DeepSeek)")
|
182 |
|
183 |
with gr.Row():
|
184 |
with gr.Column():
|
|
|
185 |
api_mode = gr.Radio(
|
186 |
label="选择API (GPT or DeepSeek)",
|
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=[
|
|
|
200 |
"Trans_to_Female",
|
201 |
"Trans_to_Mannequin",
|
202 |
"Trans_to_Intersex",
|
203 |
+
"Trans_to_Furry"
|
204 |
],
|
205 |
+
value="Trans_to_Male"
|
206 |
+
)
|
207 |
+
|
208 |
+
# 顶级分类
|
209 |
+
top_cat_dd = gr.Dropdown(
|
210 |
+
label="Furry: 主分类 (Top Category)",
|
211 |
+
choices=get_top_categories(FURRY_DATA),
|
212 |
+
value=None,
|
213 |
+
visible=False
|
214 |
+
)
|
215 |
+
# 二级分类
|
216 |
+
sub_cat_dd = gr.Dropdown(
|
217 |
+
label="Furry: 子分类 (Sub-Category)",
|
218 |
+
choices=[],
|
219 |
+
value=None,
|
220 |
+
visible=False
|
221 |
)
|
222 |
+
# 物种
|
223 |
+
species_dd = gr.Dropdown(
|
224 |
+
label="Furry: 物种 (Species)",
|
225 |
+
choices=[],
|
226 |
value=None,
|
227 |
visible=False
|
228 |
)
|
229 |
|
230 |
+
# 性别选项变化 -> 显示或隐藏 Furry 下拉
|
231 |
+
def show_furry_options(chosen):
|
232 |
+
if chosen == "Trans_to_Furry":
|
233 |
+
return gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
|
234 |
+
else:
|
235 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
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="提示词 (Prompt)",
|
268 |
+
lines=4
|
|
|
269 |
)
|
270 |
+
output_result = gr.Textbox(
|
271 |
label="(tags + 自然语言描述)",
|
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 |
+
translate_result = gr.Textbox(
|
282 |
label="翻译结果",
|
283 |
lines=10
|
284 |
)
|
285 |
|
286 |
+
######################################################################
|
287 |
+
# 生成
|
288 |
+
######################################################################
|
289 |
+
def on_generate(prompt, gender, tc, sc, spc, mode, key, lang):
|
290 |
+
# 1) 生成
|
291 |
+
tags_desc = generate_tags_and_description(prompt, gender, tc, sc, spc, mode, key)
|
292 |
+
# 2) 翻译
|
293 |
+
trans_txt = translate_text(tags_desc, lang, mode, key)
|
294 |
+
return tags_desc, trans_txt
|
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=[output_result, translate_result]
|
300 |
)
|
301 |
+
|
302 |
+
gen_btn = gr.Button("生成 / Generate")
|
303 |
+
gen_btn.click(
|
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=[output_result, translate_result]
|
307 |
)
|
308 |
|
309 |
return demo
|