Views
No views yet
self.tokenizer = AutoTokenizer.from_pretrained(model_directory)
self.tokenizer.add_special_tokens({'pad_token': '[PAD]'})
self.gpt3_model = GPTNeoForCausalLM.from_pretrained(model_directory)
self.gpt3_model.to(device) # Move model to the device (GPU or CPU)
self.load_memory_file = "C:\\Users\\withe\\PycharmProjects\\no hope2\\Chat_Bot4\\load_memory.json"
self.save_memory_file = "C:\\Users\\withe\\PycharmProjects\\no hope2\\Chat_Bot4\\save_memory.json"
self.memory_module = MemoryModule(self.load_memory_file, self.save_memory_file)
self.sentiment_module = SentimentAnalysisModule()
self.speech_engine = speech_engine # Assign the initialized speech engine
self.max_sequence_length = 200 # Decrease the value for faster response
self.num_beams = 4 # Reduce the value for faster response
self.no_repeat_ngram_size = 2
self.temperature = 0.3
self.response_cache = {} # Cache for storing frequently occurring responses
# Initialize speech recognition
self.recognizer = sr.Recognizer()
def reset_conversation(self):
self.memory_module.reset_memory()
def retrieve_cached_response(self, input_text):
named_entities = self.memory_module.get_named_entities()
for entity in named_entities:
if entity.lower() in input_text.lower():
return self.response_cache.get(entity)
return None
def generate_gpt2_response(self, input_text, conversation_history):
# Prepare the conversation history for GPT-2 input format
if len(conversation_history) == 0:
gpt2_input = "USER: " + input_text + "\n"
else:
gpt2_input = "USER: " + conversation_history[-1] + "\n" # Append the user's query
gpt2_input += "BOT: " + conversation_history[-2] + "\n" # Append the bot's previous response
# Append the rest of the conversation history in reverse order
for i in range(len(conversation_history) - 3, -1, -2):
gpt2_input += "USER: " + conversation_history[i] + "\n"
gpt2_input += "BOT: " + conversation_history[i - 1] + "\n"
# Append the current user input to the conversation history
gpt2_input += "USER: " + input_text + "\n"
# Tokenize the input text
input_ids = self.tokenizer.encode(gpt2_input, return_tensors='pt')
# Generate response using the GPT-2 model
with torch.no_grad():
output = self.model.generate(input_ids, max_length=100, num_return_sequences=1)
# Decode the generated response
generated_text = self.tokenizer.decode(output[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
# Process the GPT-2 response
response = generated_text.strip().split("\n")[-1] # Extract the last line (bot's response)
return response
def process_input(self, input_text, conversation_history):
named_entities = list(self.memory_module.get_named_entities())
for entity in named_entities:
if entity in input_text:
response = self.generate_gpt2_response(input_text, conversation_history)
self.memory_module.add_to_memory(response)
return response
# Check if the input contains a question
if '?' in input_text:
return "You're making me angry, you wouldn't like me when I'm angry."
# Check if the input is a greeting
greetings = ['hello', 'hi', 'hey', 'hola']
for greeting in greetings:
if greeting in input_text.lower():
return "Hello! How can I assist you today?"
# Check if the input is a statement about the model
if self.name.lower() in input_text.lower():
return "Yes, I am {}. How can I assist you today?".format(self.name)
# Check if the input is a statement about the creator
if 'creator' in input_text.lower():
return "I was created by {}.".format(self.param1)
# Check if the input is a sentiment analysis request
if 'sentiment' in input_text.lower():
sentiment = self.sentiment_module.analyze_sentiment(input_text)
if sentiment == 'positive':
return "The sentiment of the text is positive."
elif sentiment == 'negative':
return "The sentiment of the text is negative."
else:
return "The sentiment of the text is neutral."
# Retrieve a cached response if available
cached_response = self.retrieve_cached_response(input_text)
if cached_response:
return cached_response
# Generate a response using GPT-2
response = self.generate_gpt2_response(input_text, conversation_history)
# Update the conversation history and cache the response
conversation_history.append(input_text)
conversation_history.append(response)
self.response_cache[input_text] = response
# Update memory with the generated response
self.memory_module.add_to_memory(response)
return responsetry:
user_input = recognizer.recognize_google(audio)
print("User:", user_input)
except sr.UnknownValueError:
print("Sorry, I could not understand your speech.")
continue
except sr.RequestError:
print("Sorry, the speech recognition service is currently unavailable.")
continue
response = common_module.process_input(user_input, [])
print("Bot:", response)
text_to_speech(response)
MEMORY_MODULEdef add_to_memory(self, statement):
self.memory.append(statement)
self.save_memory()
def reset_memory(self):
self.memory = []
self.save_memory()
def save_memory(self):
with open(self.save_file, 'w') as file:
json.dump(self.memory, file)
def load_memory(self):
try:
with open(self.load_file, 'r') as file:
loaded_memory = json.load(file)
if isinstance(loaded_memory, list):
self.memory = loaded_memory
else:
print("Loaded memory is not a list. Starting with an empty memory.")
except FileNotFoundError:
print("Load memory file not found. Starting with an empty memory.")
def get_named_entities(self):
named_entities = set()
for statement in self.memory:
doc = nlp(statement)
for entity in doc.ents:
if entity.label_:
named_entities.add(entity.text)
return named_entitiesdef analyze_sentiment(self, text):
sentiment = self.sia.polarity_scores(text)
compound_score = sentiment['compound']
if compound_score >= 0.05:
return 'positive'
elif compound_score <= -0.05:
return 'negative'
else:
return 'neutral'