vincenthugging commited on
Commit
893d32f
·
1 Parent(s): c78a910

🔧 修复场景加载错误

Browse files

🐛 Bug Fixes:
- 修复场景下拉菜单默认值为 None 导致的加载错误
- 添加场景选择验证,确保用户先选择场景再加载
- 改进错误处理,使用 Gradio 通知系统提供用户友好的反馈

✨ Enhancements:
- 自动设置第一个场景为默认选中
- 添加调试日志,便于排查场景加载问题
- 使用 gr.Info/gr.Warning/gr.Error 提供即时反馈
- 确保默认示例总是可用,提高稳定性

🔍 Debug Info:
- 添加场景文件检查日志
- 显示加载场景的详细信息
- 统计总可用场景数量

现在用户可以正常选择和加载场景,不会再看到'无法加载场景: None'的错误。

Files changed (1) hide show
  1. app.py +77 -53
app.py CHANGED
@@ -124,8 +124,10 @@ def get_scenario_examples():
124
  # 加载 JSON 文件场景
125
  for key, config in SCENARIO_CONFIG.items():
126
  try:
127
- if os.path.exists(config["file"]):
128
- with open(config["file"], "r", encoding="utf-8") as f:
 
 
129
  data = json.load(f)
130
  scenarios[config["title"]] = {
131
  "text": data.get("text", ""),
@@ -136,28 +138,29 @@ def get_scenario_examples():
136
  "text2": data.get("prompt_text_speaker2", ""),
137
  "base_path": data.get("base_path", ""),
138
  }
 
 
 
139
  except Exception as e:
140
  print(f"⚠️ 加载场景 {key} 失败: {e}")
141
 
142
- # 添加默认示例(如果 JSON 文件不可用)
143
- if not scenarios:
144
- scenarios = {
145
- "🎧 默认示例": {
146
- "text": (
147
- "[S1]大家好,欢迎收听今天的节目,我是主播小雨。"
148
- "[S2]大家好,我是嘉宾阿明,很高兴和大家见面。"
149
- "[S1]今天我们要聊的话题非常有趣,相信大家会喜欢的。"
150
- "[S2]是的,让我们开始今天的精彩内容吧!"
151
- ),
152
- "description": "默认的示例对话,适合快速体验",
153
- "audio1": DEFAULT_AUDIO_CONFIG["speaker1"]["audio"],
154
- "text1": DEFAULT_AUDIO_CONFIG["speaker1"]["text"],
155
- "audio2": DEFAULT_AUDIO_CONFIG["speaker2"]["audio"],
156
- "text2": DEFAULT_AUDIO_CONFIG["speaker2"]["text"],
157
- "base_path": "",
158
- }
159
- }
160
 
 
161
  return scenarios
162
 
163
 
@@ -430,9 +433,13 @@ def create_space_ui() -> gr.Blocks:
430
 
431
  with gr.Group():
432
  gr.Markdown("### 🚀 快速操作")
 
 
 
 
433
  scenario_dropdown = gr.Dropdown(
434
- choices=list(get_scenario_examples().keys()),
435
- value=None,
436
  label="🎭 选择场景",
437
  info="选择一个预设场景,自动填充对话文本和参考音频"
438
  )
@@ -503,44 +510,61 @@ def create_space_ui() -> gr.Blocks:
503
  # ===== 交互逻辑 =====
504
  def on_load_scenario(name: str):
505
  """加载选中的场景,包括文本和音频"""
 
 
 
 
506
  scenarios = get_scenario_examples()
507
  if name not in scenarios:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
508
  return (
509
- f" 无法加载场景: {name}",
510
- None, "", None, ""
 
 
 
511
  )
512
-
513
- scenario = scenarios[name]
514
-
515
- # 处理音频路径
516
- audio1_path = None
517
- audio2_path = None
518
-
519
- if scenario.get("audio1"):
520
- audio1_full = scenario["audio1"]
521
- if scenario.get("base_path") and not audio1_full.startswith("/"):
522
- audio1_full = os.path.join(scenario["base_path"], audio1_full)
523
- if os.path.exists(audio1_full):
524
- audio1_path = audio1_full
525
-
526
- if scenario.get("audio2"):
527
- audio2_full = scenario["audio2"]
528
- if scenario.get("base_path") and not audio2_full.startswith("/"):
529
- audio2_full = os.path.join(scenario["base_path"], audio2_full)
530
- if os.path.exists(audio2_full):
531
- audio2_path = audio2_full
532
-
533
- return (
534
- scenario.get("text", ""),
535
- audio1_path,
536
- scenario.get("text1", ""),
537
- audio2_path,
538
- scenario.get("text2", "")
539
- )
540
 
541
  def on_load_default():
542
  """加载默认音频和文本"""
543
- return load_default_audio()
 
 
 
 
 
 
544
 
