-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
40 lines (35 loc) · 1.08 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
from flask import Flask, request, jsonify
from flask_cors import CORS
from flair.models import TextClassifier
from flair.data import Sentence
classifier = TextClassifier.load('./model/best-model.pt')
mapping = {
'sad': '😞',
'smile': '😀',
'food': '🍽️',
'heart': '❤️',
'baseball': '⚾'
}
app = Flask(__name__)
CORS(app)
@app.route('/emojify', methods=['POST'])
def emoji():
data = request.form.get('text')
if not len(data.strip()):
return ''
sentence = Sentence(data)
classifier.predict(sentence)
print(str(sentence.labels))
if 'sad' in str(sentence.labels):
return mapping['sad']
elif 'smile' in str(sentence.labels):
return mapping['smile']
elif 'food' in str(sentence.labels):
return mapping['food']
elif 'heart' in str(sentence.labels):
print(1)
return mapping['heart']
elif 'baseball' in str(sentence.labels):
return mapping['baseball']
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, threaded=True, debug=True)