tweet_sentiments / app /routes.py
David-ipynb's picture
Initial commit. Trained model, Flask web app, Docker container
36da459
raw
history blame
721 Bytes
from flask import Flask, jsonify, request
from utils import predict_single, predict_batch
app = Flask(__name__)
@app.route('/')
@app.route('/home')
def status():
return jsonify({'status': 'ok'})
@app.route('/predict', methods=['POST'])
def predict():
data = request.get_json()
if 'text' not in data:
return jsonify({'error': 'Missing "text" parameter'}), 400
tweets = data['text']
if len(tweets) == 1:
response = predict_single(tweets[0])
elif len(tweets) > 1:
response = predict_batch(tweets)
else:
return jsonify({'error': 'Zero text strings posted'}), 400
return jsonify({
'inputs': tweets,
'predictions': response
})