PSNbst commited on
Commit
05bf4a2
·
verified ·
1 Parent(s): 14b159d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -22
app.py CHANGED
@@ -1,4 +1,4 @@
1
- import os
2
  import gradio as gr
3
  import openai
4
 
@@ -54,18 +54,14 @@ def generate_natural_language_description_deepseek(tags, api_key, base_url=None)
54
  if not api_key:
55
  return "Error: DeepSeek API Key not provided."
56
 
57
- # 伪代码示例
58
- # ----------------------------------
59
  # import requests
60
- # ...
61
  # response = requests.post(
62
  # url=base_url or "https://api.deepseek.com/xxx",
63
  # headers={"Authorization": f"Bearer {api_key}"},
64
  # json={"tags": tags}
65
  # )
66
  # return response.json()["description"]
67
- # ----------------------------------
68
- # 这里为了演示,就直接返回简单的字符串
69
  return "DeepSeek 生成的描述(此处为示例伪代码)"
70
 
71
 
@@ -84,7 +80,6 @@ def translate_text_with_gpt(text, target_language, api_key, base_url=None, model
84
  openai.api_base = base_url
85
 
86
  try:
87
- # 通过系统提示,让 GPT 做翻译
88
  system_prompt = f"You are a professional translator. Translate the following text to {target_language}:"
89
  response = openai.ChatCompletion.create(
90
  model=model,
@@ -104,7 +99,7 @@ def translate_text_with_deepseek(text, target_language, api_key, base_url=None):
104
  """
105
  if not api_key:
106
  return "Error: DeepSeek Translation Key not provided."
107
- # 类似上面的伪代码方式。
108
  return f"DeepSeek翻译后的文本(示例)。目标语言:{target_language}"
109
 
110
 
@@ -114,15 +109,18 @@ def translate_text_with_deepseek(text, target_language, api_key, base_url=None):
114
  def transform_prompt(prompt, gender_option, furry_species, api_mode, api_key):
115
  """
116
  性别/物种转换的简单示例逻辑,然后调用相应 API。
 
117
  """
118
  tags = {}
119
 
120
  # 根据选择设置性别或物种标签
121
  if gender_option == "Trans_to_Male":
 
122
  tags["gender"] = "male"
123
  elif gender_option == "Trans_to_Female":
124
  tags["gender"] = "female"
125
  elif gender_option == "Trans_to_Mannequin":
 
126
  tags["gender"] = "genderless"
127
  elif gender_option == "Trans_to_Intersex":
128
  tags["gender"] = "intersex"
@@ -150,7 +148,7 @@ def do_translation(scene_desc, translate_language, api_mode, api_key):
150
  根据选择的 API(GPT/DeepSeek)进行翻译。
151
  """
152
  if not scene_desc.strip():
153
- return ""
154
 
155
  if api_mode == "GPT":
156
  return translate_text_with_gpt(scene_desc, translate_language, api_key)
@@ -205,7 +203,7 @@ def build_interface():
205
 
206
  # 当性别选项切换时,如果选择 Furry,就显示物种下拉,否则隐藏
207
  def show_furry_species(gender):
208
- return gr.update(visible=(gender == "Furry"))
209
 
210
  gender_option.change(
211
  show_furry_species,
@@ -218,7 +216,10 @@ def build_interface():
218
  user_prompt = gr.Textbox(
219
  label="提示词 (Prompt)",
220
  lines=5,
221
- placeholder="Please Enter your prompt words. 在此输入你的提示词,例如:一位穿着红色连衣裙的少女,坐在落日余晖下的草地上..."
 
 
 
222
  )
223
 
224
  # 输出场景描述
@@ -231,6 +232,7 @@ def build_interface():
231
  with gr.Row():
232
  translate_language = gr.Dropdown(
233
  label="翻译语言 (Translation Language)",
 
234
  choices=["English", "Chinese", "Japanese", "French", "German", "Dutch", "Arabic", "Russian", "Persian", "Italian"],
235
  value="English",
236
  )
@@ -242,28 +244,36 @@ def build_interface():
242
  ######################################################################
243
  # 事件绑定
244
  ######################################################################
245
- def on_generate(prompt, gender, furry, mode, key):
246
- return transform_prompt(prompt, gender, furry, mode, key)
247
-
248
- # 用户在 prompt 输入后按回车或失去焦点时,触发生成场景描述
 
 
 
 
 
 
 
249
  user_prompt.submit(
250
  fn=on_generate,
251
- inputs=[user_prompt, gender_option, furry_species, api_mode, api_key],
252
- outputs=[generated_output],
253
  )
254
 
255
- # 也可以加个按钮
256
  generate_button = gr.Button("生成 / Generate")
257
  generate_button.click(
258
  fn=on_generate,
259
- inputs=[user_prompt, gender_option, furry_species, api_mode, api_key],
260
- outputs=[generated_output],
261
  )
262
 
263
- # 当用户切换翻译语言时,自动翻译当前生成文本
264
  def on_translate(scene_desc, lang, mode, key):
265
  return do_translation(scene_desc, lang, mode, key)
266
 
 
267
  translate_language.change(
268
  fn=on_translate,
269
  inputs=[generated_output, translate_language, api_mode, api_key],
@@ -276,4 +286,4 @@ def build_interface():
276
  # 在 Spaces 启动
277
  if __name__ == "__main__":
278
  demo = build_interface()
279
- demo.launch()
 
1
+ import os
2
  import gradio as gr
3
  import openai
4
 
 
54
  if not api_key:
55
  return "Error: DeepSeek API Key not provided."
56
 
57
+ # 伪代码示例(需根据你的 DeepSeek API 文档做实际实现)
 
58
  # import requests
 
59
  # response = requests.post(
60
  # url=base_url or "https://api.deepseek.com/xxx",
61
  # headers={"Authorization": f"Bearer {api_key}"},
62
  # json={"tags": tags}
63
  # )
64
  # return response.json()["description"]
 
 
65
  return "DeepSeek 生成的描述(此处为示例伪代码)"
66
 
67
 
 
80
  openai.api_base = base_url
81
 
82
  try:
 
83
  system_prompt = f"You are a professional translator. Translate the following text to {target_language}:"
84
  response = openai.ChatCompletion.create(
85
  model=model,
 
99
  """
100
  if not api_key:
101
  return "Error: DeepSeek Translation Key not provided."
102
+ # 同样需要根据 DeepSeek 的文档来实现
103
  return f"DeepSeek翻译后的文本(示例)。目标语言:{target_language}"
104
 
105
 
 
109
  def transform_prompt(prompt, gender_option, furry_species, api_mode, api_key):
110
  """
111
  性别/物种转换的简单示例逻辑,然后调用相应 API。
112
+ 你可在此处结合“关于Male/Female/ambiguous/intersex的details”添加更复杂的处理。
113
  """
114
  tags = {}
115
 
116
  # 根据选择设置性别或物种标签
117
  if gender_option == "Trans_to_Male":
118
+ # 这里可以参考你的细节 rules 做更加复杂的转换
119
  tags["gender"] = "male"
120
  elif gender_option == "Trans_to_Female":
121
  tags["gender"] = "female"
122
  elif gender_option == "Trans_to_Mannequin":
123
+ # 无明显二性征
124
  tags["gender"] = "genderless"
125
  elif gender_option == "Trans_to_Intersex":
126
  tags["gender"] = "intersex"
 
148
  根据选择的 API(GPT/DeepSeek)进行翻译。
149
  """
150
  if not scene_desc.strip():
151
+ return "" # 无内容则不翻译
152
 
153
  if api_mode == "GPT":
154
  return translate_text_with_gpt(scene_desc, translate_language, api_key)
 
203
 
204
  # 当性别选项切换时,如果选择 Furry,就显示物种下拉,否则隐藏
205
  def show_furry_species(gender):
206
+ return gr.update(visible=(gender == "Trans_to_Furry"))
207
 
208
  gender_option.change(
209
  show_furry_species,
 
216
  user_prompt = gr.Textbox(
217
  label="提示词 (Prompt)",
218
  lines=5,
219
+ placeholder=(
220
+ "Please Enter your prompt words. \n"
221
+ "在此输入你的提示词,例如:一位穿着红色连衣裙的少女,坐在落日余晖下的草地上..."
222
+ )
223
  )
224
 
225
  # 输出场景描述
 
232
  with gr.Row():
233
  translate_language = gr.Dropdown(
234
  label="翻译语言 (Translation Language)",
235
+ # 可自行添加更多语言选项
236
  choices=["English", "Chinese", "Japanese", "French", "German", "Dutch", "Arabic", "Russian", "Persian", "Italian"],
237
  value="English",
238
  )
 
244
  ######################################################################
245
  # 事件绑定
246
  ######################################################################
247
+
248
+ # 新增:生成时,直接返回「转换结果」和「翻译结果」并一起更新
249
+ def on_generate(prompt, gender, furry, mode, key, lang):
250
+ # 1) 先做性别/物种转换,拿到“转换后”提示词
251
+ trans_desc = transform_prompt(prompt, gender, furry, mode, key)
252
+ # 2) 立刻翻译
253
+ trans_result = do_translation(trans_desc, lang, mode, key)
254
+ # 返回两项
255
+ return trans_desc, trans_result
256
+
257
+ # 当用户在 prompt 输入后按回车时,触发生成场景描述 + 翻译
258
  user_prompt.submit(
259
  fn=on_generate,
260
+ inputs=[user_prompt, gender_option, furry_species, api_mode, api_key, translate_language],
261
+ outputs=[generated_output, translated_text],
262
  )
263
 
264
+ # 点击按钮也触发同样的逻辑
265
  generate_button = gr.Button("生成 / Generate")
266
  generate_button.click(
267
  fn=on_generate,
268
+ inputs=[user_prompt, gender_option, furry_species, api_mode, api_key, translate_language],
269
+ outputs=[generated_output, translated_text],
270
  )
271
 
272
+ # 当用户切换翻译语言时,如果已经有转换后的内容,则再翻译一次
273
  def on_translate(scene_desc, lang, mode, key):
274
  return do_translation(scene_desc, lang, mode, key)
275
 
276
+ # 注意:这里只更新“翻译结果”一项
277
  translate_language.change(
278
  fn=on_translate,
279
  inputs=[generated_output, translate_language, api_mode, api_key],
 
286
  # 在 Spaces 启动
287
  if __name__ == "__main__":
288
  demo = build_interface()
289
+ demo.launch()