-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
49 lines (39 loc) · 1.27 KB
/
app.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
import pandas as pd
from mf_model import MfModelOption, MFSentimentAnalyzer
from flask import Flask, jsonify, request
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
@app.route('/', methods=['GET'])
def home():
resp = {
"message": "All service working normally.",
"apiVersion": "v1.0.0"
}
return jsonify(resp)
@app.route('/api/v1/analyze-sentiment', methods=['POST'])
def analyze_sentiment():
data = request.get_json()
if 'reviews' not in data:
return jsonify({'error': 'No reviews provided'}), 400
if data.get("model") not in ['MFNb', 'MFSvc']:
return jsonify({'error': 'No model found.'}), 400
df = pd.DataFrame(data['reviews'])
model_option = MfModelOption[data.get('model', 'MFNb')]
mfModel = MFSentimentAnalyzer(df, model_option)
result = mfModel.Analyze()
resp = {
"status": "success",
"data": {
"sentiment": result.to_dict(orient='records')
}
}
return jsonify(resp)
@app.errorhandler(500)
def internal_server_error(error):
return jsonify({
"status": "error",
"message": "Internal Server Error, please try again later."
}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860, debug=True)