sofiamak commited on
Commit
ab34c21
·
verified ·
1 Parent(s): 20cb253

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -34
app.py CHANGED
@@ -109,7 +109,7 @@ FEWSHOT = [
109
  ]
110
 
111
  # ===================== 调用 DeepSeek:主分析 =====================
112
- def call_deepseek(user_question: str, alt: bool = False) -> dict:
113
  """自动尝试两个端点,返回结构化兵法解析结果。"""
114
  key = os.getenv("DEEPSEEK_API_KEY", "").strip()
115
  if not key:
@@ -148,7 +148,7 @@ def call_deepseek(user_question: str, alt: bool = False) -> dict:
148
  payload = {
149
  "model": MODEL_NAME,
150
  "messages": msgs,
151
- "temperature": 0.9 if alt else 0.7,
152
  "top_p": 0.9,
153
  "max_tokens": 450
154
  }
@@ -258,7 +258,7 @@ def translate_to_en(text_cn: str) -> str:
258
  def render_main(result: dict) -> str:
259
  """
260
  主体渲染(不包含话术示例、不包含“我是老外”解释)。
261
- 严格校验:若 unsure=True 则隐藏原文与篇名。
262
  """
263
  unsure = bool(result.get("unsure"))
264
  chap = html.escape(result.get("chapter", ""))
@@ -266,7 +266,7 @@ def render_main(result: dict) -> str:
266
  insight = html.escape(result.get("insight", ""))
267
  action = html.escape(result.get("action", ""))
268
 
269
- # 现在默认“严格出处校验”:模型不确定时,隐藏原文与篇名
270
  show_quote = not unsure
271
 
272
  parts = ["<div style='font-size:22px;line-height:1.7em;'>"]
@@ -350,15 +350,16 @@ with gr.Blocks(title=APP_TITLE, theme=theme, css=CSS) as demo:
350
  # 保存最近一次 DeepSeek 返回的结构化结果,用于“话术示例”和“我是老外”
351
  state_result = gr.State({})
352
 
 
353
  with gr.Row():
354
  b1 = gr.Button("🎯 生成兵法", variant="primary")
355
- b2 = gr.Button("♻️ 换一种说法")
356
  b3 = gr.Button("🧹 清空")
357
 
 
358
  with gr.Row():
359
- btn_speech = gr.Button("💬 话术示例")
360
- btn_foreign = gr.Button("🌏 我是老外")
361
- btn_translate = gr.Button("🔁 翻译成英文")
362
 
363
  out_main = gr.HTML()
364
  out_speech = gr.HTML()
@@ -367,31 +368,35 @@ with gr.Blocks(title=APP_TITLE, theme=theme, css=CSS) as demo:
367
 
368
  # ---------- 回调函数 ----------
369
  def on_gen(user_q):
 
 
 
370
  if not (user_q or "").strip():
371
  empty_msg = (
372
  "<div style='font-size:16px;color:#777;'>"
373
  "请先在上面输入你的问题,比如:朋友借钱不还怎么办?</div>"
374
  )
375
- return empty_msg, "", "", "", {}
376
- res = call_deepseek(user_q, alt=False)
377
- main_html = render_main(res)
378
- # 生成新结果时,清空话术示例和解释区域
379
- return main_html, "", "", "", res
380
 
381
- def on_alt(user_q, cur_state):
382
- if not (user_q or "").strip():
383
- empty_msg = (
384
- "<div style='font-size:16px;color:#777;'>"
385
- "请先在上面输入你的问题,比如:朋友借钱不还怎么办?</div>"
386
- )
387
- return empty_msg, "", "", "", {}
388
- res = call_deepseek(user_q, alt=True)
389
  main_html = render_main(res)
390
- return main_html, "", "", "", res
 
 
 
 
 
 
 
 
 
 
391
 
392
  def on_clear():
393
- # 清空输入框和所有输出 & 状态
394
- return "", "", "", "", "", {}
 
395
 
396
  def on_show_speech(res):
397
  if not isinstance(res, dict) or not res:
@@ -402,12 +407,14 @@ with gr.Blocks(title=APP_TITLE, theme=theme, css=CSS) as demo:
402
  return render_speech(res)
403
 
404
  def on_show_foreign(res):
 
405
  if not isinstance(res, dict) or not res:
406
- return (
407
  "<div style='margin-top:8px;font-size:16px;color:#777;'>"
408
  "请先点击「生成兵法」,再点「我是老外」。</div>"
409
  )
410
- return render_explain_cn(res)
 
411
 
412
  def on_translate(res):
413
  if not isinstance(res, dict) or not res:
@@ -428,17 +435,32 @@ with gr.Blocks(title=APP_TITLE, theme=theme, css=CSS) as demo:
428
  b1.click(
429
  on_gen,
430
  inputs=[q],
431
- outputs=[out_main, out_speech, out_explain_cn, out_explain_en, state_result]
432
- )
433
- b2.click(
434
- on_alt,
435
- inputs=[q, state_result],
436
- outputs=[out_main, out_speech, out_explain_cn, out_explain_en, state_result]
 
 
 
 
437
  )
 
438
  b3.click(
439
  on_clear,
440
  inputs=None,
441
- outputs=[q, out_main, out_speech, out_explain_cn, out_explain_en, state_result]
 
 
 
 
 
 
 
 
 
 
442
  )
443
 
