megatrump commited on
Commit
f9e54c0
·
1 Parent(s): bb6c236

统一了接口的相应格式

Browse files
Files changed (1) hide show
  1. api.py +45 -11
api.py CHANGED
@@ -13,6 +13,7 @@ import torchaudio
13
  from funasr import AutoModel
14
  from dotenv import load_dotenv
15
  import os
 
16
 
17
  # 加载环境变量
18
  load_dotenv()
@@ -232,24 +233,57 @@ async def transcribe_audio(
232
  language: 语言代码,支持 auto/zh/en/yue/ja/ko/nospeech
233
 
234
  Returns:
235
- {"text": "识别结果"}
 
 
 
 
 
236
  """
237
- if not file.filename.lower().endswith((".mp3", ".wav", ".flac", ".ogg", ".m4a")):
238
- raise HTTPException(status_code=400, detail="不支持的音频格式")
239
-
240
- if model != "FunAudioLLM/SenseVoiceSmall":
241
- raise HTTPException(status_code=400, detail="不支持的模型")
242
-
243
- if language not in ["auto", "zh", "en", "yue", "ja", "ko", "nospeech"]:
244
- raise HTTPException(status_code=400, detail="不支持的语言")
245
 
246
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
247
  content = await file.read()
248
  text = await process_audio(content, language)
249
- return {"text": text}
 
 
 
 
 
 
250
 
251
  except Exception as e:
252
- raise HTTPException(status_code=500, detail=str(e))
 
 
 
 
 
253
 
254
 
255
  @app.get("/", response_class=HTMLResponse)
 
13
  from funasr import AutoModel
14
  from dotenv import load_dotenv
15
  import os
16
+ import time
17
 
18
  # 加载环境变量
19
  load_dotenv()
 
233
  language: 语言代码,支持 auto/zh/en/yue/ja/ko/nospeech
234
 
235
  Returns:
236
+ {
237
+ "text": "识别结果",
238
+ "error_code": 0,
239
+ "error_msg": "",
240
+ "process_time": 1.234 # 处理时间(秒)
241
+ }
242
  """
243
+ start_time = time.time()
 
 
 
 
 
 
 
244
 
245
  try:
246
+ if not file.filename.lower().endswith((".mp3", ".wav", ".flac", ".ogg", ".m4a")):
247
+ return {
248
+ "text": "",
249
+ "error_code": 400,
250
+ "error_msg": "不支持的音频格式",
251
+ "process_time": time.time() - start_time
252
+ }
253
+
254
+ if model != "FunAudioLLM/SenseVoiceSmall":
255
+ return {
256
+ "text": "",
257
+ "error_code": 400,
258
+ "error_msg": "不支持的模型",
259
+ "process_time": time.time() - start_time
260
+ }
261
+
262
+ if language not in ["auto", "zh", "en", "yue", "ja", "ko", "nospeech"]:
263
+ return {
264
+ "text": "",
265
+ "error_code": 400,
266
+ "error_msg": "不支持的语言",
267
+ "process_time": time.time() - start_time
268
+ }
269
+
270
  content = await file.read()
271
  text = await process_audio(content, language)
272
+
273
+ return {
274
+ "text": text,
275
+ "error_code": 0,
276
+ "error_msg": "",
277
+ "process_time": time.time() - start_time
278
+ }
279
 
280
  except Exception as e:
281
+ return {
282
+ "text": "",
283
+ "error_code": 500,
284
+ "error_msg": str(e),
285
+ "process_time": time.time() - start_time
286
+ }
287
 
288
 
289
  @app.get("/", response_class=HTMLResponse)