File size: 11,275 Bytes
1e6c6f2
 
 
bef3741
1e6c6f2
bef3741
 
64322bd
 
0550b9b
64322bd
0550b9b
1e6c6f2
0550b9b
 
1e6c6f2
0550b9b
ceffbde
0550b9b
1e6c6f2
0550b9b
 
1e6c6f2
0550b9b
ceffbde
 
0550b9b
ceffbde
0550b9b
 
 
 
 
 
 
 
 
 
 
 
1e6c6f2
0550b9b
ceffbde
0550b9b
 
ceffbde
0550b9b
 
 
 
 
 
1e6c6f2
0550b9b
 
 
 
 
 
 
 
 
 
1e6c6f2
 
0550b9b
 
ceffbde
 
 
0550b9b
ceffbde
 
0550b9b
ceffbde
 
0550b9b
ceffbde
 
0550b9b
 
 
ceffbde
0550b9b
 
 
 
 
 
 
 
 
 
 
 
 
 
ceffbde
 
0550b9b
ceffbde
 
0550b9b
ceffbde
 
 
 
bef3741
64322bd
bef3741
64322bd
0550b9b
1e6c6f2
 
0550b9b
1e6c6f2
 
 
0550b9b
 
 
1e6c6f2
64322bd
0550b9b
64322bd
0550b9b
ceffbde
64322bd
1e6c6f2
64322bd
1e6c6f2
0550b9b
64322bd
1e6c6f2
64322bd
0550b9b
 
 
1e6c6f2
64322bd
ceffbde
64322bd
0550b9b
64322bd
0550b9b
64322bd
 
1e6c6f2
0550b9b
ceffbde
64322bd
ceffbde
 
 
 
 
 
64322bd
bef3741
64322bd
bef3741
64322bd
0550b9b
64322bd
ceffbde
 
64322bd
 
0550b9b
 
64322bd
ceffbde
0f91f56
ceffbde
64322bd
 
0550b9b
64322bd
1e6c6f2
64322bd
0550b9b
64322bd
 
 
 
1e6c6f2
64322bd
0550b9b
64322bd
 
0550b9b
 
64322bd
0550b9b
 
64322bd
1e6c6f2
64322bd
bef3741
 
 
 
0550b9b
64322bd
0550b9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64322bd
0550b9b
 
 
 
64322bd
0f91f56
64322bd
1e6c6f2
0550b9b
 
 
 
 
 
 
 
 
 
 
 
1e6c6f2
0550b9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64322bd
 
1e6c6f2
64322bd
0550b9b
64322bd
0550b9b
1e6c6f2
 
64322bd
 
 
0550b9b
 
ceffbde
1e6c6f2
64322bd
0550b9b
1e6c6f2
 
64322bd
 
0550b9b
 
 
 
 
 
 
 
 
05bf4a2
1e6c6f2
64322bd
0550b9b
 
64322bd
0550b9b
 
 
64322bd
0550b9b
 
64322bd
 
 
 
 
1e6c6f2
bef3741
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
##############################################
# app.py
##############################################
import os
import json
import gradio as gr
from openai import OpenAI

##############################################################################
# 1. 读取外部文件: furry_species.json & gender_rules.json
##############################################################################
# 假设 furry_species.json 的结构是多级字典: { "AQUATICS ...": { "Cetaceans": [], ...}, ... }
try:
    with open("furry_species.json", "r", encoding="utf-8") as f:
        FURRY_DATA = json.load(f)
except:
    FURRY_DATA = {}

# gender_rules.json: { "male": "...", "female": "...", "intersex": "...", "genderless": "..." }
try:
    with open("gender_rules.json", "r", encoding="utf-8") as f:
        GENDER_RULES = json.load(f)
except:
    GENDER_RULES = {}

##############################################################################
# 2. 构造多级下拉菜单:先选“主分类”,再选“子分类”
##############################################################################
def get_top_categories(furry_data):
    """获取所有顶级分类 (keys)"""
    return sorted(list(furry_data.keys()))

def get_sub_categories(furry_data, top_category):
    """
    根据所选 top_category, 返回二级分类列表
    furry_data[top_category] -> { "Cetaceans": [...], "FishFurs": [...], ... }
    """
    if top_category in furry_data:
        return sorted(list(furry_data[top_category].keys()))
    return []

