Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,67 @@
|
|
|
|
|
|
|
|
1 |
import os
|
|
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
|
5 |
-
#
|
6 |
from openai import OpenAI
|
7 |
|
8 |
##############################################################################
|
9 |
-
# 1. Furry 物种数据
|
10 |
##############################################################################
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
"""
|
20 |
-
|
21 |
-
["CANIDS (Canines) - Dogs", "CANIDS (Canines) - Wolves", ...]
|
22 |
"""
|
23 |
result = []
|
24 |
-
for category, species_list in
|
25 |
for sp in species_list:
|
26 |
result.append(f"{category} - {sp}")
|
27 |
return sorted(result)
|
28 |
|
29 |
-
ALL_FURRY_SPECIES =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
##############################################################################
|
32 |
-
# 2.
|
33 |
##############################################################################
|
34 |
-
|
|
|
35 |
"""
|
36 |
-
1) 构造 tags
|
37 |
-
2)
|
38 |
-
3)
|
39 |
"""
|
|
|
|
|
|
|
|
|
|
|
40 |
tags = {}
|
41 |
if gender_option == "Trans_to_Male":
|
42 |
tags["gender"] = "male"
|
@@ -52,61 +77,65 @@ def generate_transformed_prompt(prompt, gender_option, furry_species, api_mode,
|
|
52 |
|
53 |
tags["base_prompt"] = prompt
|
54 |
|
55 |
-
#
|
56 |
if api_mode == "GPT":
|
57 |
-
# GPT
|
58 |
base_url = None
|
59 |
-
model_name = "gpt-3.5-turbo" #
|
60 |
else:
|
61 |
# DeepSeek
|
62 |
base_url = "https://api.deepseek.com"
|
63 |
model_name = "deepseek-chat"
|
64 |
|
65 |
-
#
|
66 |
-
if not api_key:
|
67 |
-
return "Error: API Key not provided."
|
68 |
-
|
69 |
client = OpenAI(api_key=api_key)
|
70 |
if base_url:
|
71 |
client.base_url = base_url
|
72 |
|
73 |
-
#
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
try:
|
77 |
-
# 发起 chat.completions.create()
|
78 |
response = client.chat.completions.create(
|
79 |
model=model_name,
|
80 |
messages=[
|
|
|
81 |
{
|
82 |
-
"role": "
|
83 |
"content": (
|
84 |
-
"
|
85 |
-
"
|
86 |
-
"cohesive narrative. Use at least three sentences but no more than five sentences."
|
87 |
),
|
88 |
},
|
89 |
-
|
90 |
-
"role": "user",
|
91 |
-
"content": f"Here are the tags:\n{tag_desc}\nPlease generate a vivid, imaginative scene description.",
|
92 |
-
},
|
93 |
-
]
|
94 |
)
|
95 |
-
|
|
|
|
|
|
|
|
|
|
|
96 |
except Exception as e:
|
97 |
return f"{api_mode} generation failed. Error: {e}"
|
98 |
|
99 |
-
|
100 |
-
def translate_prompt_text(text, target_language, api_mode, api_key):
|
101 |
"""
|
102 |
-
|
103 |
"""
|
104 |
if not api_key:
|
105 |
-
return "Error: API Key
|
106 |
-
|
107 |
if not text.strip():
|
108 |
return ""
|
109 |
|
|
|
110 |
if api_mode == "GPT":
|
111 |
base_url = None
|
112 |
model_name = "gpt-3.5-turbo"
|
@@ -118,8 +147,8 @@ def translate_prompt_text(text, target_language, api_mode, api_key):
|
|
118 |
if base_url:
|
119 |
client.base_url = base_url
|
120 |
|
|
|
121 |
try:
|
122 |
-
system_prompt = f"You are a professional translator. Translate the following text to {target_language}:"
|
123 |
resp = client.chat.completions.create(
|
124 |
model=model_name,
|
125 |
messages=[
|
@@ -134,24 +163,26 @@ def translate_prompt_text(text, target_language, api_mode, api_key):
|
|
134 |
##############################################################################
|
135 |
# 3. Gradio 界面
|
136 |
##############################################################################
|
137 |
-
def
|
138 |
with gr.Blocks() as demo:
|
139 |
-
gr.Markdown("## Prompt
|
140 |
|
141 |
with gr.Row():
|
142 |
with gr.Column():
|
|
|
143 |
api_mode = gr.Radio(
|
144 |
-
label="选择
|
145 |
choices=["GPT", "DeepSeek"],
|
146 |
value="GPT",
|
147 |
)
|
148 |
api_key = gr.Textbox(
|
149 |
-
label="API
|
150 |
type="password",
|
151 |
-
placeholder="
|
152 |
)
|
|
|
153 |
gender_option = gr.Radio(
|
154 |
-
label="转换目标
|
155 |
choices=[
|
156 |
"Trans_to_Male",
|
157 |
"Trans_to_Female",
|
@@ -161,60 +192,62 @@ def build_app():
|
|
161 |
],
|
162 |
value="Trans_to_Male",
|
163 |
)
|
164 |
-
|
165 |
-
label="Furry
|
166 |
choices=ALL_FURRY_SPECIES,
|
167 |
value=None,
|
168 |
visible=False
|
169 |
)
|
170 |
-
|
171 |
-
|
172 |
-
|
|
|
|
|
173 |
|
174 |
with gr.Column():
|
175 |
-
|
176 |
label="提示词 (Prompt)",
|
177 |
-
lines=
|
178 |
-
placeholder="
|
179 |
)
|
180 |
-
|
181 |
-
label="
|
182 |
-
lines=
|
183 |
)
|
184 |
|
185 |
-
# 翻译
|
186 |
with gr.Row():
|
187 |
translate_language = gr.Dropdown(
|
188 |
label="翻译语言 (Translation Language)",
|
189 |
choices=["English", "Chinese", "Japanese", "French", "German", "Spanish"],
|
190 |
-
value="English"
|
191 |
)
|
192 |
translate_output = gr.Textbox(
|
193 |
-
label="翻译结果
|
194 |
-
lines=
|
195 |
)
|
196 |
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
|
|
203 |
|
204 |
-
|
205 |
fn=on_generate,
|
206 |
-
inputs=[
|
207 |
-
outputs=[
|
208 |
)
|
209 |
-
|
210 |
-
|
211 |
fn=on_generate,
|
212 |
-
inputs=[
|
213 |
-
outputs=[
|
214 |
)
|
215 |
|
216 |
return demo
|
217 |
|
218 |
if __name__ == "__main__":
|
219 |
-
demo =
|
220 |
demo.launch()
|
|
|
1 |
+
##############################################
|
2 |
+
# app.py
|
3 |
+
##############################################
|
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. 从外部文件中加载 Furry 物种数据 + Gender 细节文本
|
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 fs_file:
|
25 |
+
furry_map = json.load(fs_file)
|
26 |
+
except:
|
27 |
+
furry_map = {} # 若文件不存在或出错,可给个空dict备用
|
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 |
+
ALL_FURRY_SPECIES = flatten_furry_species(furry_map)
|
40 |
+
|
41 |
+
# 读取 Gender 细节长文本 (记忆库)
|
42 |
+
try:
|
43 |
+
with open("gender_details.txt", "r", encoding="utf-8") as gd_file:
|
44 |
+
GENDER_DETAILS_TEXT = gd_file.read()
|
45 |
+
except:
|
46 |
+
GENDER_DETAILS_TEXT = (
|
47 |
+
"Gender conversion rules are missing. Please provide gender_details.txt."
|
48 |
+
)
|
49 |
|
50 |
##############################################################################
|
51 |
+
# 2. 核心函数:根据选择调用 GPT 或 DeepSeek
|
52 |
##############################################################################
|
53 |
+
|
54 |
+
def generate_description_with_tags(prompt, gender_option, furry_species, api_mode, api_key):
|
55 |
"""
|
56 |
+
1) 构造 tags(包含 gender/furry/base_prompt)。
|
57 |
+
2) 调用 GPT 或 DeepSeek,带上记忆库信息 (GENDER_DETAILS_TEXT) 作为系统提示。
|
58 |
+
3) 返回 (tags + 自然语言描述) 的输出形式。
|
59 |
"""
|
60 |
+
|
61 |
+
if not api_key:
|
62 |
+
return "Error: No API Key provided."
|
63 |
+
|
64 |
+
# 组装 tags
|
65 |
tags = {}
|
66 |
if gender_option == "Trans_to_Male":
|
67 |
tags["gender"] = "male"
|
|
|
77 |
|
78 |
tags["base_prompt"] = prompt
|
79 |
|
80 |
+
# 确定 base_url + model
|
81 |
if api_mode == "GPT":
|
|
|
82 |
base_url = None
|
83 |
+
model_name = "gpt-3.5-turbo" # or "gpt-4", "gpt-4o", etc.
|
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 |
+
# 准备对话消息:把 gender_details 放在系统提示中,以便 AI 参考
|
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 obey the following gender transformation & furry rules:\n\n"
|
102 |
+
f"{GENDER_DETAILS_TEXT}\n\n"
|
103 |
+
"After forming your final description, return it in no more than five sentences. Thank you!"
|
104 |
+
)
|
105 |
|
106 |
try:
|
|
|
107 |
response = client.chat.completions.create(
|
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 |
+
description = response.choices[0].message.content.strip()
|
121 |
+
|
122 |
+
# 输出形式:(tags + 自然语言描述)
|
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(text, translate_language, api_mode, api_key):
|
|
|
130 |
"""
|
131 |
+
仅做一次简单翻译。若不需要翻译可以省去。也可用系统提示让 AI 翻译。
|
132 |
"""
|
133 |
if not api_key:
|
134 |
+
return "Error: No API Key provided."
|
|
|
135 |
if not text.strip():
|
136 |
return ""
|
137 |
|
138 |
+
# GPT vs DeepSeek
|
139 |
if api_mode == "GPT":
|
140 |
base_url = None
|
141 |
model_name = "gpt-3.5-turbo"
|
|
|
147 |
if base_url:
|
148 |
client.base_url = base_url
|
149 |
|
150 |
+
system_prompt = f"You are a professional translator. Translate the following text to {translate_language}:"
|
151 |
try:
|
|
|
152 |
resp = client.chat.completions.create(
|
153 |
model=model_name,
|
154 |
messages=[
|
|
|
163 |
##############################################################################
|
164 |
# 3. Gradio 界面
|
165 |
##############################################################################
|
166 |
+
def build_interface():
|
167 |
with gr.Blocks() as demo:
|
168 |
+
gr.Markdown("## Prompt Transformer (GPT / DeepSeek) - (tags + 自然语言描述)")
|
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密钥 (API Key)",
|
180 |
type="password",
|
181 |
+
placeholder="在此输入 GPT / DeepSeek 的API Key"
|
182 |
)
|
183 |
+
# 性别 / Furry
|
184 |
gender_option = gr.Radio(
|
185 |
+
label="转换目标",
|
186 |
choices=[
|
187 |
"Trans_to_Male",
|
188 |
"Trans_to_Female",
|
|
|
192 |
],
|
193 |
value="Trans_to_Male",
|
194 |
)
|
195 |
+
furry_species = gr.Dropdown(
|
196 |
+
label="Furry 物种选择",
|
197 |
choices=ALL_FURRY_SPECIES,
|
198 |
value=None,
|
199 |
visible=False
|
200 |
)
|
201 |
+
|
202 |
+
def show_furry_species(choice):
|
203 |
+
return gr.update(visible=(choice == "Trans_to_Furry"))
|
204 |
+
|
205 |
+
gender_option.change(show_furry_species, inputs=[gender_option], outputs=[furry_species])
|
206 |
|
207 |
with gr.Column():
|
208 |
+
user_prompt = gr.Textbox(
|
209 |
label="提示词 (Prompt)",
|
210 |
+
lines=4,
|
211 |
+
placeholder="示例:一位穿着蓝色长裙的少女,坐在海边..."
|
212 |
)
|
213 |
+
output_tags_and_desc = gr.Textbox(
|
214 |
+
label="(tags + 自然语言描述)",
|
215 |
+
lines=10
|
216 |
)
|
217 |
|
|
|
218 |
with gr.Row():
|
219 |
translate_language = gr.Dropdown(
|
220 |
label="翻译语言 (Translation Language)",
|
221 |
choices=["English", "Chinese", "Japanese", "French", "German", "Spanish"],
|
222 |
+
value="English"
|
223 |
)
|
224 |
translate_output = gr.Textbox(
|
225 |
+
label="翻译结果",
|
226 |
+
lines=10
|
227 |
)
|
228 |
|
229 |
+
# 点击生成 -> 同时调用生成描述 & 翻译
|
230 |
+
def on_generate(prompt, gender, f_species, mode, key, lang):
|
231 |
+
# 1. 生成 (tags + 自然语言描述)
|
232 |
+
result_text = generate_description_with_tags(prompt, gender, f_species, mode, key)
|
233 |
+
# 2. 翻译
|
234 |
+
translated = translate_text(result_text, lang, mode, key)
|
235 |
+
return result_text, translated
|
236 |
|
237 |
+
user_prompt.submit(
|
238 |
fn=on_generate,
|
239 |
+
inputs=[user_prompt, gender_option, furry_species, api_mode, api_key, translate_language],
|
240 |
+
outputs=[output_tags_and_desc, translate_output]
|
241 |
)
|
242 |
+
generate_btn = gr.Button("生成 / Generate")
|
243 |
+
generate_btn.click(
|
244 |
fn=on_generate,
|
245 |
+
inputs=[user_prompt, gender_option, furry_species, api_mode, api_key, translate_language],
|
246 |
+
outputs=[output_tags_and_desc, translate_output]
|
247 |
)
|
248 |
|
249 |
return demo
|
250 |
|
251 |
if __name__ == "__main__":
|
252 |
+
demo = build_interface()
|
253 |
demo.launch()
|