update
Browse files
app.py
CHANGED
|
@@ -1,19 +1,19 @@
|
|
| 1 |
import os
|
| 2 |
import shutil
|
| 3 |
import tempfile
|
| 4 |
-
import spaces
|
| 5 |
import gradio as gr
|
| 6 |
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
| 7 |
from fastapi.responses import JSONResponse
|
| 8 |
from faster_whisper import WhisperModel
|
| 9 |
|
| 10 |
-
# 1. 声明加载的模型大小
|
| 11 |
MODEL_SIZE = "small"
|
| 12 |
|
| 13 |
-
# 2. 初始化模型(将其载
|
| 14 |
model = WhisperModel(MODEL_SIZE, device="cpu", compute_type="float32")
|
| 15 |
|
| 16 |
-
# 3. 核心
|
| 17 |
@spaces.GPU
|
| 18 |
def transcribe_core(audio_path: str):
|
| 19 |
# 动态将模型放到 GPU 上执行推理
|
|
@@ -24,15 +24,9 @@ def transcribe_core(audio_path: str):
|
|
| 24 |
text = "".join([segment.text for segment in segments])
|
| 25 |
return text
|
| 26 |
|
| 27 |
-
# 4. 创建 Gradio 界面
|
| 28 |
-
def gradio_predict(audio_path):
|
| 29 |
-
if audio_path is None:
|
| 30 |
-
return "请先上传音频或录音!"
|
| 31 |
-
return transcribe_core(audio_path)
|
| 32 |
-
|
| 33 |
-
# ============ 替换第 33 行及之后的所有代码 ============
|
| 34 |
demo = gr.Interface(
|
| 35 |
-
fn=
|
| 36 |
inputs=gr.Audio(sources=["microphone", "upload"], type="filepath", label="输入音频"),
|
| 37 |
outputs=gr.Textbox(label="识别出的文本"),
|
| 38 |
title="Whisper 语音识别 API 节点",
|
|
|
|
| 1 |
import os
|
| 2 |
import shutil
|
| 3 |
import tempfile
|
| 4 |
+
import spaces
|
| 5 |
import gradio as gr
|
| 6 |
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
| 7 |
from fastapi.responses import JSONResponse
|
| 8 |
from faster_whisper import WhisperModel
|
| 9 |
|
| 10 |
+
# 1. 声明加载的模型大小
|
| 11 |
MODEL_SIZE = "small"
|
| 12 |
|
| 13 |
+
# 2. 初始化模型(将其加载进内存,优先检测CPU以防止启动时报错)
|
| 14 |
model = WhisperModel(MODEL_SIZE, device="cpu", compute_type="float32")
|
| 15 |
|
| 16 |
+
# 3. 核心计算函数(加上 @spaces.GPU 装饰器使用 A100/A10G 算力)
|
| 17 |
@spaces.GPU
|
| 18 |
def transcribe_core(audio_path: str):
|
| 19 |
# 动态将模型放到 GPU 上执行推理
|
|
|
|
| 24 |
text = "".join([segment.text for segment in segments])
|
| 25 |
return text
|
| 26 |
|
| 27 |
+
# 4. 创建 Gradio 界面并直接绑定带有 @spaces.GPU 装饰器的函数
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
demo = gr.Interface(
|
| 29 |
+
fn=transcribe_core, # 直接将带有 GPU 装饰器的函数绑定给 Gradio 的 fn
|
| 30 |
inputs=gr.Audio(sources=["microphone", "upload"], type="filepath", label="输入音频"),
|
| 31 |
outputs=gr.Textbox(label="识别出的文本"),
|
| 32 |
title="Whisper 语音识别 API 节点",
|