CelebChat / celebbot.py
lhzstar
new commits
edcdcdb
raw
history blame
7.79 kB
import datetime
import numpy as np
import torch
import torch.nn.functional as F
import os
import json
import speech_recognition as sr
import re
import time
import spacy
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, AutoModel
import pickle
import streamlit as st
from sklearn.metrics.pairwise import cosine_similarity
# Build the AI
class CelebBot():
def __init__(self, name, gender, QA_tokenizer, QA_model, sentTr_tokenizer, sentTr_model, spacy_model, knowledge_sents):
self.name = name
self.gender = gender
print("--- starting up", self.name, self.gender, "---")
self.text = ""
self.QA_tokenizer = QA_tokenizer
self.QA_model = QA_model
self.sentTr_tokenizer = sentTr_tokenizer
self.sentTr_model = sentTr_model
self.spacy_model = spacy_model
self.all_knowledge = knowledge_sents
def speech_to_text(self):
recognizer = sr.Recognizer()
with sr.Microphone() as mic:
recognizer.adjust_for_ambient_noise(mic, duration=1)
# flag = input("Are you ready to record?\nProceed (Y/n)")
# try:
# assert flag=='Y'
# except:
# self.text = ""
# print(f"me --> Permission denied")
time.sleep(1)
print("listening")
audio = recognizer.listen(mic)
try:
self.text = recognizer.recognize_google(audio)
except:
self.text = ""
print(f"me --> No audio recognized")
def text_to_speech(self, autoplay=True):
import run_tts
return run_tts.tts(self.text, "_".join(self.name.split(" ")), self.spacy_model, autoplay)
def sentence_embeds_inference(self, texts: list):
def _mean_pooling(model_output, attention_mask):
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
# Tokenize sentences
encoded_input = self.sentTr_tokenizer(texts, padding=True, truncation=True, return_tensors='pt')
encoded_input["input_ids"] = encoded_input["input_ids"].to(self.sentTr_model.device)
encoded_input["attention_mask"] = encoded_input["attention_mask"].to(self.sentTr_model.device)
# Compute token embeddings
with torch.no_grad():
model_output = self.sentTr_model(**encoded_input)
# Perform pooling
sentence_embeddings = _mean_pooling(model_output, encoded_input['attention_mask'])
# Normalize embeddings
sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
return sentence_embeddings
def retrieve_knowledge_assertions(self, change_person=True):
question_embeddings = self.sentence_embeds_inference([self.text])
all_knowledge_embeddings = self.sentence_embeds_inference(self.all_knowledge)
similarity = cosine_similarity(all_knowledge_embeddings.cpu(), question_embeddings.cpu())
similarity = np.reshape(similarity, (1, -1))[0]
K = min(8, len(self.all_knowledge))
top_K = np.sort(np.argpartition(similarity, -K)[-K: ])
all_knowledge_assertions = np.array(self.all_knowledge)[top_K]
# similarities = np.array(similarity)[top_K]
# print(*all_knowledge_assertions, sep='\n')
if change_person:
all_knowledge_assertions = [self.third_to_first_person(sent) for sent in all_knowledge_assertions]
return " ".join(all_knowledge_assertions)
def third_to_first_person(self, text):
text = text.replace(" ", " ")
name = self.name.split(" ")[-1].lower()
doc = self.spacy_model(text)
transformed_text = []
for i, token in enumerate(doc):
if self.gender == "M":
if token.text.lower() == "he":
transformed_text.append("I")
elif token.text.lower() == "him":
transformed_text.append("me")
elif token.text.lower() == "his":
transformed_text.append("my")
elif token.text.lower() == name and token.dep_ in ["nsubj", "nsubjpass"]:
transformed_text.append("I")
elif token.text in ["'s", "’s"] and doc[i-1].text.lower() == name:
transformed_text[-1] = "my"
elif token.text.lower() == "their":
transformed_text.append("our")
elif token.text.lower() == "they":
transformed_text.append("we")
else:
transformed_text.append(token.text)
elif self.gender == "F":
if token.text.lower() == "she":
transformed_text.append("I")
elif token.text.lower() == "her":
if i != len(doc)-1 and doc[i+2].dep_ in ["nsubj", "nsubjpass", "dobj", "appos", "dative", "attr", "amod", "nummod", "compound", "pobj", "pcomp"]:
transformed_text.append("my")
else:
transformed_text.append("me")
elif token.text.lower() == name and token.dep_ in ["nsubj", "nsubjpass"]:
transformed_text.append("I")
elif token.text in ["'s", "’s"] and doc[i-1].text.lower() == name:
transformed_text[-1] = "my"
elif token.text.lower() == "their":
transformed_text.append("our")
elif token.text.lower() == "they":
transformed_text.append("we")
else:
transformed_text.append(token.text)
return "".join(transformed_text)
def question_answer(self, instruction='', knowledge=''):
instruction = f"Your name is {self.name}. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information."
if self.text != "":
if re.search(re.compile(rf'\b(you|your|yours)\b', flags=re.IGNORECASE), self.text) != None:
knowledge = self.retrieve_knowledge_assertions()
else:
knowledge = self.retrieve_knowledge_assertions(change_person=False)
query = f"Context: {instruction} {knowledge}\n\nQuestion: {self.text}\n\nAnswer:"
input_ids = self.QA_tokenizer(f"{query}", return_tensors="pt").input_ids.to(self.QA_model.device)
outputs = self.QA_model.generate(input_ids, max_length=1024, min_length=8, do_sample=True, temperature=0.2, repetition_penalty=2.5)
self.text = self.QA_tokenizer.decode(outputs[0], skip_special_tokens=True)
# self.text = " ".join([i.text.strip().capitalize() for i in self.spacy_model(self.text).sents])
return self.text
@staticmethod
def action_time():
return f"it's {datetime.datetime.now().time().strftime('%H:%M')}"
@staticmethod
def save_kb(kb, filename):
with open(filename, "wb") as f:
pickle.dump(kb, f)
@staticmethod
def load_kb(filename):
res = None
with open(filename, "rb") as f:
res = pickle.load(f)
return res