-
Notifications
You must be signed in to change notification settings - Fork 42
/
main.py
47 lines (40 loc) · 1.05 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
import traceback
from fastapi import FastAPI
from fastapi import Request
from infer import generate
app = FastAPI()
@app.post('/api/generate')
async def api_generate(request: Request):
"""
curl -XPOST http://localhost:8000/api/generate \
-H 'Content-Type: applicaton/json' \
-d '{"text": "你好啊"}'
"""
data = await request.json()
if 'text' not in data or not isinstance(data['text'], str):
return {
'ok': False,
'error': 'Invalid text in post data',
}
try:
ret = generate(
text=data['text'],
max_len = data.get('max_len', 50),
temperature = data.get('temperature', 1.0),
top_p = data.get('top_p', 0.95),
top_k = data.get('top_k', 50)
)
return {
'ok': True,
'text': ret,
}
except Exception:
return {
'ok': False,
'error': traceback.format_exc(),
}
@app.get('/')
async def hello():
return {
'hello': 'world',
}