-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.py
More file actions
269 lines (230 loc) · 9.32 KB
/
api.py
File metadata and controls
269 lines (230 loc) · 9.32 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import uvicorn
from fastapi import FastAPI, Request, File, UploadFile, HTTPException, Body
from fastapi_utils.api_model import APIModel
from fastapi.responses import RedirectResponse
import os
import json
from typing import List
from IE.ner import ner
from IE.relation import relation_extraction
from IE.attribute_extraction import attribute_extraction
from IE.summary import summary
from IE.keywords import keywords_extraction
from IE.region_identifier import region_extraction
from IE.sentiment_analysis import sentiment_analysis
from IE.text_classification import text_classification
from IE.passage_cos import file_cos
from IE.machine_translation import tranlate
from IE.paper_read import paper_read
from units.calculate_md5 import calculate_md5
from units.load_data import load_data
from units.load_data import lazy_load_data
from units.del_tmp import delete_files_in_directory
from units.abstract_model import AbstractModel
PORT = 12931
app = FastAPI()
process_exception = HTTPException(
status_code=422, detail="Process failed, please check your input.")
@app.get("/")
async def redirect():
"""
Show Restful API doc in explorer, by redirect to doc route
Returns:
doc website
"""
response = RedirectResponse(url=f"http://localhost:{PORT}/docs/")
return response
async def save_file(file: UploadFile) -> str:
"""
Store uploaded file stream into local server
Args:
file (UploadFile): file to be extracted, like PDF, Word, TXT, etc.
Returns:
str: local path to read this file
"""
# time_stamp = int(time.time())
file_content = await file.read()
md5 = await calculate_md5(file_content)
file_prefix = '.'.join(str(file.filename).split('.')[:-1])
file_suffix = str(file.filename).split('.')[-1]
if not os.path.exists('tmp'):
os.mkdir('tmp/')
file_save_path = f"tmp/{file_prefix}_{md5}.{file_suffix}"
file_type = file.content_type
with open(file_save_path, "wb") as f:
f.write(file_content)
return file_save_path, file_type
# 实体抽取
async def doc_ner(file: UploadFile = File(...)):
max_pages = 5
try:
file_path, file_type = await save_file(file)
data = await load_data(file_path, file_type, max_pages=max_pages)
result = await ner(data)
delete_files_in_directory('tmp')
return {'result': result}
except ValueError as e:
raise HTTPException(
status_code=422, detail="File type %s is not a valid file type" % file_type)
except Exception as e:
raise process_exception
# 关系抽取
async def doc_ee(file: UploadFile = File(...)):
max_pages = 5
try:
file_path, file_type = await save_file(file)
data = await load_data(file_path, file_type, max_pages=max_pages)
result = await relation_extraction(data)
delete_files_in_directory('tmp')
return {'result': result}
except ValueError as e:
raise HTTPException(
status_code=422, detail="File type %s is not a valid file type" % file_type)
except Exception as e:
raise process_exception
# 属性抽取
async def doc_ae(file: UploadFile = File(...)):
max_pages = 5
try:
file_path, file_type = await save_file(file)
data = await load_data(file_path, file_type, max_pages=max_pages)
result = await attribute_extraction(data)
delete_files_in_directory('tmp')
return {'result': result}
except ValueError as e:
raise HTTPException(
status_code=422, detail="File type %s is not a valid file type" % file_type)
except Exception as e:
raise process_exception
# 摘要
async def doc_summary(file: UploadFile = File(...)):
max_pages = 5
try:
file_path, file_type = await save_file(file)
data = await load_data(file_path, file_type, max_pages=max_pages)
result = await summary(data)
delete_files_in_directory('tmp')
return {'result': result}
except ValueError as e:
raise HTTPException(
status_code=422, detail="File type %s is not a valid file type" % file_type)
except Exception as e:
raise process_exception
# 关键词抽取
async def doc_keywords(file: UploadFile = File(...), ):
max_pages = 5
try:
file_path, file_type = await save_file(file)
data = await load_data(file_path, file_type, max_pages=max_pages, is_keyword=True)
result = await keywords_extraction(data)
delete_files_in_directory('tmp')
return {'result': result}
except ValueError as e:
raise HTTPException(
status_code=422, detail="File type %s is not a valid file type" % file_type)
except Exception as e:
raise process_exception
# 地区识别
async def doc_region(file: UploadFile = File(...)):
max_pages = 5
try:
file_path, file_type = await save_file(file)
data = await load_data(file_path, file_type, max_pages=max_pages)
result = await region_extraction(data)
delete_files_in_directory('tmp')
return {'result': result}
except ValueError as e:
raise HTTPException(
status_code=422, detail="File type %s is not a valid file type" % file_type)
except Exception as e:
raise process_exception
# 情感分析
async def doc_sentiment(file: UploadFile = File(...)):
max_pages = 5
try:
file_path, file_type = await save_file(file)
data = await load_data(file_path, file_type, max_pages=max_pages)
result = await sentiment_analysis(data)
delete_files_in_directory('tmp')
return {'result': result}
except ValueError as e:
raise HTTPException(
status_code=422, detail="File type %s is not a valid file type" % file_type)
except Exception as e:
raise process_exception
# 文本分类
async def doc_classification(file: UploadFile = File(...)):
max_pages = 5
try:
file_path, file_type = await save_file(file)
data = await load_data(file_path, file_type, max_pages=max_pages)
result = await text_classification(data)
delete_files_in_directory('tmp')
return {'result': result}
except ValueError as e:
raise HTTPException(
status_code=422, detail="File type %s is not a valid file type" % file_type)
except Exception as e:
raise process_exception
# 文章相似度比较
async def doc_similarity(files: List[UploadFile] = File(...)):
try:
file_paths = []
file_types = []
for file in files:
file_path, file_type = await save_file(file)
file_paths.append(file_path)
file_types.append(file_type)
pages1 = await lazy_load_data(file_paths[0], file_types[0])
pages2 = await lazy_load_data(file_paths[1], file_types[1])
results = await file_cos(pages1=pages1, pages2=pages2)
delete_files_in_directory('tmp')
return {'results': results}
except ValueError as e:
raise HTTPException(
status_code=422, detail="File type %s is not a valid file type" % file_type)
except Exception as e:
raise process_exception
# 翻译
async def doc_translate(file: UploadFile = File(...)):
max_pages = 5
try:
file_path, file_type = await save_file(file)
data = await load_data(file_path, file_type, max_pages=max_pages)
result = await tranlate(data)
delete_files_in_directory('tmp')
return {'result': result}
except ValueError as e:
raise HTTPException(
status_code=422, detail="File type %s is not a valid file type" % file_type)
except Exception as e:
raise process_exception
async def doc_paper_read(data: AbstractModel = Body(...)):
try:
prompt = []
with open('prompt/read_paper.jsonl', 'r', encoding='utf-8') as f:
for line in f:
prompt.append(eval(line.strip()))
result = await paper_read(data.content, prompt)
return {'result': result}
except Exception as e:
raise process_exception
app.post("/doc_ie/ner", tags=["IE"], summary="单文档命名实体识别")(doc_ner)
app.post("/doc_ie/re", tags=["IE"], summary="单文档关系抽取")(doc_ee)
app.post("/doc_ie/ae", tags=["IE"], summary="单文档属性抽取")(doc_ae)
app.post("/doc_ie/summary", tags=["IE"], summary="单文档摘要")(doc_summary)
app.post("/doc_ie/keywords", tags=["IE"], summary="单文档关键词抽取")(doc_keywords)
app.post("/doc_ie/region", tags=["IE"], summary="单文档地区识别")(doc_region)
app.post("/doc_ie/sentiment", tags=["IE"],
summary="单文档情感分析")(doc_sentiment)
app.post("/doc_ie/classification",
tags=["IE"], summary="单文档文本分类")(doc_classification)
app.post("/doc_ie/similarity", tags=["IE"], summary="文档相似度比较")(doc_similarity)
app.post("/doc_ie/translate", tags=["IE"], summary="单文档翻译")(doc_translate)
app.post("/doc_ie/paper_read", tags=["IE"], summary="论文速读")(doc_paper_read)
if __name__ == '__main__':
uvicorn.run(
app=app,
host="localhost",
port=PORT
)