|
import os
|
|
import sys
|
|
import logging
|
|
from flask import Flask, request, jsonify
|
|
from flask_cors import CORS
|
|
|
|
|
|
from serve import get_model_api
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
CORS(app)
|
|
|
|
|
|
|
|
if 'DYNO' in os.environ:
|
|
app.logger.addHandler(logging.StreamHandler(sys.stdout))
|
|
app.logger.setLevel(logging.INFO)
|
|
|
|
app.logger.addHandler(logging.StreamHandler(sys.stdout))
|
|
app.logger.setLevel(logging.INFO)
|
|
|
|
model_api = get_model_api()
|
|
|
|
|
|
|
|
@app.route('/api', methods=['POST'])
|
|
def api():
|
|
"""API function
|
|
|
|
All model-specific logic to be defined in the get_model_api()
|
|
function
|
|
"""
|
|
input_data = request.json
|
|
log = open("test_topic_serve_log.csv", 'a', encoding='utf-8')
|
|
app.logger.info("api_input: " + str(input_data))
|
|
log.write("api_input: " + str(input_data))
|
|
|
|
|
|
input_sys_prompt_str = input_data['system_prompt']
|
|
input_USER_str = input_data['USER']
|
|
|
|
input_history_str = input_data['history']
|
|
|
|
output_data = model_api(input_sys_prompt_str, input_history_str, input_USER_str)
|
|
app.logger.info("api_output: " + str(output_data))
|
|
response = jsonify(output_data)
|
|
log.write("api_output: " + str(output_data) + "\n")
|
|
|
|
return response
|
|
|
|
|
|
@app.route('/labelapi', methods=['POST'])
|
|
def labelapi():
|
|
"""label API function
|
|
record user label action
|
|
All model-specific logic to be defined in the get_model_api()
|
|
function
|
|
"""
|
|
input_data = request.json
|
|
log = open("test_topic_label_log.csv", 'a', encoding='utf-8')
|
|
app.logger.info("api_input: " + str(input_data))
|
|
log.write("api_input: " + str(input_data)+ "\n")
|
|
output_data = {"input": input_data, "output": "record_success"}
|
|
|
|
response = output_data
|
|
return response
|
|
|
|
@app.route('/')
|
|
def index():
|
|
return "Index API"
|
|
|
|
|
|
@app.errorhandler(404)
|
|
def url_error(e):
|
|
return """
|
|
Wrong URL!
|
|
<pre>{}</pre>""".format(e), 404
|
|
|
|
|
|
@app.errorhandler(500)
|
|
def server_error(e):
|
|
return """
|
|
An internal error occurred: <pre>{}</pre>
|
|
See logs for full stacktrace.
|
|
""".format(e), 500
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run(host='0.0.0.0',port=4455,debug=True)
|
|
|
|
|