+1 (315) 557-6473 

Python Program to Implement AI Essay Writer Assignment Solution.


Write a program to implement AI essay writer in python.

Requirements and Specifications

The assignment was " Create an AI research assistant to research and write an essay for you.
Class is : Artificial Intelligence CSC370
Book: Artificial Intelligence by Example: Develop machine intelligence from scratch using real artificial intelligence use cases 1st Edition, by Denis Rothman
Source Code
# Required Imports
pip install wikipedia
import tensorflow as tf
from tensorflow import keras
import numpy as np
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from keras.utils.data_utils import get_file
import sys
import io
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
import wikipedia
# Search on Wikipedia for topic
### Inf the following cell, you'll see a list with different topics that are related between them. If you want to change the topic, try adding terms related to the same topic so the info researched is bigger
topic = [
'black hole',
'Stephen Hawkings',
'speed of light',
'general relativity',
'albert einstein',
'hawking radiation',
'gravitatonal waves',
topic_text = ""
for term in topic:
# Specify the title of the Wikipedia page
wiki = wikipedia.page(term)
# Extract the plain text content of the page, excluding images, tables, and other data.
text = wiki.content
# Replace '==' with '' (an empty string)
text = text.replace('==', '')
topic_text += text
# Tokenize words
tokenizer = Tokenizer()
corpus = topic_text.lower().split("\n")
total_words = len(tokenizer.word_index)+1
print(f"A total of {total_words} words were tokenized")
# Vectorization: Convert each line of text into a text sequence
input_sequences = []
for line in corpus:
token_list = tokenizer.texts_to_sequences([line])[0]
for i in range(1, len(token_list)):
n_gram_sequence = token_list[:i+1]
max_sequence_len = max([len(x) for x in input_sequences])
padded = pad_sequences(input_sequences, padding='pre', maxlen=max_sequence_len)
# Split the data into X and y
X = padded[:,:-1]
Y = padded[:,-1]
# Categorize the labels (target variable)
y = tf.keras.utils.to_categorical(Y, num_classes = total_words)
# Model
### **NOTE:** If you want to create the model from zero, just run the following cell. If you want to load a pre-trained model, skip the following cell and execute the next one
model = keras.Sequential([
tf.keras.layers.Embedding(total_words, 64, input_length = max_sequence_len-1),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64, return_sequences = True)),
tf.keras.layers.Dense(total_words, activation = 'softmax')
model.compile(loss='categorical_crossentropy', optimizer = 'adam', metrics=['accuracy'])
###Execute the next cell if you want to load a pre-trained model
saved_model_name = 'essay_writer_black_hole.h5'
model = keras.models.load_model(saved_model_name)
# Show summary of Model
# Train Model
history = model.fit(X, y, epochs=30)
topic_str = topic[0].replace(' ', '_')
# Finally, write an essay. In the parameter **next_words** you can declare the length of the Essay
# Predict
essay_text = "Black holes"
next_words = 500
for i in range(next_words):
token_list = tokenizer.texts_to_sequences([essay_text])[0]
token_list = pad_sequences([token_list], maxlen = max_sequence_len-1, padding='pre')
#predicted = model.predict_classes(token_list, verbose=0)
y_pred = model.predict(token_list, verbose=0)
predicted = np.argmax(y_pred)
output_word = ""
for word, index in tokenizer.word_index.items():
if index == predicted:
output_word = word
essay_text += " " + output_word
if i%16 == 0:
essay_text += "\n"