444
  btn_speech.click(
@@ -450,7 +472,7 @@ with gr.Blocks(title=APP_TITLE, theme=theme, css=CSS) as demo:
450
  btn_foreign.click(
451
  on_show_foreign,
452
  inputs=[state_result],
453
- outputs=[out_explain_cn]
454
  )
455
 
456
  btn_translate.click(
 
109
  ]
110
 
111
  # ===================== 调用 DeepSeek:主分析 =====================
112
+ def call_deepseek(user_question: str) -> dict:
113
  """自动尝试两个端点,返回结构化兵法解析结果。"""
114
  key = os.getenv("DEEPSEEK_API_KEY", "").strip()
115
  if not key:
 
148
  payload = {
149
  "model": MODEL_NAME,
150
  "messages": msgs,
151
+ "temperature": 0.7,
152
  "top_p": 0.9,
153
  "max_tokens": 450
154
  }
 
258
  def render_main(result: dict) -> str:
259
  """
260
  主体渲染(不包含话术示例、不包含“我是老外”解释)。
261
+ 默认严格校验:若 unsure=True 则隐藏原文与篇名。
262
  """
263
  unsure = bool(result.get("unsure"))
264
  chap = html.escape(result.get("chapter", ""))
 
266
  insight = html.escape(result.get("insight", ""))
267
  action = html.escape(result.get("action", ""))
268
 
269
+ # 默认“严格出处校验”:模型不确定时,隐藏原文与篇名
270
  show_quote = not unsure
271
 
272
  parts = ["<div style='font-size:22px;line-height:1.7em;'>"]
 
350
  # 保存最近一次 DeepSeek 返回的结构化结果,用于“话术示例”和“我是老外”
351
  state_result = gr.State({})
352
 
353
+ # 首页:只显示两个按钮
354
  with gr.Row():
355
  b1 = gr.Button("🎯 生成兵法", variant="primary")
 
356
  b3 = gr.Button("🧹 清空")
357
 
358
+ # 下面这些按钮一开始不显示,生成兵法后再出现
359
  with gr.Row():
360
+ btn_speech = gr.Button("💬 话术示例", visible=False)
361
+ btn_foreign = gr.Button("🌏 我是老外", visible=False)
362
+ btn_translate = gr.Button("🔁 翻译成英文", visible=False)
363
 
364
  out_main = gr.HTML()
365
  out_speech = gr.HTML()
 
368
 
369
  # ---------- 回调函数 ----------
370
  def on_gen(user_q):
371
+ # 返回顺序:
372
+ # out_main, out_speech, out_explain_cn, out_explain_en, state_result,
373
+ # btn_speech_update, btn_foreign_update, btn_translate_update
374
  if not (user_q or "").strip():
375
  empty_msg = (
376
  "<div style='font-size:16px;color:#777;'>"
377
  "请先在上面输入你的问题,比如:朋友借钱不还怎么办?</div>"
378
  )
379
+ hide = gr.update(visible=False)
380
+ return empty_msg, "", "", "", {}, hide, hide, hide
 
 
 
381
 
382
+ res = call_deepseek(user_q)
 
 
 
 
 
 
 
383
  main_html = render_main(res)
384
+ # 生成新结果时,显示「话术示例」「我是老外」,但先不显示「翻译成英文」
385
+ return (
386
+ main_html,
387
+ "",
388
+ "",
389
+ "",
390
+ res,
391
+ gr.update(visible=True), # btn_speech
392
+ gr.update(visible=True), # btn_foreign
393
+ gr.update(visible=False), # btn_translate
394
+ )
395
 
396
  def on_clear():
397
+ # 清空输入框和所有输出 & 状态,并隐藏所有二级按钮
398
+ hide = gr.update(visible=False)
399
+ return "", "", "", "", "", {}, hide, hide, hide
400
 
401
  def on_show_speech(res):
402
  if not isinstance(res, dict) or not res:
 
407
  return render_speech(res)
408
 
409
  def on_show_foreign(res):
410
+ # 返回:out_explain_cn, btn_translate_update
411
  if not isinstance(res, dict) or not res:
412
+ msg = (
413
  "<div style='margin-top:8px;font-size:16px;color:#777;'>"
414
  "请先点击「生成兵法」,再点「我是老外」。</div>"
415
  )
416
+ return msg, gr.update(visible=False)
417
+ return render_explain_cn(res), gr.update(visible=True)
418
 
419
  def on_translate(res):
420
  if not isinstance(res, dict) or not res:
 
435
  b1.click(
436
  on_gen,
437
  inputs=[q],
438
+ outputs=[
439
+ out_main,
440
+ out_speech,
441
+ out_explain_cn,
442
+ out_explain_en,
443
+ state_result,
444
+ btn_speech,
445
+ btn_foreign,
446
+ btn_translate,
447
+ ]
448
  )
449
+
450
  b3.click(
451
  on_clear,
452
  inputs=None,
453
+ outputs=[
454
+ q,
455
+ out_main,
456
+ out_speech,
457
+ out_explain_cn,
458
+ out_explain_en,
459
+ state_result,
460
+ btn_speech,
461
+ btn_foreign,
462
+ btn_translate,
463
+ ]
464
  )
465
 
466
  btn_speech.click(
 
472
  btn_foreign.click(
473
  on_show_foreign,
474
  inputs=[state_result],
475
+ outputs=[out_explain_cn, btn_translate]
476
  )
477
 
478
  btn_translate.click(