Spaces:
Sleeping
Sleeping
update
Browse files- app.py +39 -0
- requirements.txt +18 -0
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# 加载模型
|
| 6 |
+
pipe = pipeline(
|
| 7 |
+
task="automatic-speech-recognition",
|
| 8 |
+
model="fj11/whisper-ja-zh-tiny",
|
| 9 |
+
return_timestamps=True,
|
| 10 |
+
generate_kwargs={"task": "translate", "language": "japanese"}
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
MAX_FILE_SIZE_MB = 10
|
| 14 |
+
|
| 15 |
+
def transcribe(audio_path):
|
| 16 |
+
if audio_path is None:
|
| 17 |
+
return "请上传音频文件"
|
| 18 |
+
|
| 19 |
+
# 检查文件大小
|
| 20 |
+
file_size_mb = os.path.getsize(audio_path) / (1024 * 1024)
|
| 21 |
+
if file_size_mb > MAX_FILE_SIZE_MB:
|
| 22 |
+
return f"❌ 文件太大:{file_size_mb:.2f} MB,最大只支持 {MAX_FILE_SIZE_MB} MB"
|
| 23 |
+
|
| 24 |
+
# 执行转录
|
| 25 |
+
result = pipe(audio_path)
|
| 26 |
+
return result["text"]
|
| 27 |
+
|
| 28 |
+
# Gradio UI
|
| 29 |
+
demo = gr.Interface(
|
| 30 |
+
fn=transcribe,
|
| 31 |
+
inputs=gr.Audio(source="upload", type="filepath", label="上传日语音频文件(≤10MB)"),
|
| 32 |
+
outputs=gr.Textbox(label="翻译后的中文文本"),
|
| 33 |
+
title="🎙️ Whisper ja→zh 语音翻译模型",
|
| 34 |
+
description="上传最大 10MB 的日语语音,输出中文翻译结果。",
|
| 35 |
+
allow_flagging="never"
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
if __name__ == "__main__":
|
| 39 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
certifi==2025.4.26
|
| 2 |
+
charset-normalizer==3.4.2
|
| 3 |
+
filelock==3.18.0
|
| 4 |
+
fsspec==2025.5.1
|
| 5 |
+
hf-xet==1.1.3
|
| 6 |
+
huggingface-hub==0.33.0
|
| 7 |
+
idna==3.10
|
| 8 |
+
numpy==2.3.0
|
| 9 |
+
packaging==25.0
|
| 10 |
+
PyYAML==6.0.2
|
| 11 |
+
regex==2024.11.6
|
| 12 |
+
requests==2.32.4
|
| 13 |
+
safetensors==0.5.3
|
| 14 |
+
tokenizers==0.21.1
|
| 15 |
+
tqdm==4.67.1
|
| 16 |
+
transformers==4.52.4
|
| 17 |
+
typing_extensions==4.14.0
|
| 18 |
+
urllib3==2.4.0
|