Create BootleggerAI.py
Browse files- BootleggerAI.py +69 -0
BootleggerAI.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
import numpy as np
|
3 |
+
|
4 |
+
# Function to tokenize input data
|
5 |
+
def tokenize_input(data):
|
6 |
+
tokenizer = tf.keras.preprocessing.text.Tokenizer()
|
7 |
+
tokenizer.fit_on_texts(data)
|
8 |
+
vocab_size = len(tokenizer.word_index) + 1
|
9 |
+
input_seq = tokenizer.texts_to_sequences(data)
|
10 |
+
max_len = max([len(x) for x in input_seq])
|
11 |
+
input_seq = tf.keras.preprocessing.sequence.pad_sequences(input_seq, maxlen=max_len, padding='post')
|
12 |
+
return tokenizer, input_seq, max_len, vocab_size
|
13 |
+
|
14 |
+
# Function to define the neural network model
|
15 |
+
def define_model(vocab_size, max_len):
|
16 |
+
model = tf.keras.Sequential([
|
17 |
+
tf.keras.layers.Embedding(vocab_size, 16),
|
18 |
+
tf.keras.layers.GlobalAveragePooling1D(),
|
19 |
+
tf.keras.layers.Dense(16, activation='relu'),
|
20 |
+
tf.keras.layers.Dense(max_len, activation='softmax')
|
21 |
+
])
|
22 |
+
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
|
23 |
+
return model
|
24 |
+
|
25 |
+
# Function to train the neural network
|
26 |
+
def train_model(model, input_seq, output, epochs=10):
|
27 |
+
model.fit(input_seq, output, epochs=epochs, verbose=1)
|
28 |
+
return model
|
29 |
+
|
30 |
+
# Function to test the model on new input
|
31 |
+
def test_model(model, test_data, tokenizer, max_len):
|
32 |
+
test_seq = tokenizer.texts_to_sequences(test_data)
|
33 |
+
test_seq = tf.keras.preprocessing.sequence.pad_sequences(test_seq, maxlen=max_len, padding='post')
|
34 |
+
predictions = model.predict(test_seq)
|
35 |
+
return predictions
|
36 |
+
|
37 |
+
# Define the input and output data
|
38 |
+
data = np.array(['create a python script', 'build a program', 'generate a code'])
|
39 |
+
output = np.array([['create', 'python', 'script'], ['build', 'program'], ['generate', 'code']])
|
40 |
+
|
41 |
+
# Tokenize the input data
|
42 |
+
tokenizer, input_seq, max_len, vocab_size = tokenize_input(data)
|
43 |
+
|
44 |
+
# Define the neural network model
|
45 |
+
model = define_model(vocab_size, max_len)
|
46 |
+
|
47 |
+
# Train the neural network model
|
48 |
+
model = train_model(model, input_seq, output)
|
49 |
+
|
50 |
+
# Define a function to generate a response based on the predicted output
|
51 |
+
def generate_response(predictions, tokenizer):
|
52 |
+
output = [tokenizer.index_word[i] for i in np.argmax(predictions, axis=1)]
|
53 |
+
if 'create' in output and 'python' in output and 'script' in output:
|
54 |
+
return 'Sure, I can help you with that! What kind of script would you like to create?'
|
55 |
+
elif 'build' in output and 'program' in output:
|
56 |
+
return 'Of course! What programming language would you like to use for your program?'
|
57 |
+
elif 'generate' in output and 'code' in output:
|
58 |
+
return 'Certainly! What kind of code do you need generated?'
|
59 |
+
else:
|
60 |
+
return "I'm sorry, I didn't understand your request. Can you please rephrase it?"
|
61 |
+
|
62 |
+
# Implement the chatbot
|
63 |
+
print("Hi, I'm BootLeggerAI, a chatbot that can help you with your programming needs! What can I assist you with today?")
|
64 |
+
while True:
|
65 |
+
user_input = input("Please enter your request: ")
|
66 |
+
test_data = np.array([user_input])
|
67 |
+
predictions = test_model(model, test_data, tokenizer, max_len)
|
68 |
+
response = generate_response(predictions, tokenizer)
|
69 |
+
print(response)
|