-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
159 lines (137 loc) · 4.33 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import argparse
from tempfile import TemporaryDirectory
import uvicorn
from fastapi import FastAPI, File, Form, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from minutes_maker import MinutesMaker
class OutputData(BaseModel):
timeline: str
summary: str
class MinutesMakerAPI:
"""
API for Minutes Maker.
Attributes
----------
app : FastAPI
FastAPI instance.
mm : MinutesMaker
MinutesMaker instance.
Methods
-------
minutes_maker
Minutes Maker API endpoint.
"""
def __init__(self, model: str, cpu_threads: int = 0, num_workers: int = 1):
"""
Initialize MinutesMakerAPI.
Parameters
----------
model : str
model name for summarization.
cpu_threads : int, optional
number of threads for CPU whisper inference,
by default 0 for auto.
num_workers : int, optional
number of workers for whisper inference,
by default 1 for non-parallel.
"""
self.app = FastAPI()
self.mm = MinutesMaker(
model=model, cpu_threads=cpu_threads, num_workers=num_workers
)
self.app.add_api_route(
"/minutes_maker",
self.minutes_maker,
methods=["POST"],
response_model=OutputData,
)
self.app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
async def minutes_maker(
self,
file: UploadFile = File(...),
filename: str = Form(...),
language: str = Form(...),
category: str = Form(...),
content: str = Form(...),
) -> OutputData:
"""
Minutes Maker API endpoint called when a POST request is sent to
"/minutes_maker".
This method is composed of the following steps:
1. Save the file to a temporary directory.
2. Make timeline and summary of the meeting or lecture.
3. Return timeline and summary.
Parameters
----------
file : UploadFile
audio or video file.
filename : str
filename of the uploaded file.
language : str
language of the uploaded file, "en" or "ja".
category : str
category of the uploaded file, "meeting" or "lecture".
content : str
topic of the meeting or lecture in the uploaded file.
Returns
-------
OutputData
timeline and summary of the uploaded file.
"""
# 0. await file.read() to get bytes
file = await file.read()
with TemporaryDirectory() as tempdir:
# 1. save the file to a temporary directory
with open(f"{tempdir}/{filename}", "wb") as f:
f.write(file)
# 2. make timeline and summary of the meeting or lecture
timeline, summary = self.mm(
audio_or_video_file_path=f"{tempdir}/{filename}",
language=language,
category=category,
content=content,
)
# 3. return timeline and summary
return OutputData(timeline=timeline, summary=summary)
if __name__ == "__main__":
argparser = argparse.ArgumentParser()
argparser.add_argument(
"-m",
"--model",
type=str,
default="gpt-3.5-turbo-16k-0613",
help="model name for summarization (default: gpt-3.5-turbo-16k-0613)",
)
argparser.add_argument(
"-t",
"--cpu_threads",
type=int,
default=0,
help="number of threads for CPU whisper inference (default: 0 for auto)",
)
argparser.add_argument(
"-w",
"--num_workers",
type=int,
default=1,
help="number of workers for whisper inference (default: 1 for non-parallel)",
)
argparser.add_argument(
"-p",
"--port",
type=int,
default=10355,
help="port number for API (default: 10355)",
)
args = argparser.parse_args()
mm_api = MinutesMakerAPI(
model=args.model, cpu_threads=args.cpu_threads, num_workers=args.num_workers
)
uvicorn.run(mm_api.app, host="0.0.0.0", port=args.port)