def get_species_list(furry_data, top_category, sub_category):
    """
    返回最终物种列表
    furry_data[top_category][sub_category] -> list
    """
    if (
        top_category in furry_data
        and sub_category in furry_data[top_category]
    ):
        return sorted(furry_data[top_category][sub_category])
    return []

##############################################################################
# 3. 调用逻辑:GPT 或 DeepSeek
##############################################################################
def generate_tags_and_description(prompt, gender_option, top_cat, sub_cat, species_item, api_mode, api_key):
    """
    1) 构造 tags: gender, base_prompt, furry_species
    2) 读取 gender_rules 并拼入 system prompt
    3) 调用 GPT/DeepSeek
    4) 输出 (tags + 自然语言描述)
    """
    if not api_key:
        return "Error: No API Key provided."
    
    # 性别
    tags = {}
    if gender_option == "Trans_to_Male":
        tags["gender"] = "male"
        rule_text = GENDER_RULES.get("male", "")
    elif gender_option == "Trans_to_Female":
        tags["gender"] = "female"
        rule_text = GENDER_RULES.get("female", "")
    elif gender_option == "Trans_to_Mannequin":
        tags["gender"] = "genderless"
        rule_text = GENDER_RULES.get("genderless", "")
    elif gender_option == "Trans_to_Intersex":
        tags["gender"] = "intersex"
        rule_text = GENDER_RULES.get("intersex", "")
    else:
        # Furry
        tags["gender"] = "furry"
        rule_text = (
            GENDER_RULES.get("male", "") + "\n\n"  # 你可以根据自己的业务需求处理
            + GENDER_RULES.get("female", "") + "\n\n"
            + GENDER_RULES.get("intersex", "") + "\n\n"
            + GENDER_RULES.get("genderless", "")
        )  # 或者只给一个简要 furry rule

        # 选定物种
        final_species = "unknown"
        if top_cat and sub_cat and species_item:
            final_species = f"{top_cat} > {sub_cat} > {species_item}"
        tags["furry_species"] = final_species

    # 原始提示词
    tags["base_prompt"] = prompt

    # BaseURL & 模型
    if api_mode == "GPT":
        base_url = None
        model_name = "gpt-3.5-turbo"
    else:
        base_url = "https://api.deepseek.com"
        model_name = "deepseek-chat"

    client = OpenAI(api_key=api_key)
    if base_url:
        client.base_url = base_url

    # 将 tags 拼为字符串
    tags_str = "\n".join([f"{k}: {v}" for k, v in tags.items() if v])

    # system prompt 带上Gender Rules
    system_prompt = (
        "You are a creative assistant that generates detailed and imaginative scene descriptions "
        "for AI generation prompts. Focus on the details provided, incorporate them into a cohesive narrative, "
        "and follow these gender/furry rules:\n\n"
        f"{rule_text}\n\n"
        "When you respond, do not exceed five sentences. Return your final text in English or relevant language.\n"
    )

    # Chat
    try:
        resp = client.chat.completions.create(
            model=model_name,
            messages=[
                {"role": "system", "content": system_prompt},
                {
                    "role": "user",
                    "content": f"Here are the tags:\n{tags_str}\nPlease generate a vivid, imaginative scene description."
                },
            ],
        )
        desc_text = resp.choices[0].message.content.strip()
        # 输出 (tags + desc)
        return f"=== Tags ===\n{tags_str}\n\n=== Description ===\n{desc_text}"

    except Exception as e:
        return f"{api_mode} generation failed. Error: {e}"

def translate_text(content, lang, api_mode, api_key):
    """
    调用 GPT 或 DeepSeek 做翻译
    """
    if not api_key:
        return "Error: No API Key provided."
    if not content.strip():
        return ""

    if api_mode == "GPT":
        base_url = None
        model_name = "gpt-3.5-turbo"
    else:
        base_url = "https://api.deepseek.com"
        model_name = "deepseek-chat"

    client = OpenAI(api_key=api_key)
    if base_url:
        client.base_url = base_url

    system_prompt = f"You are a translator. Translate the following text to {lang}:"
    try:
        resp = client.chat.completions.create(
            model=model_name,
            messages=[
                {"role": "system", "content": system_prompt},
                {"role": "user",   "content": content},
            ],
        )
        return resp.choices[0].message.content.strip()
    except Exception as e:
        return f"{api_mode} translation failed. Error: {e}"

