-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
72 lines (53 loc) · 2.46 KB
/
server.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
# -*- coding: utf-8 -*-
import os
import logging
import argparse
from flask import Flask, request, jsonify
from intent_classifier import IntentClassifier
app = Flask(__name__)
model = IntentClassifier()
logging.basicConfig(level=logging.INFO, filename='classifier.log', filemode='a')
logger = logging.getLogger(__name__)
log_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
@app.route('/ready')
def ready():
if model.is_ready():
return 'OK', 200
else:
return 'Not ready', 423
@app.route('/intent', methods=['POST'])
def intent():
try:
# Check if the request has a JSON body
if not request.json:
return jsonify({"label": "BODY_MISSING", "message": "Request doesn't have a body."}), 400
# Check if the 'text' field is present in the request JSON
if 'text' not in request.json:
return jsonify({"label": "TEXT_MISSING", "message": "\"text\" missing from request body."}), 400
# Check if the 'text' field is a string
if not isinstance(request.json['text'], str):
return jsonify({"label": "INVALID_TYPE", "message": "\"text\" is not a string."}), 400
# Check if the 'text' field is empty
if not request.json['text'].strip():
return jsonify({"label": "TEXT_EMPTY", "message": "\"text\" is empty."}), 400
# Perform intent classification (replace this with your actual intent classification logic)
intents = model.classify_intent_function(request.json['text'])
# Create a response with the top 3 intent predictions
response = {"intents": intents[:3]}
return jsonify(response)
except Exception as e:
logger.exception('Internal error: %s', str(e))
# Handle internal errors
return jsonify({"label": "INTERNAL_ERROR", "message": str(e)}), 500
def main():
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('--model', type=str, required=True, help='Path to model directory or file.')
arg_parser.add_argument('--port', type=int, default=os.getenv('PORT', 8080), help='Server port number.')
args = arg_parser.parse_args()
model.load(args.model)
logger.info('Server is starting on port %s', args.port)
app.run(port=args.port)
if __name__ == '__main__':
main()
#curl http://localhost:8080/ready
#curl -X POST -H "Content-Type: application/json" -d '{"text": "when is the next flight to new york"}' http://localhost:8080/intent