fdbw commited on
Commit
2fce972
·
verified ·
1 Parent(s): 76f7f95

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -19
app.py CHANGED
@@ -2,22 +2,15 @@ import gradio as gr
2
  import requests
3
  from gradio_client import Client, handle_file
4
 
5
- # 定义多个备用免费 Whisper Space 节点,防止单个节点维护或排队
6
- WHISPER_SPACES = [
7
- "hf-audio/whisper-large-v3",
8
- "openai/whisper-large-v3",
9
- "sanchit-gandhi/whisper-large-v3",
10
- ]
11
-
12
  _whisper_client = None
13
- _current_space_idx = 0
14
 
15
 
16
  def get_whisper_client():
17
- global _whisper_client, _current_space_idx
18
  if _whisper_client is None:
19
- space_name = WHISPER_SPACES[_current_space_idx]
20
- _whisper_client = Client(space_name)
21
  return _whisper_client
22
 
23
 
@@ -34,24 +27,22 @@ LANG_MAP = {
34
  def transcribe(audio_path):
35
  client = get_whisper_client()
36
 
37
- # 不强制指定 api_name,让 client 自动匹配默认入口
38
- # 同时兼容不同 Space 的入参命名 (inputs 或 input_audio)
39
  try:
40
  result = client.predict(
41
  inputs=handle_file(audio_path),
42
  task="transcribe",
 
43
  )
44
- except TypeError:
45
- # 如果对应的 Space 只有 1 个音频输入参数:
46
  result = client.predict(
47
  handle_file(audio_path),
 
48
  )
49
 
50
- # 提取返回值中的文本部分
51
  if isinstance(result, (list, tuple)):
52
  result = result[0]
53
-
54
- # 某些 space 返回 dict
55
  if isinstance(result, dict) and "text" in result:
56
  result = result["text"]
57
 
@@ -88,7 +79,7 @@ def process(audio_path, mode_label, history):
88
  return history, None
89
  trans = translate(text, cfg["pair"])
90
  except Exception as e:
91
- history.append({"role": "assistant", "content": f"⚠️ 识别接口响应异常 ({e}),请重试或检查网络。"})
92
  return history, None
93
 
94
  history.append({"role": "user", "content": f"[{cfg['src_tag']}] {text}"})
 
2
  import requests
3
  from gradio_client import Client, handle_file
4
 
5
+ # 使用 API 结构极简且稳定的公共 Whisper 空间
6
+ FREE_WHISPER_SPACE = "sanchit-gandhi/whisper-large-v3"
 
 
 
 
 
7
  _whisper_client = None
 
8
 
9
 
10
  def get_whisper_client():
11
+ global _whisper_client
12
  if _whisper_client is None:
13
+ _whisper_client = Client(FREE_WHISPER_SPACE)
 
14
  return _whisper_client
15
 
16
 
 
27
  def transcribe(audio_path):
28
  client = get_whisper_client()
29
 
30
+ # 该 Space 只含一个简洁的 predict 入口,并使用 fn_index=0 确保精准匹配
 
31
  try:
32
  result = client.predict(
33
  inputs=handle_file(audio_path),
34
  task="transcribe",
35
+ fn_index=0,
36
  )
37
+ except Exception:
38
+ # 兼容无 extra 选项的识别调用
39
  result = client.predict(
40
  handle_file(audio_path),
41
+ fn_index=0,
42
  )
43
 
 
44
  if isinstance(result, (list, tuple)):
45
  result = result[0]
 
 
46
  if isinstance(result, dict) and "text" in result:
47
  result = result["text"]
48
 
 
79
  return history, None
80
  trans = translate(text, cfg["pair"])
81
  except Exception as e:
82
+ history.append({"role": "assistant", "content": f"⚠️ 出错了:{e}"})
83
  return history, None
84
 
85
  history.append({"role": "user", "content": f"[{cfg['src_tag']}] {text}"})