-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
42 lines (38 loc) · 972 Bytes
/
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
import traceback
from fastapi import Request
from infer import sim
from app import app
@app.post('/api/sim')
async def api_sim(request: Request):
"""
time curl -XPOST http://localhost:8000/api/sim \
-H 'Content-Type: applicaton/json' \
-d '{"a": "你好啊", "b": "你好"}'
"""
data = await request.json()
if 'a' not in data or not isinstance(data['a'], str):
return {
'ok': False,
'error': 'Invalid a in post data',
}
if 'b' not in data or not isinstance(data['b'], str):
return {
'ok': False,
'error': 'Invalid b in post data',
}
try:
ret = sim(data['a'], data['b'])
return {
'ok': True,
**ret
}
except Exception:
return {
'ok': False,
'error': traceback.format_exc(),
}
@app.get('/')
async def hello():
return {
'hello': 'world',
}