-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
48 lines (40 loc) · 1.22 KB
/
main.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
from flask import Flask, jsonify
from flask import request
from src.classifier import Aspect_Classifier
import os
app = Flask(__name__)
pipeline = Aspect_Classifier(
model_path=os.path.join(*[os.getcwd(), "weights", "tiny_bert.pt"]),
token_path=os.path.join(*[os.getcwd(), "weights", "tiny_bert_tokenizer"])
)
sentiment_id = {
0: "negative",
1: "neutral",
2: "positive"
}
@app.route('/predict', methods=['POST'])
def predict():
"""
Request
-------
>>> import requests
>>> files = {
... "text": "my laptop is working fine but the mouse is a little noisy."
... "phrase": "mouse"
... }
>>> requests.post("http://localhost:5000/predict", files=files).json()
{'label': 2}
"""
if request.method == 'POST':
text = request.files["text"].read().decode('UTF-8')
phrase = request.files["phrase"].read().decode('UTF-8')
output = pipeline(str(text), str(phrase))
sentiment = sentiment_id[output["label"]]
probablities = round(output[sentiment], 2)
return jsonify({
"label": output["label"],
"sentiment": sentiment,
"probablities": probablities
})
if __name__ == '__main__':
app.run()