File size: 1,547 Bytes
459c675 |
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 |
from flask import Flask, request, jsonify
import time
import ktrain
app = Flask(__name__)
predictor = ktrain.load_predictor('model/bert_model')
# worked
print(predictor.predict('I love this product!'))
print(predictor.predict('I hate this product!'))
print(predictor.predict('I am so sad!'))
print(predictor.predict('I am so happy!'))
print(predictor.predict("I am looking for a job."))
print(predictor.predict("I like to play football."))
print(predictor.predict("I am going to the beach."))
print(predictor.predict("I am going to the hospital."))
print(predictor.predict("His son is very sick."))
@app.route('/')
def index():
response = {
'message': 'Social Media Emotion Analysis!'
}
return jsonify(response)
@app.route('/predict-str', methods=['POST'])
def predict_message():
data = request.json
message = data.get('message', '')
start_time = time.time()
prediction = predictor.predict(message)
response = {
'message': message,
'prediction': prediction,
'elapsed_time': time.time() - start_time
}
return jsonify(response)
@app.route('/predict-list', methods=['POST'])
def predict_list():
data = request.json
messages = data.get('messages', [])
start_time = time.time()
predictions = predictor.predict(messages)
response = {
'messages': messages,
'predictions': predictions,
'elapsed_time': time.time() - start_time
}
return jsonify(response)
if __name__ == '__main__':
app.run() |