|
import tensorflow as tf |
|
import numpy as np |
|
|
|
|
|
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 |
|
|
|
|
|
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 |
|
|
|
|
|
def train_model(model, input_seq, output, epochs=10): |
|
model.fit(input_seq, output, epochs=epochs, verbose=1) |
|
return model |
|
|
|
|
|
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 |
|
|
|
|
|
data = np.array(['create a python script', 'build a program', 'generate a code']) |
|
output = np.array([['create', 'python', 'script'], ['build', 'program'], ['generate', 'code']]) |
|
|
|
|
|
tokenizer, input_seq, max_len, vocab_size = tokenize_input(data) |
|
|
|
|
|
model = define_model(vocab_size, max_len) |
|
|
|
|
|
model = train_model(model, input_seq, 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?" |
|
|
|
|
|
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) |