545
  btn_load_scenario.click(
546
  fn=on_load_scenario,
 
124
  # 加载 JSON 文件场景
125
  for key, config in SCENARIO_CONFIG.items():
126
  try:
127
+ file_path = config["file"]
128
+ print(f"🔍 检查场景文件: {file_path}")
129
+ if os.path.exists(file_path):
130
+ with open(file_path, "r", encoding="utf-8") as f:
131
  data = json.load(f)
132
  scenarios[config["title"]] = {
133
  "text": data.get("text", ""),
 
138
  "text2": data.get("prompt_text_speaker2", ""),
139
  "base_path": data.get("base_path", ""),
140
  }
141
+ print(f"✅ 成功加载场景: {config['title']}")
142
+ else:
143
+ print(f"❌ 场景文件不存在: {file_path}")
144
  except Exception as e:
145
  print(f"⚠️ 加载场景 {key} 失败: {e}")
146
 
147
+ # 添加默认示例(确保总有可用场景)
148
+ scenarios["🎧 默认示例"] = {
149
+ "text": (
150
+ "[S1]大家好,欢迎收听今天的节目,我是主播小雨。"
151
+ "[S2]大家好,我是嘉宾阿明,很高兴和大家见面。"
152
+ "[S1]今天我们要聊的话题非常有趣,相信大家会喜欢的。"
153
+ "[S2]是的,让我们开始今天的精彩内容吧!"
154
+ ),
155
+ "description": "默认的示例对话,适合快速体验",
156
+ "audio1": DEFAULT_AUDIO_CONFIG["speaker1"]["audio"],
157
+ "text1": DEFAULT_AUDIO_CONFIG["speaker1"]["text"],
158
+ "audio2": DEFAULT_AUDIO_CONFIG["speaker2"]["audio"],
159
+ "text2": DEFAULT_AUDIO_CONFIG["speaker2"]["text"],
160
+ "base_path": "",
161
+ }
 
 
 
162
 
163
+ print(f"📊 总共加载了 {len(scenarios)} 个场景")
164
  return scenarios
165
 
166
 
 
433
 
434
  with gr.Group():
435
  gr.Markdown("### 🚀 快速操作")
436
+ # 获取场景选项,设置第一个为默认值
437
+ scenario_choices = list(get_scenario_examples().keys())
438
+ default_scenario = scenario_choices[0] if scenario_choices else None
439
+
440
  scenario_dropdown = gr.Dropdown(
441
+ choices=scenario_choices,
442
+ value=default_scenario,
443
  label="🎭 选择场景",
444
  info="选择一个预设场景,自动填充对话文本和参考音频"
445
  )
 
510
  # ===== 交互逻辑 =====
511
  def on_load_scenario(name: str):
512
  """加载选中的场景,包括文本和音频"""
513
+ if not name or name.strip() == "":
514
+ gr.Warning("⚠️ 请先选择一个场景")
515
+ return gr.update(), gr.update(), gr.update(), gr.update(), gr.update()
516
+
517
  scenarios = get_scenario_examples()
518
  if name not in scenarios:
519
+ gr.Error(f"❌ 场景不存在: {name}")
520
+ return gr.update(), gr.update(), gr.update(), gr.update(), gr.update()
521
+
522
+ try:
523
+ scenario = scenarios[name]
524
+
525
+ # 处理音频路径
526
+ audio1_path = None
527
+ audio2_path = None
528
+
529
+ if scenario.get("audio1"):
530
+ audio1_full = scenario["audio1"]
531
+ if scenario.get("base_path") and not audio1_full.startswith("/"):
532
+ audio1_full = os.path.join(scenario["base_path"], audio1_full)
533
+ if os.path.exists(audio1_full):
534
+ audio1_path = audio1_full
535
+ else:
536
+ print(f"⚠️ 音频文件不存在: {audio1_full}")
537
+
538
+ if scenario.get("audio2"):
539
+ audio2_full = scenario["audio2"]
540
+ if scenario.get("base_path") and not audio2_full.startswith("/"):
541
+ audio2_full = os.path.join(scenario["base_path"], audio2_full)
542
+ if os.path.exists(audio2_full):
543
+ audio2_path = audio2_full
544
+ else:
545
+ print(f"⚠️ 音频文件不存在: {audio2_full}")
546
+
547
+ gr.Info(f"✅ 成功加载场景: {name}")
548
  return (
549
+ scenario.get("text", ""),
550
+ audio1_path,
551
+ scenario.get("text1", ""),
552
+ audio2_path,
553
+ scenario.get("text2", "")
554
  )
555
+ except Exception as e:
556
+ gr.Error(f"❌ 加载场景时出错: {str(e)}")
557
+ return gr.update(), gr.update(), gr.update(), gr.update(), gr.update()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
558
 
559
  def on_load_default():
560
  """加载默认音频和文本"""
561
+ try:
562
+ result = load_default_audio()
563
+ gr.Info("✅ 成功加载默认音频和文本")
564
+ return result
565
+ except Exception as e:
566
+ gr.Error(f"❌ 加载默认音频时出错: {str(e)}")
567
+ return gr.update(), gr.update(), gr.update(), gr.update(), gr.update()
568
 
569
  btn_load_scenario.click(
570
  fn=on_load_scenario,