##############################################################################
# 4. Gradio 界面
##############################################################################
def build_interface():
    with gr.Blocks() as demo:
        gr.Markdown("## Prompt Furry/Gender Transformer (GPT / DeepSeek)")

        with gr.Row():
            with gr.Column():
                api_mode = gr.Radio(
                    label="选择API (GPT or DeepSeek)",
                    choices=["GPT", "DeepSeek"],
                    value="GPT"
                )
                api_key = gr.Textbox(
                    label="API Key",
                    type="password"
                )

                # 性别
                gender_option = gr.Radio(
                    label="转换目标",
                    choices=[
                        "Trans_to_Male",
                        "Trans_to_Female",
                        "Trans_to_Mannequin",
                        "Trans_to_Intersex",
                        "Trans_to_Furry"
                    ],
                    value="Trans_to_Male"
                )

                # 顶级分类
                top_cat_dd = gr.Dropdown(
                    label="Furry: 主分类 (Top Category)",
                    choices=get_top_categories(FURRY_DATA),
                    value=None,
                    visible=False
                )
                # 二级分类
                sub_cat_dd = gr.Dropdown(
                    label="Furry: 子分类 (Sub-Category)",
                    choices=[],
                    value=None,
                    visible=False
                )
                # 物种
                species_dd = gr.Dropdown(
                    label="Furry: 物种 (Species)",
                    choices=[],
                    value=None,
                    visible=False
                )

                # 性别选项变化 -> 显示或隐藏 Furry 下拉
                def show_furry_options(chosen):
                    if chosen == "Trans_to_Furry":
                        return gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
                    else:
                        return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)

                gender_option.change(
                    fn=show_furry_options,
                    inputs=[gender_option],
                    outputs=[top_cat_dd, sub_cat_dd, species_dd]
                )

                # 顶级分类 -> 更新子分类
                def on_top_cat_select(selected):
                    subs = get_sub_categories(FURRY_DATA, selected)
                    return gr.update(choices=subs, value=None)

                top_cat_dd.change(
                    fn=on_top_cat_select,
                    inputs=[top_cat_dd],
                    outputs=[sub_cat_dd]
                )

                # 子分类 -> 更新物种
                def on_sub_cat_select(top_c, sub_c):
                    sp = get_species_list(FURRY_DATA, top_c, sub_c)
                    return gr.update(choices=sp, value=None)

                sub_cat_dd.change(
                    fn=on_sub_cat_select,
                    inputs=[top_cat_dd, sub_cat_dd],
                    outputs=[species_dd]
                )

            with gr.Column():
                user_prompt = gr.Textbox(
                    label="提示词 (Prompt)",
                    lines=4
                )
                output_result = gr.Textbox(
                    label="(tags + 自然语言描述)",
                    lines=10
                )

        with gr.Row():
            translate_lang = gr.Dropdown(
                label="翻译语言",
                choices=["English", "Chinese", "Japanese", "French", "German", "Spanish"],
                value="English"
            )
            translate_result = gr.Textbox(
                label="翻译结果",
                lines=10
            )

        ######################################################################
        # 生成
        ######################################################################
        def on_generate(prompt, gender, tc, sc, spc, mode, key, lang):
            # 1) 生成
            tags_desc = generate_tags_and_description(prompt, gender, tc, sc, spc, mode, key)
            # 2) 翻译
            trans_txt = translate_text(tags_desc, lang, mode, key)
            return tags_desc, trans_txt

        user_prompt.submit(
            fn=on_generate,
            inputs=[user_prompt, gender_option, top_cat_dd, sub_cat_dd, species_dd, api_mode, api_key, translate_lang],
            outputs=[output_result, translate_result]
        )

        gen_btn = gr.Button("生成 / Generate")
        gen_btn.click(
            fn=on_generate,
            inputs=[user_prompt, gender_option, top_cat_dd, sub_cat_dd, species_dd, api_mode, api_key, translate_lang],
            outputs=[output_result, translate_result]
        )

    return demo

if __name__ == "__main__":
    demo = build_interface()
    demo.launch()