-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
184 lines (133 loc) · 5.85 KB
/
run.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from flask import Flask, request, jsonify
from AFL import AFL
from ChatBot import ChatBot
from flask import Flask, render_template, session, redirect, url_for, session ,request
from flask_wtf import FlaskForm
from wtforms import TextField,SubmitField
from wtforms.validators import NumberRange
from tensorflow.keras.models import load_model
import joblib
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from transformers import BertForSequenceClassification,BertTokenizer
import torch
from flask_restful import Resource, Api
import easydict
import logging
import random
from argparse import ArgumentParser
from itertools import chain
from pprint import pformat
import warnings
import time
import torch
import torch.nn.functional as F
from transformers import OpenAIGPTLMHeadModel, OpenAIGPTTokenizer, GPT2LMHeadModel, GPT2Tokenizer
from train import SPECIAL_TOKENS, build_input_from_segments, add_special_tokens_
from utils import get_dataset, download_pretrained_model
import json
dataset_path = 'data/persona_label.json'
with open(dataset_path, "r", encoding="utf-8") as f:
persona_label = json.loads(f.read())
app = Flask(__name__)
args = easydict.EasyDict({
"model": 'gpt2',
"dataset_path": "data/en_book_conversational.json",
"dataset_cache": "./dataset_cache",
"model_checkpoint":"/home/ubuntu/server/transfer-learning-conv-ai/runs/Jun04_18-39-17_ime-502_gpt2",
"temperature": 1.9,
"top_k": 180,
"top_p": 0.1,
"max_history": 2,
"device" : "cuda" if torch.cuda.is_available() else "cpu",
"no_sample": True,
"max_length": 20,
"min_length" :1,
"seed": 0
})
if args.model_checkpoint == "":
if args.model == 'gpt2':
raise ValueError("Interacting with GPT2 requires passing a finetuned model_checkpoint")
else:
args.model_checkpoint = download_pretrained_model()
if args.seed != 0:
random.seed(args.seed)
torch.random.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
# logger.info("Get pretrained model and tokenizer")
tokenizer_class, model_class = (GPT2Tokenizer, GPT2LMHeadModel) if args.model == 'gpt2' else (OpenAIGPTTokenizer, OpenAIGPTLMHeadModel)
tokenizer = tokenizer_class.from_pretrained(args.model_checkpoint)
model = model_class.from_pretrained(args.model_checkpoint)
model.to(args.device)
add_special_tokens_(model, tokenizer)
# logger.info("Sample a personality")
dataset = get_dataset(tokenizer, args.dataset_path,args.dataset_cache)
personalities = [dialog["personality"] for dataset in dataset.values() for dialog in dataset]
personality = random.choice(personalities)
book={}
chapter=""
for data in persona_label:
for unit, persona in data.items():
while persona[0] not in tokenizer.decode(chain(*personality)):
personality= random.choice(personalities)
book[unit]=personality
# test_personalities= [book['in a hospital'],book['hotel check-in']]
personality = random.choice(personalities)
contents=[tokenizer.decode(i) for i in personality]
for unit, pers in book.items():
if personality[0] in pers:
chapter=unit
# print(f"personality: {personality}")
# print("_________________________")
# print(pers)
print(f"chapter:{chapter}")
SPELL_API_KEY="6e93cb58ed3b4ad594947f95c0c32600"
SPELL_params = {
'mkt':'en-us',
'mode':'proof'
}
SPELL_headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Ocp-Apim-Subscription-Key': SPELL_API_KEY,
}
M_tokenizer = AutoTokenizer.from_pretrained("bert-base-cased-finetuned-mrpc")
M_model = AutoModelForSequenceClassification.from_pretrained("bert-base-cased-finetuned-mrpc")
C_model = BertForSequenceClassification.from_pretrained("/home/ubuntu/server/transfer-learning-conv-ai/data/CoLAoutput")
C_tokenizer= BertTokenizer.from_pretrained("/home/ubuntu/server/transfer-learning-conv-ai/data/CoLAoutput")
chatbot=ChatBot(args,tokenizer,model,personalities,personality,contents,chapter)
print(chatbot.contents)
CoLA =AFL(C_model,C_tokenizer,"CoLA")
MRPC =AFL(M_model,M_tokenizer,"MRPC")
Redundancy = AFL(M_model,M_tokenizer,"Redundancy")
@app.route('/prediction', methods = ['POST'])
def prediction():
sentence = request.get_json()#json 데이터를 받아옴
# sentence = request.get_json()#json 데이터를 받아옴
result_conv=chatbot.return_message(sample_json=sentence)
result_mrpc =MRPC.return_prediction(chatbot,sample_json=sentence)
result_cola =CoLA.return_prediction(chatbot,sample_json=sentence)
result_spell= AFL.spell_check(sentence,SPELL_API_KEY,SPELL_params,SPELL_headers)
result_redundancy = Redundancy.return_prediction(chatbot,result_conv)
print(result_redundancy)
AFL.count+=1
print(chatbot.history)
results={'sentence': result_conv,'similarity':result_mrpc,'correct':result_cola ,'contents':chatbot.contents ,'count':AFL.count,'spell': result_spell if result_spell else ['nothing to change!'] ,'isChanged':AFL.changed_flag,"chapter":chatbot.chapter }
AFL.changed_flag=False
if AFL.count >= 4:## 나중에 5턴
CoLA_avg = CoLA.average()
MRPC_avg = MRPC.average()
if CoLA_avg >70 and MRPC_avg > 65 and AFL.changed_flag ==False :
chatbot.personality = AFL.change_content(chatbot,book)
AFL.count =0
elif result_redundancy >70:
print("너무 똑같아서 바꿈")
chatbot.personality = AFL.change_content(chatbot ,book)
Redundancy.answer=[]
AFL.count =0
chatbot.history=[]
return jsonify(results)# 받아온 데이터를 다시 전송
@app.route('/first' , methods=['GET'])
def first():
return jsonify({'chapter':chatbot.chapter})
# def change_content():
if __name__ == "__main__":
app.run(host='0.0.0.0')