working_backend

This commit is contained in:
ahmed531998 2023-04-09 22:43:13 +02:00
parent 7e58c32311
commit 7e1bf49d0d
3 changed files with 28 additions and 18 deletions

5
DM.py
View File

@ -50,7 +50,4 @@ class DM:
elif self.curr_state['intent'] == 'SUMMARIZEPAPER': elif self.curr_state['intent'] == 'SUMMARIZEPAPER':
return "sumPaper" return "sumPaper"
else: else:
if self.curr_state['intent'] in ['FINDPAPER', 'FINDDATASET', 'SUMMARIZEPAPER'] and len(self.curr_state['entities']) == 0: return "Clarify"
return "ClarifyResource"
else:
return "GenClarify"

View File

@ -9,26 +9,38 @@ class Recommender:
self.retriever = retriever self.retriever = retriever
self.rand_seed = 5 self.rand_seed = 5
def _new(self, material):
for row in self.curr_recommendations:
print(row)
if row['id'] == material['id'] and row['type'] == material['type']:
return False
return True
def _match_tags(self, material, interest): def _match_tags(self, material, interest):
score = 0.7 score = 0.7
for tag in material['tags']: for tag in material['tags']:
if cosine_similarity(np.array(self.retriever.encode([tag])), if cosine_similarity(np.array(self.retriever.encode([tag])),
np.array(self.retriever.encode([interest]))) > score: np.array(self.retriever.encode([interest]))) > score:
if material not in self.curr_recommendations: if self._new(material):
print('hi')
self.curr_recommendations.append(material) self.curr_recommendations.append(material)
self.recommended.append(False) self.recommended.append(False)
def generate_recommendations(self, interests, new_material): def generate_recommendations(self, interests, new_material):
for interest in interests: for interest in interests:
for material in new_material: for i, material in new_material.iterrows():
self._match_tags(material, interest) self._match_tags(material, interest)
def make_recommendation(self, user): def make_recommendation(self, user):
if len(self.curr_recommendations) == 0: if len(self.curr_recommendations) == 0:
return "" return ""
index = random.choice(list(range(0, len(self.curr_recommendations)))) to_consider = [idx for idx, value in enumerate(self.recommended) if value == False]
while self.recommended[index] == True: if len(to_consider) == 0:
index = random.choice(list(range(0, len(self.curr_recommendations)))) return ""
index = random.choice(list(range(0, len(to_consider))))
index = self.recommended[index]
#while self.recommended[index] == True:
# index = random.choice(list(range(0, len(self.curr_recommendations))))
recommendation = "Hey " + user + "! This " + self.curr_recommendations[index][ recommendation = "Hey " + user + "! This " + self.curr_recommendations[index][
'type'].lower() + " about " + ', '.join( 'type'].lower() + " about " + ', '.join(
self.curr_recommendations[index]['tags']).lower() + " was posted recently by " + \ self.curr_recommendations[index]['tags']).lower() + " was posted recently by " + \

View File

@ -168,13 +168,14 @@ class ResponseGenerator:
gen_kwargs = {"length_penalty": 1.5, "num_beams":6, "max_length": 120} gen_kwargs = {"length_penalty": 1.5, "num_beams":6, "max_length": 120}
answer = self.generators['summ'](gen_seq, **gen_kwargs)[0]['generated_text'] + ' ' answer = self.generators['summ'](gen_seq, **gen_kwargs)[0]['generated_text'] + ' '
return answer return answer
elif action == "Clarify":
elif action == "ClarifyResource": if state['intent'] in ['FINDPAPER', 'SUMMARIZEPAPER'] and len(state['entities']) == 0:
if state['intent'] in ['FINDPAPER', 'SUMMARIZEPAPER']: if len(self.paper) == 0:
return 'Please specify the title, the topic or the paper of interest.' return 'Please specify the title, the topic of the paper of interest.'
elif state['intent'] == 'FINDDATASET' and len(state['entities']) == 0:
if len(self.dataset) == 0:
return 'Please specify the title, the topic of the dataset of interest.'
else: else:
return 'Please specify the title, the topic or the dataset of interest.' gen_kwargs = {"length_penalty": 2.5, "num_beams":8, "max_length": 120}
elif action == "GenClarify": question = self.generators['amb']('question: '+ utterance + ' context: ' + consec_history , **gen_kwargs)[0]['generated_text']
gen_kwargs = {"length_penalty": 2.5, "num_beams":8, "max_length": 120} return question
question = self.generators['amb']('question: '+ utterance + ' context: ' + consec_history , **gen_kwargs)[0]['generated_text']
return question