print("Initializing...") import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' import pickle import re import webbrowser as wb import tensorflow as tf import numpy as np import speech_recognition as sr import keyboard # Define the function to preprocess the input text def preprocess_text(text): # Convert the text to lowercase text = text.lower() # Replace any non-alphanumeric characters with a space text = re.sub(r'[^a-zA-Z0-9\'+\-*/\s]', ' ', text) # Replace the word 'plus' with the '+' symbol text = text.replace('plus', '+') # Replace the word 'minus' with the '-' symbol text = text.replace('minus', '-') # Replace the word 'times' with the '*' symbol text = text.replace('times', '*') # Replace the word 'divided by' with the '/' symbol text = text.replace('divided by', '/') # Replace the characters * / - + with a space and the character itself text = text.replace('*', ' * ').replace('/', ' / ').replace('-', ' - ').replace('+', ' + ').replace('.', ' . ') # Replace all numbers with a special token text = re.sub(r'\d+', '', text) # Replace tokens that are not present in the tokenizer with a special token text = text.split() text = [word if word in vocab.word_index else '' for word in text] text = ' '.join(text) # Remove any extra whitespace text = re.sub(r'\s+', ' ', text) return text.strip() def listen_and_convert(): recognizer = sr.Recognizer() with sr.Microphone() as source: print("Recording... Speak something:") audio = recognizer.listen(source) print("Processing...") try: text = recognizer.recognize_google(audio) print(text) process_input(text) # Process the converted text using the existing function except sr.UnknownValueError: print("Google Speech Recognition could not understand the audio.") except sr.RequestError as e: print(f"Could not request results from Google Speech Recognition service; {e}") def site(): # Define a regex pattern to find URL url_pattern = r'(?:http://|https://)?(\S+\.(?:com|org|net|edu|gov|me|ai|io))' matches = re.findall(url_pattern, input_text) url = None if matches: url = "https://" + matches[0] elif "chatgpt" in input_text: url = "chat.openai.com" elif "email" in input_text: url = "mail.google.com" elif "gmail" in input_text: url = "mail.google.com" elif "youtube" in input_text: url = "youtube.com" elif "pandora" in input_text: url = "pandora.com" elif "spotify" in input_text: url = "spotify.com" if url is not None: print("opened " + url) wb.get('windows-default').open(url) else: print("Somethings wrong, check your input, if your input is good then create a bug report with the following: " f"category: open website, input: {input_text}") def search(): # Remove common words that don't contribute to search terms stop_words = {'search', 'for', 'find', 'on', 'the', 'web', 'do', 'do a', 'google'} words = input_text.split() filtered_words = [word for word in words if word.lower() not in stop_words] # Extract the remaining words as search terms search_terms = ' '.join(filtered_words) print("searched for " + search_terms) search_url = f"https://duckduckgo.com/?q={search_terms}" wb.get('windows-default').open(search_url) def math(): # Define a regex pattern to match basic math expressions (e.g., 2 + 3, 4 * 5, etc.) math_pattern = r'(\d+(\.\d+)?(\s*(\+|-|\*|\/)\s*\d+(\.\d+)?)+)' matches = re.findall(math_pattern, input_text) return matches[0][0] if matches else None def process_input(text): if text == "/quit": return False processed_input_text = preprocess_text(text) test_data = [processed_input_text] # Wrap the input data in a list test_encoded = vocab.texts_to_sequences(test_data) test_padded = tf.keras.preprocessing.sequence.pad_sequences(test_encoded, maxlen=15, padding='post') predictions = model.predict(test_padded) predicted_category_number = np.argmax(predictions[0]) predicted_category = "report this as logic error 1 and with steps to reproduce." if predicted_category_number == 0: predicted_category = "math" math() elif predicted_category_number == 1: predicted_category = "web search" search() elif predicted_category_number == 2: predicted_category = "open website" site() print(f"Predicted Function: {predicted_category}") return True # Load the saved model model = tf.keras.models.load_model('main.h5') # Load the tokenizer using pickle with open('tokenizer.pkl', 'rb') as file: vocab = pickle.load(file) print() # Enter into an interactive loop to test the model while True: input_text = input("user: ") # Check for the hotkey press if keyboard.is_pressed('ctrl'): listen_and_convert() # If hotkey pressed, start listening and convert speech to text if not process_input(input_text): break