File size: 3,030 Bytes
093e4b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import tensorflow as tf
import numpy as np

# Function to tokenize input data
def tokenize_input(data):
    tokenizer = tf.keras.preprocessing.text.Tokenizer()
    tokenizer.fit_on_texts(data)
    vocab_size = len(tokenizer.word_index) + 1
    input_seq = tokenizer.texts_to_sequences(data)
    max_len = max([len(x) for x in input_seq])
    input_seq = tf.keras.preprocessing.sequence.pad_sequences(input_seq, maxlen=max_len, padding='post')
    return tokenizer, input_seq, max_len, vocab_size

# Function to define the neural network model
def define_model(vocab_size, max_len):
    model = tf.keras.Sequential([
        tf.keras.layers.Embedding(vocab_size, 16),
        tf.keras.layers.GlobalAveragePooling1D(),
        tf.keras.layers.Dense(16, activation='relu'),
        tf.keras.layers.Dense(max_len, activation='softmax')
    ])
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

# Function to train the neural network
def train_model(model, input_seq, output, epochs=10):
    model.fit(input_seq, output, epochs=epochs, verbose=1)
    return model

# Function to test the model on new input
def test_model(model, test_data, tokenizer, max_len):
    test_seq = tokenizer.texts_to_sequences(test_data)
    test_seq = tf.keras.preprocessing.sequence.pad_sequences(test_seq, maxlen=max_len, padding='post')
    predictions = model.predict(test_seq)
    return predictions

# Define the input and output data
data = np.array(['create a python script', 'build a program', 'generate a code'])
output = np.array([['create', 'python', 'script'], ['build', 'program'], ['generate', 'code']])

# Tokenize the input data
tokenizer, input_seq, max_len, vocab_size = tokenize_input(data)

# Define the neural network model
model = define_model(vocab_size, max_len)

# Train the neural network model
model = train_model(model, input_seq, output)

# Define a function to generate a response based on the predicted output
def generate_response(predictions, tokenizer):
    output = [tokenizer.index_word[i] for i in np.argmax(predictions, axis=1)]
    if 'create' in output and 'python' in output and 'script' in output:
        return 'Sure, I can help you with that! What kind of script would you like to create?'
    elif 'build' in output and 'program' in output:
        return 'Of course! What programming language would you like to use for your program?'
    elif 'generate' in output and 'code' in output:
        return 'Certainly! What kind of code do you need generated?'
    else:
        return "I'm sorry, I didn't understand your request. Can you please rephrase it?"

# Implement the chatbot
print("Hi, I'm BootLeggerAI, a chatbot that can help you with your programming needs! What can I assist you with today?")
while True:
    user_input = input("Please enter your request: ")
    test_data = np.array([user_input])
    predictions = test_model(model, test_data, tokenizer, max_len)
    response = generate_response(predictions, tokenizer)
    print(response)