maolin.liu commited on
Commit
1e44fe0
·
1 Parent(s): 59ef18e

[feature]First commit.

Browse files
Files changed (2) hide show
  1. requirements.txt +3 -0
  2. server.py +92 -0
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ faster-whisper==1.1.0
2
+ fastapi==0.115.5
3
+ uvicorn==0.32.1
server.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import typing
3
+ from contextlib import asynccontextmanager
4
+
5
+ import uvicorn
6
+ from fastapi import FastAPI, Request, UploadFile, File
7
+ from fastapi.middleware.cors import CORSMiddleware
8
+ from fastapi.middleware.gzip import GZipMiddleware
9
+ from faster_whisper import WhisperModel
10
+ from pydantic import BaseModel, Field, FilePath
11
+
12
+
13
+ @asynccontextmanager
14
+ async def register_init(app: FastAPI):
15
+ """
16
+ 启动初始化
17
+
18
+ :return:
19
+ """
20
+
21
+ yield
22
+
23
+
24
+ def register_middleware(app: FastAPI):
25
+ # Gzip: Always at the top
26
+ app.add_middleware(GZipMiddleware)
27
+
28
+ # CORS: Always at the end
29
+ app.add_middleware(
30
+ CORSMiddleware,
31
+ allow_origins=['*'],
32
+ allow_credentials=True,
33
+ allow_methods=['*'],
34
+ allow_headers=['*'],
35
+ )
36
+
37
+
38
+ def create_app():
39
+ app = FastAPI(
40
+ lifespan=register_init
41
+ )
42
+ register_middleware(app)
43
+ return app
44
+
45
+
46
+ app = create_app()
47
+
48
+ model_size = os.environ.get('WHISPER-MODEL-SIZE', 'large-v3')
49
+ # Run on GPU with FP16
50
+ whisper_model: typing.Optional[WhisperModel] = WhisperModel(model_size, device='cuda', compute_type='float16')
51
+
52
+
53
+ class TranscribeRequestParams(BaseModel):
54
+ uuid: str = Field(title='Request Unique Id.')
55
+ audio_file: FilePath = Field()
56
+ language: typing.Literal['en', 'zh',]
57
+
58
+
59
+ @app.post('/transcribe')
60
+ def transcribe_api(
61
+ request: Request,
62
+ obj: TranscribeRequestParams
63
+ ):
64
+ transcribed_text = whisper_model.transcribe(obj.audio_file, language=obj.language)
65
+ return {
66
+ "if_success": True,
67
+ 'uuid': obj.uuid,
68
+ 'transcribed_text': transcribed_text
69
+ }
70
+
71
+
72
+ @app.post('/transcribe-file')
73
+ def transcribe_file_api(
74
+ request: Request,
75
+ uuid: str,
76
+ audio_file: typing.Annotated[UploadFile, File()],
77
+ language: typing.Literal['en', 'zh']
78
+ ):
79
+ transcribed_text = whisper_model.transcribe(audio_file.file, language=language)
80
+ return {
81
+ "if_success": True,
82
+ 'uuid': uuid,
83
+ 'transcribed_text': transcribed_text
84
+ }
85
+
86
+
87
+ if __name__ == '__main__':
88
+ uvicorn.run(
89
+ app,
90
+ host=os.environ.get('HOST', '0.0.0.0'),
91
+ port=int(os.environ.get('PORT', 8080))
92
+ )