Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,82 @@
|
|
1 |
-
##############################################
|
2 |
-
# app.py
|
3 |
-
##############################################
|
4 |
import os
|
5 |
-
import requests
|
6 |
import gradio as gr
|
|
|
7 |
|
8 |
-
#
|
9 |
from openai import OpenAI
|
10 |
|
11 |
##############################################################################
|
12 |
-
# 1.
|
13 |
##############################################################################
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
"""
|
16 |
-
|
17 |
-
|
18 |
-
2) client = OpenAI(api_key=...)
|
19 |
-
3) client.chat.completions.create(...)
|
20 |
"""
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
if not api_key:
|
25 |
-
return "Error:
|
26 |
|
27 |
-
# 创建一个新的 OpenAI Client
|
28 |
client = OpenAI(api_key=api_key)
|
29 |
-
|
30 |
-
# 如果你有自定义 base_url(如代理/私有化部署),可在此设置
|
31 |
if base_url:
|
32 |
client.base_url = base_url
|
33 |
|
34 |
-
#
|
35 |
-
|
36 |
-
f"{k}: {', '.join(v) if isinstance(v, list) else v}"
|
37 |
-
for k, v in tags.items() if v
|
38 |
-
])
|
39 |
|
40 |
try:
|
41 |
-
|
42 |
-
|
|
|
43 |
messages=[
|
44 |
{
|
45 |
"role": "system",
|
@@ -51,180 +88,70 @@ def generate_natural_language_description_gpt(tags, api_key=None, base_url=None,
|
|
51 |
},
|
52 |
{
|
53 |
"role": "user",
|
54 |
-
"content": f"Here are the tags
|
55 |
},
|
56 |
]
|
57 |
)
|
58 |
-
|
59 |
-
return chat_completion.choices[0].message.content.strip()
|
60 |
except Exception as e:
|
61 |
-
return f"
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
##############################################################################
|
66 |
-
def generate_natural_language_description_deepseek(tags, api_key=None, base_url=None):
|
67 |
"""
|
68 |
-
调用 DeepSeek
|
69 |
-
注意:此处示例的接口 URL、请求体和返回格式需要替换为真实 DeepSeek 文档内容。
|
70 |
"""
|
71 |
if not api_key:
|
72 |
-
return "Error:
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
try:
|
78 |
-
headers = {
|
79 |
-
"Content-Type": "application/json",
|
80 |
-
"Authorization": f"Bearer {api_key}"
|
81 |
-
}
|
82 |
-
payload = {
|
83 |
-
"tags": tags
|
84 |
-
}
|
85 |
-
resp = requests.post(url, json=payload, headers=headers, timeout=30)
|
86 |
-
if resp.status_code == 200:
|
87 |
-
data = resp.json()
|
88 |
-
# 根据你们的返回结构来取值
|
89 |
-
if data.get("success"):
|
90 |
-
return data["data"].get("description", "No description found.")
|
91 |
-
else:
|
92 |
-
return f"DeepSeek generation success=false. {data}"
|
93 |
-
else:
|
94 |
-
return f"DeepSeek generation failed. Status {resp.status_code}, {resp.text}"
|
95 |
-
except Exception as e:
|
96 |
-
return f"DeepSeek generation request error: {e}"
|
97 |
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
if not api_key:
|
105 |
-
return "Error: No GPT Translation Key provided."
|
106 |
|
107 |
client = OpenAI(api_key=api_key)
|
108 |
if base_url:
|
109 |
client.base_url = base_url
|
110 |
|
111 |
try:
|
112 |
-
# Prompt: 让 GPT 扮演翻译
|
113 |
system_prompt = f"You are a professional translator. Translate the following text to {target_language}:"
|
114 |
-
|
115 |
-
model=
|
116 |
messages=[
|
117 |
{"role": "system", "content": system_prompt},
|
118 |
{"role": "user", "content": text},
|
119 |
]
|
120 |
)
|
121 |
-
return
|
122 |
-
except Exception as e:
|
123 |
-
return f"GPT translation failed. Error: {e}"
|
124 |
-
|
125 |
-
##############################################################################
|
126 |
-
# 4. DeepSeek 翻译函数(需你修改为真实接口)
|
127 |
-
##############################################################################
|
128 |
-
def translate_text_with_deepseek(text, target_language, api_key=None, base_url=None):
|
129 |
-
if not api_key:
|
130 |
-
return "Error: No DeepSeek Translation Key provided."
|
131 |
-
|
132 |
-
# 假设 DeepSeek 翻译接口如下,请改为实际
|
133 |
-
url = base_url or "https://api.deepseek.com/v1/translate"
|
134 |
-
|
135 |
-
try:
|
136 |
-
headers = {
|
137 |
-
"Content-Type": "application/json",
|
138 |
-
"Authorization": f"Bearer {api_key}"
|
139 |
-
}
|
140 |
-
payload = {
|
141 |
-
"text": text,
|
142 |
-
"target_language": target_language
|
143 |
-
}
|
144 |
-
resp = requests.post(url, json=payload, headers=headers, timeout=30)
|
145 |
-
if resp.status_code == 200:
|
146 |
-
data = resp.json()
|
147 |
-
if data.get("success"):
|
148 |
-
return data["data"].get("translated_text", "No translated_text found.")
|
149 |
-
else:
|
150 |
-
return f"DeepSeek translation success=false. {data}"
|
151 |
-
else:
|
152 |
-
return f"DeepSeek translation failed. Status {resp.status_code}, {resp.text}"
|
153 |
except Exception as e:
|
154 |
-
return f"
|
155 |
-
|
156 |
-
##############################################################################
|
157 |
-
# 5. 根据用户选择进行提示词转换并调用 GPT/DeepSeek 生成描述
|
158 |
-
##############################################################################
|
159 |
-
def transform_prompt(prompt, gender_option, furry_species, api_mode, api_key):
|
160 |
-
"""
|
161 |
-
示例转换逻辑:将 prompt 结合 gender/furry 信息组成 tags,然后调用指定 API。
|
162 |
-
你可以在此处扩充更复杂的处理逻辑。
|
163 |
-
"""
|
164 |
-
tags = {}
|
165 |
-
|
166 |
-
# 性别 / 物种 设定
|
167 |
-
if gender_option == "Trans_to_Male":
|
168 |
-
tags["gender"] = "male"
|
169 |
-
elif gender_option == "Trans_to_Female":
|
170 |
-
tags["gender"] = "female"
|
171 |
-
elif gender_option == "Trans_to_Mannequin":
|
172 |
-
tags["gender"] = "genderless"
|
173 |
-
elif gender_option == "Trans_to_Intersex":
|
174 |
-
tags["gender"] = "intersex"
|
175 |
-
elif gender_option == "Trans_to_Furry":
|
176 |
-
tags["gender"] = "furry"
|
177 |
-
tags["furry_species"] = furry_species or "unknown"
|
178 |
-
|
179 |
-
# 原始 prompt
|
180 |
-
tags["base_prompt"] = prompt
|
181 |
-
|
182 |
-
# 调用对应 API
|
183 |
-
if api_mode == "GPT":
|
184 |
-
scene_description = generate_natural_language_description_gpt(tags, api_key)
|
185 |
-
else: # DeepSeek
|
186 |
-
scene_description = generate_natural_language_description_deepseek(tags, api_key)
|
187 |
-
|
188 |
-
return scene_description
|
189 |
|
190 |
##############################################################################
|
191 |
-
#
|
192 |
##############################################################################
|
193 |
-
def
|
194 |
-
if not scene_desc.strip():
|
195 |
-
return ""
|
196 |
-
if api_mode == "GPT":
|
197 |
-
return translate_text_with_gpt(scene_desc, translate_language, api_key)
|
198 |
-
else:
|
199 |
-
return translate_text_with_deepseek(scene_desc, translate_language, api_key)
|
200 |
-
|
201 |
-
##############################################################################
|
202 |
-
# 7. 搭建 Gradio 界面
|
203 |
-
##############################################################################
|
204 |
-
def build_interface():
|
205 |
with gr.Blocks() as demo:
|
206 |
-
|
207 |
-
gr.Markdown("## Prompts_TransTool - 提示词一键性别物种转换器(openai>=1.0.0 版)")
|
208 |
|
209 |
with gr.Row():
|
210 |
with gr.Column():
|
211 |
-
# 选择 API 服务(GPT / DeepSeek)
|
212 |
api_mode = gr.Radio(
|
213 |
-
label="选择 API
|
214 |
choices=["GPT", "DeepSeek"],
|
215 |
-
value="GPT"
|
216 |
)
|
217 |
-
|
218 |
-
# 输入 API Key
|
219 |
api_key = gr.Textbox(
|
220 |
label="API 密钥 (API Key)",
|
221 |
type="password",
|
222 |
-
placeholder="
|
223 |
)
|
224 |
-
|
225 |
-
# 性别 / Furry 选项
|
226 |
gender_option = gr.Radio(
|
227 |
-
label="
|
228 |
choices=[
|
229 |
"Trans_to_Male",
|
230 |
"Trans_to_Female",
|
@@ -234,94 +161,60 @@ def build_interface():
|
|
234 |
],
|
235 |
value="Trans_to_Male",
|
236 |
)
|
237 |
-
|
238 |
-
# 如果是 Furry,显示可选物种
|
239 |
-
furry_species = gr.Dropdown(
|
240 |
label="Furry 物种 (Furry Species)",
|
241 |
-
choices=
|
242 |
value=None,
|
243 |
visible=False
|
244 |
)
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
gender_option.change(
|
249 |
-
fn=show_furry_species,
|
250 |
-
inputs=[gender_option],
|
251 |
-
outputs=[furry_species]
|
252 |
-
)
|
253 |
|
254 |
with gr.Column():
|
255 |
-
|
256 |
-
user_prompt = gr.Textbox(
|
257 |
label="提示词 (Prompt)",
|
258 |
lines=5,
|
259 |
-
placeholder=
|
260 |
-
"示例:一位穿着红色连衣裙的少女,坐在落日余晖下的草地上..."
|
261 |
-
)
|
262 |
)
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
label="转换后的提示词 (Generated Trans-Description)",
|
267 |
-
lines=7
|
268 |
)
|
269 |
|
|
|
270 |
with gr.Row():
|
271 |
-
# 翻译语言
|
272 |
translate_language = gr.Dropdown(
|
273 |
label="翻译语言 (Translation Language)",
|
274 |
-
choices=[
|
275 |
-
"English", "Chinese", "Japanese", "French", "German",
|
276 |
-
"Dutch", "Arabic", "Russian", "Persian", "Italian"
|
277 |
-
],
|
278 |
value="English",
|
279 |
)
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
lines=7
|
284 |
)
|
285 |
|
286 |
-
############################################################################
|
287 |
-
# 事件逻辑
|
288 |
-
############################################################################
|
289 |
-
# 1) 生成并翻译
|
290 |
def on_generate(prompt, gender, furry, mode, key, lang):
|
291 |
-
|
292 |
-
|
293 |
-
|
|
|
|
|
294 |
|
295 |
-
|
296 |
-
user_prompt.submit(
|
297 |
fn=on_generate,
|
298 |
-
inputs=[
|
299 |
-
outputs=[
|
300 |
)
|
301 |
-
|
302 |
-
|
303 |
-
generate_button = gr.Button("生成 / Generate")
|
304 |
-
generate_button.click(
|
305 |
fn=on_generate,
|
306 |
-
inputs=[
|
307 |
-
outputs=[
|
308 |
-
)
|
309 |
-
|
310 |
-
# 2) 语言切换再翻译
|
311 |
-
def on_translate(scene_desc, lang, mode, key):
|
312 |
-
return do_translation(scene_desc, lang, mode, key)
|
313 |
-
|
314 |
-
translate_language.change(
|
315 |
-
fn=on_translate,
|
316 |
-
inputs=[generated_output, translate_language, api_mode, api_key],
|
317 |
-
outputs=[translated_text]
|
318 |
)
|
319 |
|
320 |
return demo
|
321 |
|
322 |
-
##############################################################################
|
323 |
-
# 主入口
|
324 |
-
##############################################################################
|
325 |
if __name__ == "__main__":
|
326 |
-
demo =
|
327 |
demo.launch()
|
|
|
|
|
|
|
|
|
1 |
import os
|
|
|
2 |
import gradio as gr
|
3 |
+
import requests
|
4 |
|
5 |
+
# 从新版 openai>=1.0.0 库导入
|
6 |
from openai import OpenAI
|
7 |
|
8 |
##############################################################################
|
9 |
+
# 1. Furry 物种数据
|
10 |
##############################################################################
|
11 |
+
furry_species_map = {
|
12 |
+
"CANIDS (Canines)": ["Dogs", "Wolves", "Foxes", "Jackals"],
|
13 |
+
"FELINES (Cats)": ["HouseCats", "Lions", "Tigers", "Cheetahs"],
|
14 |
+
"CUSTOM (Just An Example)": ["Dragons", "Werewolves", "Kitsune"],
|
15 |
+
# ... 这里你可以替换成更完整的物种分类,或保留最简
|
16 |
+
}
|
17 |
+
|
18 |
+
def flatten_species(map_dict):
|
19 |
"""
|
20 |
+
将分门别类的物种字典展开成一个扁平列表,比如:
|
21 |
+
["CANIDS (Canines) - Dogs", "CANIDS (Canines) - Wolves", ...]
|
|
|
|
|
22 |
"""
|
23 |
+
result = []
|
24 |
+
for category, species_list in map_dict.items():
|
25 |
+
for sp in species_list:
|
26 |
+
result.append(f"{category} - {sp}")
|
27 |
+
return sorted(result)
|
28 |
+
|
29 |
+
ALL_FURRY_SPECIES = flatten_species(furry_species_map)
|
30 |
+
|
31 |
+
##############################################################################
|
32 |
+
# 2. 核心:调用 GPT 或 DeepSeek 生成描述 & 翻译
|
33 |
+
##############################################################################
|
34 |
+
def generate_transformed_prompt(prompt, gender_option, furry_species, api_mode, api_key):
|
35 |
+
"""
|
36 |
+
1) 构造 tags(包括性别/物种)
|
37 |
+
2) 选择 GPT / DeepSeek 调用
|
38 |
+
3) 返回生成的描述
|
39 |
+
"""
|
40 |
+
tags = {}
|
41 |
+
if gender_option == "Trans_to_Male":
|
42 |
+
tags["gender"] = "male"
|
43 |
+
elif gender_option == "Trans_to_Female":
|
44 |
+
tags["gender"] = "female"
|
45 |
+
elif gender_option == "Trans_to_Mannequin":
|
46 |
+
tags["gender"] = "genderless"
|
47 |
+
elif gender_option == "Trans_to_Intersex":
|
48 |
+
tags["gender"] = "intersex"
|
49 |
+
elif gender_option == "Trans_to_Furry":
|
50 |
+
tags["gender"] = "furry"
|
51 |
+
tags["furry_species"] = furry_species or "unknown"
|
52 |
+
|
53 |
+
tags["base_prompt"] = prompt
|
54 |
+
|
55 |
+
# 2. 根据选择调用 GPT / DeepSeek
|
56 |
+
if api_mode == "GPT":
|
57 |
+
# GPT
|
58 |
+
base_url = None
|
59 |
+
model_name = "gpt-3.5-turbo" # 你可改成 "gpt-4" 等
|
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 |
+
tag_desc = "\n".join([f"{k}: {v}" for k, v in tags.items() if v])
|
|
|
|
|
|
|
75 |
|
76 |
try:
|
77 |
+
# 发起 chat.completions.create()
|
78 |
+
response = client.chat.completions.create(
|
79 |
+
model=model_name,
|
80 |
messages=[
|
81 |
{
|
82 |
"role": "system",
|
|
|
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 |
+
return response.choices[0].message.content.strip()
|
|
|
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 |
+
调用 GPT / DeepSeek 做翻译,简化写法:同样只需改 model/base_url
|
|
|
103 |
"""
|
104 |
if not api_key:
|
105 |
+
return "Error: API Key not provided."
|
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"
|
113 |
+
else:
|
114 |
+
base_url = "https://api.deepseek.com"
|
115 |
+
model_name = "deepseek-chat"
|
|
|
|
|
116 |
|
117 |
client = OpenAI(api_key=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=[
|
126 |
{"role": "system", "content": system_prompt},
|
127 |
{"role": "user", "content": text},
|
128 |
]
|
129 |
)
|
130 |
+
return resp.choices[0].message.content.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
except Exception as e:
|
132 |
+
return f"{api_mode} translation failed. Error: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
|
134 |
##############################################################################
|
135 |
+
# 3. Gradio 界面
|
136 |
##############################################################################
|
137 |
+
def build_app():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
with gr.Blocks() as demo:
|
139 |
+
gr.Markdown("## Prompt TransTool - GPT/DeepSeek 性别物种转换")
|
|
|
140 |
|
141 |
with gr.Row():
|
142 |
with gr.Column():
|
|
|
143 |
api_mode = gr.Radio(
|
144 |
+
label="选择 API(GPT / DeepSeek)",
|
145 |
choices=["GPT", "DeepSeek"],
|
146 |
+
value="GPT",
|
147 |
)
|
|
|
|
|
148 |
api_key = gr.Textbox(
|
149 |
label="API 密钥 (API Key)",
|
150 |
type="password",
|
151 |
+
placeholder="请输入 GPT 或 DeepSeek 的 API 密钥"
|
152 |
)
|
|
|
|
|
153 |
gender_option = gr.Radio(
|
154 |
+
label="转换目标 (Gender / Furry)",
|
155 |
choices=[
|
156 |
"Trans_to_Male",
|
157 |
"Trans_to_Female",
|
|
|
161 |
],
|
162 |
value="Trans_to_Male",
|
163 |
)
|
164 |
+
furry_species_box = gr.Dropdown(
|
|
|
|
|
165 |
label="Furry 物种 (Furry Species)",
|
166 |
+
choices=ALL_FURRY_SPECIES,
|
167 |
value=None,
|
168 |
visible=False
|
169 |
)
|
170 |
+
def show_furry_spec(g):
|
171 |
+
return gr.update(visible=(g == "Trans_to_Furry"))
|
172 |
+
gender_option.change(show_furry_spec, inputs=[gender_option], outputs=[furry_species_box])
|
|
|
|
|
|
|
|
|
|
|
173 |
|
174 |
with gr.Column():
|
175 |
+
prompt_input = gr.Textbox(
|
|
|
176 |
label="提示词 (Prompt)",
|
177 |
lines=5,
|
178 |
+
placeholder="示例:一位穿红色连衣裙的少女在花园中..."
|
|
|
|
|
179 |
)
|
180 |
+
gen_output = gr.Textbox(
|
181 |
+
label="转换后 (Transformed Prompt)",
|
182 |
+
lines=6
|
|
|
|
|
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="翻译结果 (Translation Result)",
|
194 |
+
lines=6
|
|
|
195 |
)
|
196 |
|
|
|
|
|
|
|
|
|
197 |
def on_generate(prompt, gender, furry, mode, key, lang):
|
198 |
+
# 1) 生成转换后提示词
|
199 |
+
transformed = generate_transformed_prompt(prompt, gender, furry, mode, key)
|
200 |
+
# 2) 翻译
|
201 |
+
translated = translate_prompt_text(transformed, lang, mode, key)
|
202 |
+
return transformed, translated
|
203 |
|
204 |
+
prompt_input.submit(
|
|
|
205 |
fn=on_generate,
|
206 |
+
inputs=[prompt_input, gender_option, furry_species_box, api_mode, api_key, translate_language],
|
207 |
+
outputs=[gen_output, translate_output],
|
208 |
)
|
209 |
+
btn = gr.Button("生成 / Generate")
|
210 |
+
btn.click(
|
|
|
|
|
211 |
fn=on_generate,
|
212 |
+
inputs=[prompt_input, gender_option, furry_species_box, api_mode, api_key, translate_language],
|
213 |
+
outputs=[gen_output, translate_output],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
214 |
)
|
215 |
|
216 |
return demo
|
217 |
|
|
|
|
|
|
|
218 |
if __name__ == "__main__":
|
219 |
+
demo = build_app()
|
220 |
demo.launch()
|