File size: 5,189 Bytes
8e8f2a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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+', '<num>', 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 '<oov>' 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