From 3276eeb0da1ef91e2cd35916241097a8c3ab58b0 Mon Sep 17 00:00:00 2001 From: Javclaude Date: Sun, 31 May 2020 18:29:03 +0200 Subject: [PATCH] Add API server --- app/app.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 app/app.py diff --git a/app/app.py b/app/app.py new file mode 100644 index 0000000..754f9e5 --- /dev/null +++ b/app/app.py @@ -0,0 +1,45 @@ +import argparse + +import flask +from flask import Flask, jsonify, request +import requests + +from utils import load_model, load_tokenizer, encode_text, predict +from transformers import AutoTokenizer + +app = Flask(__name__) + +Model, config_file = load_model("./src/Baseline_model/state_dict.py", "./src/Baseline_model/config_model.json") # argparse +tokenizer = load_tokenizer() + +@app.route('/predict', methods = ['POST']) +def api_sentiment(): + data = request.get_json(force = True) + + encoded = encode_text(data['text'], tokenizer, config_file) + + prediction = predict(Model, encoded) + + request_responses = { + "Negative Score" : prediction[0][0], + "Positive Score" : prediction[0][1] + } + + return jsonify(request_responses) + +if __name__ == "__main__": + + """ parser = argparse.ArgumentParser() + + parser.add_argument("--path_to_model", help="Path to the pre trained model", typr=str) + parser.add_argument("--path_to_config", help="Path to the config file", type=str) + + args = parser.parse_args() + """ + app.run(debug=True) + + + + + +