-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.py
46 lines (33 loc) · 1.43 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
import os
import uvicorn
import boto3
from fastapi import FastAPI, Request, Form
from fastapi.templating import Jinja2Templates
from classifier.model import TextClassifier
import logging
def download_classifier():
model_bucket_name = os.environ['MODEL_BUCKET_NAME']
s3_client = boto3.client('s3')
with open('classifier.pkl', 'wb') as f:
s3_client.download_fileobj(model_bucket_name, 'classifier.pkl', f)
classifier = TextClassifier.load('classifier.pkl')
return classifier
classifier = download_classifier()
app = FastAPI()
templates = Jinja2Templates(directory='templates/')
@app.get('/')
def health():
return 'healthy.'
@app.get('/classify')
def classify_get(request: Request):
classifier_result = {}
return templates.TemplateResponse('form_template.html', context={'request': request, 'classifier_result': classifier_result})
@app.post('/classify')
def classify_post(request: Request, text: str = Form(...)):
classifier_result = classifier(text)
return templates.TemplateResponse('form_template.html', context={'request': request, 'classifier_result': classifier_result})
if __name__ == '__main__':
logging.basicConfig(format='{levelname:7} {message}', style='{', level=logging.INFO)
log_config = uvicorn.config.LOGGING_CONFIG
log_config['formatters']['access']['fmt'] = '%(asctime)s - %(levelname)s - %(message)s'
uvicorn.run(app, host='0.0.0.0', port=80, debug=True, log_config=None)