-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaibot.py
162 lines (131 loc) · 6.73 KB
/
aibot.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
from cleantext.clean import clean
#from aryana import *
#from Audio_Rec import convert_speech_text
from transformers import TFBertForSequenceClassification, TFAutoModelForTokenClassification
from transformers import BertTokenizer, AutoTokenizer, AutoConfig
from weatherAPI import Weather
from adhanAPI import Adhan
from timeAPI import Time
from calenderAPI import Calender
from aibot_utils import cleaning, classify_question, ner_question
TR_ID_AIBOTID = {0: "1", 1: "2", 4: "3", 3: "4", 2: "-1"}
CLASSIFIER_PATH = "../models/classifier"
PARSBERTNER_PATH = "../models/ner_model"
class BOT:
def __init__(self):
self.modified = False
# load models
self.classifier_tokenizer = BertTokenizer.from_pretrained(
CLASSIFIER_PATH)
self.classifier_config = AutoConfig.from_pretrained(CLASSIFIER_PATH)
self.classifier_model = TFBertForSequenceClassification.from_pretrained(
CLASSIFIER_PATH)
self.ner_tokenizer = AutoTokenizer.from_pretrained(PARSBERTNER_PATH)
self.ner_config = AutoConfig.from_pretrained(PARSBERTNER_PATH)
self.ner_model = TFAutoModelForTokenClassification.from_pretrained(
PARSBERTNER_PATH)
self.weather_api = Weather()
self.adhan_api = Adhan()
self.time_api = Time()
self.calender_api = Calender()
# self.deepm = Deepmine()
def is_modified(self):
return self.modified
'''
This method takes an string as input, the string contains the string of question.
If you are using this method, we presume that you want to use nevisa and ariana.
:Param Question : an string containing the question.
: return : A dictionary containing the type of question, corresponding arguments, api_url and result.
'''
# def aibot_voice(self, Address):
# answer = {'type': ['-1'], 'city': [], 'date': [],
# 'time': [], 'religious_time': [], 'calendar_type': [], 'event': [], 'api_url': '', 'result': []}
# r, Question = convert_speech_text(Address)
# if r == -1 or not Question:
# # print("error in sound conversion")
# response = aryana("مشکل در تشخیص صوت به وجود آمد")
# return answer, response, Question, ""
# # print("the sound has been converted to text {}".format(Question))
# # Question = google(Address)
# Question = cleaning(Question)
# type_pred = TR_ID_AIBOTID[classify_question(
# self.classifier_model, self.classifier_tokenizer, Question)]
# tokens, labels = ner_question(
# self.ner_model, self.ner_tokenizer, self.ner_config, Question)
# if "خاموش" in tokens:
# return None, None, 1
# if ("آهنگ" in tokens or "اهنگ" in tokens):
# return None, None, 2
# if type_pred == "-1":
# answer["type"] = ["-1"]
# generated_sentence = "سوال پرسیده شده خارج از توان بات میباشد"
# elif type_pred == "1":
# answer, generated_sentence = self.weather_api.get_answer(
# Question, tokens, labels)
# generated_sentence = cleaning(generated_sentence).replace("٫", "/")
# elif type_pred == "2":
# answer, generated_sentence = self.adhan_api.get_answer(
# Question, tokens, labels)
# if not answer:
# answer, generated_sentence = self.weather_api.get_answer(
# Question, tokens, labels)
# elif type_pred == "3":
# answer, generated_sentence = self.time_api.get_answer(
# Question, tokens, labels)
# else:
# answer, generated_sentence = self.calender_api.get_answer(
# Question, tokens, labels)
# if 'سلام' in tokens:
# generated_sentence = "سلام، " + generated_sentence
# if generated_sentence:
# response = aryana(generated_sentence)
# else:
# response = aryana("متاسفانه پاسخی یافت نشد")
# # print("answer has been generated: {}".format(generated_sentence))
# # with open("test.txt", 'w') as ftest:
# # print(Question, file=ftest)
# # print(answer, file=ftest)
# # print(generated_sentence, file=ftest)
# return answer, response, Question, generated_sentence
'''
This method takes an string as input, the string contains the address of a wav file.
You can either use your own speech recognition or nevisa to extract the question from that file.
Also you should call ariana to create an audio file as output.
:Param Address : an string containing the the address of a wav file.
: return : A dictionary containing the type of question, corresponding arguments, api_url and result.
'''
def aibot(self, Question):
answer = {'type': ['-1'], 'city': [], 'date': [],
'time': [], 'religious_time': [], 'calendar_type': [], 'event': [], 'api_url': '', 'result': []}
type_pred = TR_ID_AIBOTID[classify_question(
self.classifier_model, self.classifier_tokenizer, Question)]
tokens, labels = ner_question(
self.ner_model, self.ner_tokenizer, self.ner_config, Question)
Question = cleaning(Question)
if type_pred == "-1":
answer["type"] = ["-1"]
generated_sentence = "سوال پرسیده شده خارج از توان بات میباشد"
elif type_pred == "1":
answer, generated_sentence = self.weather_api.get_answer(
Question, tokens, labels)
generated_sentence = cleaning(generated_sentence).replace("٫", "/")
elif type_pred == "2":
answer, generated_sentence = self.adhan_api.get_answer(
Question, tokens, labels)
if not answer:
answer, generated_sentence = self.weather_api.get_answer(
Question, tokens, labels)
elif type_pred == "3":
answer, generated_sentence = self.time_api.get_answer(
Question, tokens, labels)
else:
answer, generated_sentence = self.calender_api.get_answer(
Question, tokens, labels)
return answer, generated_sentence
def AIBOT_Modified(self, Address):
answer = {'type': '0', 'city': [], 'date': [],
'time': [], 'religous_time': [], 'calendar_type': [], 'event': [], 'api_url': '', 'result': ''}
'''
You should implement your code right here.
'''
return answer