Update app.py
Browse files
app.py
CHANGED
|
@@ -2,22 +2,15 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
from gradio_client import Client, handle_file
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 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
|
| 18 |
if _whisper_client is None:
|
| 19 |
-
|
| 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 |
-
#
|
| 38 |
-
# 同时兼容不同 Space 的入参命名 (inputs 或 input_audio)
|
| 39 |
try:
|
| 40 |
result = client.predict(
|
| 41 |
inputs=handle_file(audio_path),
|
| 42 |
task="transcribe",
|
|
|
|
| 43 |
)
|
| 44 |
-
except
|
| 45 |
-
#
|
| 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"⚠️
|
| 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}"})
|