forked from Fill-Easy-Limited/ChatBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
53 lines (40 loc) · 1.81 KB
/
Copy pathapi.py
File metadata and controls
53 lines (40 loc) · 1.81 KB
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
from flask import Flask, request, jsonify
from flask_restful import Api, Resource, reqparse
import numpy as np
from transformers import AutoTokenizer
import numpy as np
from transformers import TFAutoModelForSequenceClassification
from tensorflow.keras.losses import BinaryCrossentropy
from tensorflow.keras.optimizers import Adam
import pandas as pd
from chartbot_config import *
app = Flask(__name__)
api = Api(app)
parser = reqparse.RequestParser()
parser.add_argument('data')
df = pd.read_csv(raw_data_path, encoding = 'unicode_escape')
num_labels = df.Intent.nunique()
df = df.melt(id_vars=["Domain", "Sub domain", "Intent", "Answer Format"]).drop("variable", axis = 1)
Classes_dict_1 = dict(zip(list(df["Answer Format"].unique()), [i for i in range(df["Intent"].nunique())]))
Classes_dict = {value:key for key, value in Classes_dict_1.items()}
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
model = TFAutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=num_labels, problem_type="multi_label_classification")
model.load_weights(model_path)
opt = Adam()
loss = BinaryCrossentropy(from_logits=True)
model.compile(
optimizer = opt,
loss = loss,
metrics=["accuracy"],
)
# Define how the api will respond to the post requests
class QuestionClassifier(Resource):
@app.route('/foo', methods=['POST'])
def post():
data = request.json
sentence = data['data']
tokenized_dataset = tokenizer(sentence, padding=True, truncation=True, return_tensors = "tf")
output = model(**tokenized_dataset)["logits"]
class_preds = [np.argmax(i) if np.sum(i) == 1 else len(i) for i in output > 0][0]
return jsonify(data=Classes_dict.get(class_preds, "Sorry I cannot understand."))
api.add_resource(QuestionClassifier, '/chartbot')