| 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 |
|
|
|
|
| |
| def preprocess_text(text): |
| |
| text = text.lower() |
| |
| text = re.sub(r'[^a-zA-Z0-9\'+\-*/\s]', ' ', text) |
| |
| text = text.replace('plus', '+') |
| |
| text = text.replace('minus', '-') |
| |
| text = text.replace('times', '*') |
| |
| text = text.replace('divided by', '/') |
| |
| text = text.replace('*', ' * ').replace('/', ' / ').replace('-', ' - ').replace('+', ' + ').replace('.', ' . ') |
| |
| text = re.sub(r'\d+', '<num>', text) |
| |
| text = text.split() |
| text = [word if word in vocab.word_index else '<oov>' for word in text] |
| text = ' '.join(text) |
| |
| 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) |
| 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(): |
| |
| 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(): |
| |
| 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] |
|
|
| |
| 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(): |
| |
| 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] |
| 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 |
|
|
|
|
| |
| model = tf.keras.models.load_model('main.h5') |
|
|
| |
| with open('tokenizer.pkl', 'rb') as file: |
| vocab = pickle.load(file) |
| print() |
|
|
| |
| while True: |
| input_text = input("user: ") |
|
|
| |
| if keyboard.is_pressed('ctrl'): |
| listen_and_convert() |
|
|
| if not process_input(input_text): |
| break |
|
|