<jupyter_start><jupyter_text><jupyter_code>#I ran this originally with a text file containing both the full texts of *Infinite Jest* and *The Broom of the System.*
import random

#FROM WEASYPRINT TUTORIAL
!pip install weasyprint
from weasyprint import HTML

#INSTALL MARKOVIFY
!pip install -q markovify
import markovify
#FROM TENSORFLOW TEXT_GENERATION NOTEBOOK

from __future__ import absolute_import, division, print_function, unicode_literals

try:
  %tensorflow_version 2.x
except Exception:
  pass
import tensorflow as tf

import numpy as np
import os
import time
text = open('inf.txt', 'rb').read().decode(encoding='utf-8')
print ('Length of text: {} characters'.format(len(text)))
print(text[:250])
vocab = sorted(set(text))
print ('{} unique characters'.format(len(vocab)))
char2idx = {u:i for i, u in enumerate(vocab)}
idx2char = np.array(vocab)

text_as_int = np.array([char2idx[c] for c in text])
print('{')
for char,_ in zip(char2idx, range(20)):
    print('  {:4s}: {:3d},'.format(repr(char), char2idx[char]))
print('  ...\n}')
print ('{} ---- characters mapped to int ---- > {}'.format(repr(text[:13]), text_as_int[:13]))
seq_length = 100
examples_per_epoch = len(text)//(seq_length+1)

char_dataset = tf.data.Dataset.from_tensor_slices(text_as_int)

for i in char_dataset.take(5):
  print(idx2char[i.numpy()])
sequences = char_dataset.batch(seq_length+1, drop_remainder=True)

for item in sequences.take(5):
  print(repr(''.join(idx2char[item.numpy()])))
def split_input_target(chunk):
    input_text = chunk[:-1]
    target_text = chunk[1:]
    return input_text, target_text

dataset = sequences.map(split_input_target)
for input_example, target_example in  dataset.take(1):
  print ('Input data: ', repr(''.join(idx2char[input_example.numpy()])))
  print ('Target data:', repr(''.join(idx2char[target_example.numpy()])))
for i, (input_idx, target_idx) in enumerate(zip(input_example[:5], target_example[:5])):
    print("Step {:4d}".format(i))
    print("  input: {} ({:s})".format(input_idx, repr(idx2char[input_idx])))
    print("  expected output: {} ({:s})".format(target_idx, repr(idx2char[target_idx])))
BATCH_SIZE = 64

BUFFER_SIZE = 10000

dataset = dataset.shuffle(BUFFER_SIZE).batch(BATCH_SIZE, drop_remainder=True)

dataset
vocab_size = len(vocab)
embedding_dim = 256
rnn_units = 1024
def build_model(vocab_size, embedding_dim, rnn_units, batch_size):
  model = tf.keras.Sequential([
    tf.keras.layers.Embedding(vocab_size, embedding_dim,
                              batch_input_shape=[batch_size, None]),
    tf.keras.layers.GRU(rnn_units,
                        return_sequences=True,
                        stateful=True,
                        recurrent_initializer='glorot_uniform'),
    tf.keras.layers.Dense(vocab_size)
  ])
  return model
model = build_model(
  vocab_size = len(vocab),
  embedding_dim=embedding_dim,
  rnn_units=rnn_units,
  batch_size=BATCH_SIZE)
for input_example_batch, target_example_batch in dataset.take(1):
  example_batch_predictions = model(input_example_batch)
  print(example_batch_predictions.shape, "# (batch_size, sequence_length, vocab_size)")
model.summary()
sampled_indices = tf.random.categorical(example_batch_predictions[0], num_samples=1)
sampled_indices = tf.squeeze(sampled_indices,axis=-1).numpy()
sampled_indices
print("Input: \n", repr("".join(idx2char[input_example_batch[0]])))
print()
print("Next Char Predictions: \n", repr("".join(idx2char[sampled_indices ])))
def loss(labels, logits):
  return tf.keras.losses.sparse_categorical_crossentropy(labels, logits, from_logits=True)

example_batch_loss  = loss(target_example_batch, example_batch_predictions)
print("Prediction shape: ", example_batch_predictions.shape, " # (batch_size, sequence_length, vocab_size)")
print("scalar_loss:      ", example_batch_loss.numpy().mean())
model.compile(optimizer='adam', loss=loss)
checkpoint_dir = './training_checkpoints'

checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt_{epoch}")

checkpoint_callback=tf.keras.callbacks.ModelCheckpoint(
    filepath=checkpoint_prefix,
    save_weights_only=True)
#takes about an hour for this novel
EPOCHS=50
history = model.fit(dataset, epochs=EPOCHS, callbacks=[checkpoint_callback])
#link to epoch with least loss
tf.train.latest_checkpoint("/content/training_checkpoints/ckpt_22.index")
model = build_model(vocab_size, embedding_dim, rnn_units, batch_size=1)

model.load_weights(tf.train.latest_checkpoint(checkpoint_dir))

model.build(tf.TensorShape([1, None]))
model.summary()
def generate_text(model, start_string):

  num_generate = 2000000

  input_eval = [char2idx[s] for s in start_string]
  input_eval = tf.expand_dims(input_eval, 0)

  text_generated = []

  #since this text is abstract already, a lower setting works better.
  temperature = 0.5


  model.reset_states()
  for i in range(num_generate):
      predictions = model(input_eval)
      predictions = tf.squeeze(predictions, 0)

      predictions = predictions / temperature
      predicted_id = tf.random.categorical(predictions, num_samples=1)[-1,0].numpy()

      input_eval = tf.expand_dims([predicted_id], 0)

      text_generated.append(idx2char[predicted_id])

  return (start_string + ''.join(text_generated))
with open('jestsyst.txt', 'w') as f:
  f.write(generate_text(model, start_string=u"The"))
#FROM MARKOVIFY DEMO
novel = ''

with open("jestsyst.txt") as f:
    text = f.read()

text_model = markovify.Text(text)

for i in range(20000):
  novel += str(text_model.make_sentence()) + " "

  s = random.randint(0,100)
  if (s < 25):              
    novel += "<br>"
  if (s > 90):
    novel += "<br><br>"
  if (s == 75):
    novel += '<p style="page-break-before: always;"><h3>' + "Chapter " + f"{i + 1}" + ": " + str(text_model.make_short_sentence(200)) + '</h3></p><br><br>'
novelfin ='<h1>The Jest of the System</h1> <p> <h2>a novel by <NAME></h2> <p>'
novelfin += '<p style="page-break-before: always;"><h3>' + "Chapter " + f"{i + 1}" + ": " + str(text_model.make_short_sentence(200)) + '</h3></p><br><br>'
novelfin += novel

html_template = f"""
<html>
  <head>
  <title>The Jest of the System</title>
  <style>
    body {{
      font-family: "Arial";
    }}
  </style>
  </head>
  <body>
  {novelfin}
  </body>
</html>
"""

HTML(string=html_template).write_pdf("systemjest.pdf")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14843503003477712, 'fraction_numerical': 0.017704710717673097, 'mean_word_length': 5.1248789932236205, 'pattern_counts': {'":': 2, '<': 38, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '16738', 'license': 'MIT', 'n_tokens_mistral': 2332, 'n_tokens_neox': 2248, 'n_words': 445, 'path': 'novel/jest_of_the_system.ipynb', 'repo_name': 'cewbewus/creativecode', 'size': '6341'}