maolin.liu commited on
Commit
2278032
·
1 Parent(s): 705afb7

[feature]Support choose audio file path.

Browse files
Files changed (1) hide show
  1. server.py +18 -3
server.py CHANGED
@@ -2,6 +2,7 @@ import base64
2
  import io
3
  import logging
4
  import os
 
5
  import typing
6
  from contextlib import asynccontextmanager
7
 
@@ -10,7 +11,7 @@ from fastapi import FastAPI, Request, UploadFile, File, WebSocket
10
  from fastapi.middleware.cors import CORSMiddleware
11
  from fastapi.middleware.gzip import GZipMiddleware
12
  from faster_whisper import WhisperModel
13
- from pydantic import BaseModel, Field, ValidationError
14
  from starlette.websockets import WebSocketState
15
 
16
 
@@ -69,6 +70,15 @@ class TranscribeRequestParams(BaseModel):
69
  uuid: str = Field(title='Request Unique Id.')
70
  audio_file: str
71
  language: typing.Literal['en', 'zh',]
 
 
 
 
 
 
 
 
 
72
 
73
 
74
  @app.post('/transcribe')
@@ -77,7 +87,9 @@ async def transcribe_api(
77
  obj: TranscribeRequestParams
78
  ):
79
  try:
80
- audio_file = io.BytesIO(base64.b64decode(obj.audio_file))
 
 
81
 
82
  segments, _ = asr_model.transcribe(audio_file, language=obj.language)
83
 
@@ -153,7 +165,10 @@ async def transcribe_ws_api(
153
  continue
154
 
155
  try:
156
- audio_file = io.BytesIO(base64.b64decode(form.audio_file))
 
 
 
157
 
158
  segments, _ = asr_model.transcribe(audio_file, language=form.language)
159
 
 
2
  import io
3
  import logging
4
  import os
5
+ import pathlib
6
  import typing
7
  from contextlib import asynccontextmanager
8
 
 
11
  from fastapi.middleware.cors import CORSMiddleware
12
  from fastapi.middleware.gzip import GZipMiddleware
13
  from faster_whisper import WhisperModel
14
+ from pydantic import BaseModel, Field, ValidationError, model_validator, ValidationInfo
15
  from starlette.websockets import WebSocketState
16
 
17
 
 
70
  uuid: str = Field(title='Request Unique Id.')
71
  audio_file: str
72
  language: typing.Literal['en', 'zh',]
73
+ using_file_content: bool
74
+
75
+ @model_validator(mode='after')
76
+ def check_audio_file(self):
77
+ if self.using_file_content:
78
+ return self
79
+
80
+ if not pathlib.Path(self.audio_file).exists():
81
+ raise FileNotFoundError(f'Audio file not exists.')
82
 
83
 
84
  @app.post('/transcribe')
 
87
  obj: TranscribeRequestParams
88
  ):
89
  try:
90
+ audio_file = obj.audio_file
91
+ if obj.using_file_content:
92
+ audio_file = io.BytesIO(base64.b64decode(obj.audio_file))
93
 
94
  segments, _ = asr_model.transcribe(audio_file, language=obj.language)
95
 
 
165
  continue
166
 
167
  try:
168
+
169
+ audio_file = form.audio_file
170
+ if form.using_file_content:
171
+ audio_file = io.BytesIO(base64.b64decode(form.audio_file))
172
 
173
  segments, _ = asr_model.transcribe(audio_file, language=form.language)
174