-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
38 lines (30 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
from flask import Flask, request, jsonify
import numpy as np
import json
from transformers import AutoTokenizer, AutoModelForTokenClassification
from transformers import pipeline
app = Flask(__name__)
class Encoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.float32):
return float(obj)
return json.JSONEncoder.default(self, obj)
@app.route('/', methods=['GET', 'POST'])
def makecalc():
if request.method=="POST":
print("request received")
data = request.get_json()
print("data ---- > ", data)
# prediction = np.array2string(model.predict(data))
results = classifier(data)
return json.dumps(results, cls=Encoder)
return "Not a proper equest method or data"
if __name__ == '__main__':
model_path = './models/transformers/'
model = AutoModelForTokenClassification.from_pretrained(model_path, local_files_only=True)
print("----------- transformer model loaded ------------")
tokenizer = AutoTokenizer.from_pretrained(model_path, local_files_only=True)
print("----------- transformer tokenizer loaded ------------")
classifier = pipeline('token-classification', model=model, tokenizer=tokenizer)
print(classifier)
app.run(debug=False, host='0.0.0.0')