Spaces:
Running
Running
Upload api.py with huggingface_hub
Browse files
api.py
CHANGED
|
@@ -4,6 +4,7 @@ import torch
|
|
| 4 |
import torchaudio
|
| 5 |
import numpy as np
|
| 6 |
from fastapi import FastAPI, UploadFile, File, Header, HTTPException, status, Form
|
|
|
|
| 7 |
from funasr import AutoModel
|
| 8 |
from funasr.utils.postprocess_utils import rich_transcription_postprocess
|
| 9 |
|
|
@@ -21,7 +22,7 @@ VAD_MODEL_LOCAL_PATH = os.path.join(MODEL_CACHE_DIR, "fsmn-vad")
|
|
| 21 |
device = "cpu"
|
| 22 |
|
| 23 |
print(f"Loading SenseVoice model on {device}...")
|
| 24 |
-
|
| 25 |
model=SENSE_VOICE_SMALL_LOCAL_PATH,
|
| 26 |
trust_remote_code=False,
|
| 27 |
vad_model=VAD_MODEL_LOCAL_PATH,
|
|
@@ -33,6 +34,12 @@ model = AutoModel(
|
|
| 33 |
print("Model loaded successfully.")
|
| 34 |
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
@app.post("/transcribe")
|
| 37 |
async def transcribe(
|
| 38 |
file: UploadFile = File(...),
|
|
@@ -59,7 +66,7 @@ async def transcribe(
|
|
| 59 |
|
| 60 |
try:
|
| 61 |
# Generate transcription
|
| 62 |
-
res =
|
| 63 |
input=audio_path,
|
| 64 |
cache={},
|
| 65 |
language="auto",
|
|
@@ -93,7 +100,7 @@ async def openai_audio_transcriptions(
|
|
| 93 |
|
| 94 |
Request (multipart/form-data):
|
| 95 |
- file: audio file
|
| 96 |
-
- model: optional model identifier
|
| 97 |
- response_format: json | text | srt | vtt (default json)
|
| 98 |
|
| 99 |
Response:
|
|
@@ -118,7 +125,7 @@ async def openai_audio_transcriptions(
|
|
| 118 |
audio_path = tmp.name
|
| 119 |
|
| 120 |
try:
|
| 121 |
-
res =
|
| 122 |
input=audio_path,
|
| 123 |
cache={},
|
| 124 |
language="auto",
|
|
@@ -138,7 +145,6 @@ async def openai_audio_transcriptions(
|
|
| 138 |
os.unlink(audio_path)
|
| 139 |
|
| 140 |
if fmt == "text":
|
| 141 |
-
from fastapi.responses import PlainTextResponse
|
| 142 |
return PlainTextResponse(content=text)
|
| 143 |
|
| 144 |
return {"text": text}
|
|
|
|
| 4 |
import torchaudio
|
| 5 |
import numpy as np
|
| 6 |
from fastapi import FastAPI, UploadFile, File, Header, HTTPException, status, Form
|
| 7 |
+
from fastapi.responses import PlainTextResponse
|
| 8 |
from funasr import AutoModel
|
| 9 |
from funasr.utils.postprocess_utils import rich_transcription_postprocess
|
| 10 |
|
|
|
|
| 22 |
device = "cpu"
|
| 23 |
|
| 24 |
print(f"Loading SenseVoice model on {device}...")
|
| 25 |
+
asr_model = AutoModel(
|
| 26 |
model=SENSE_VOICE_SMALL_LOCAL_PATH,
|
| 27 |
trust_remote_code=False,
|
| 28 |
vad_model=VAD_MODEL_LOCAL_PATH,
|
|
|
|
| 34 |
print("Model loaded successfully.")
|
| 35 |
|
| 36 |
|
| 37 |
+
@app.get("/")
|
| 38 |
+
def read_root():
|
| 39 |
+
"""Hugging Face Space health check endpoint."""
|
| 40 |
+
return {"status": "ok", "message": "SenseVoice ASR API is running"}
|
| 41 |
+
|
| 42 |
+
|
| 43 |
@app.post("/transcribe")
|
| 44 |
async def transcribe(
|
| 45 |
file: UploadFile = File(...),
|
|
|
|
| 66 |
|
| 67 |
try:
|
| 68 |
# Generate transcription
|
| 69 |
+
res = asr_model.generate(
|
| 70 |
input=audio_path,
|
| 71 |
cache={},
|
| 72 |
language="auto",
|
|
|
|
| 100 |
|
| 101 |
Request (multipart/form-data):
|
| 102 |
- file: audio file
|
| 103 |
+
- model: optional model identifier (ignored, uses SenseVoice)
|
| 104 |
- response_format: json | text | srt | vtt (default json)
|
| 105 |
|
| 106 |
Response:
|
|
|
|
| 125 |
audio_path = tmp.name
|
| 126 |
|
| 127 |
try:
|
| 128 |
+
res = asr_model.generate(
|
| 129 |
input=audio_path,
|
| 130 |
cache={},
|
| 131 |
language="auto",
|
|
|
|
| 145 |
os.unlink(audio_path)
|
| 146 |
|
| 147 |
if fmt == "text":
|
|
|
|
| 148 |
return PlainTextResponse(content=text)
|
| 149 |
|
| 150 |
return {"text": text}
|