AI Book Engine Automated Publishing Page
It would be very cool if the books we create automatically in a batch process are published to the web. This article will show you how that can be done. I use a couple of things:
The process begins with the automated creation of books by the AI Book Engine. Once these books are generated, they become instantly accessible on a web platform that supports various interactive functions such as Voting, Reading, Publishing, and Peer Reviewing. This system is flexible and can accommodate additional functionalities, enhancing its integration and user engagement.
The unique aspect of this system is its capability to function as a real or multiple publishers, streamlining the book creation process to remarkable simplicity. Users need only to provide a text file listing the desired book titles, and the AI Book Engine handles the rest, automating the entire production cycle.
I'll now outline the method employed by the AI Book Engine. The process starts with the engine reading the file from the disk. It then identifies and processes the lines containing the book titles, transferring them to a Blob storage.
It's important to note that the demonstration here will conclude before the actual content generation for these books. This is intended for example purposes only. You are encouraged to develop the content based on the initial framework provided by this code, tailoring it to suit your specific requirements and creative vision.
// Only the word "batch" needs to be given.
// Subject WILL be overwritten.
// It will read a text file called FileBatch.txt line by line.
// "Agile_Coach" 1 2 1 false (line format in the file)
// param 1 = subject (unerscores needed)
// 2 amount of book, always put on 1
// 3 Amount of chapters, can be whatever you want, do under 23
// 4 amount of same bboks on the topic (content will be different)
// 5 images
else if (args[0] != null && args[0].Equals("batch"))
{
List<string> getVerses = new List<string>();
string[][] myArray = null;
TextToSpeechClient client = IniGoogleSound();
string textToSynthesize = "";
string appPath = AppDomain.CurrentDomain.BaseDirectory;
string filePath = "FileBatch.txt"; // Replace with your file path
string theFile = appPath + filePath;
List<string> lines = new List<string>();
try
{
using (StreamReader sr = new StreamReader(filePath))
{
string line;
while ((line = sr.ReadLine()) != null)
{
lines.Add(line);
}
myArray = new string[lines.Count][];
}
string _subject = "";
int _books = 0;
int _chapters = 0;
int _examples = 0;
bool _images = false;
int w = 0;
foreach (var item in lines)
{
//string[] parameters = item.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string[] parameters = item.Split("\" \"");
if (parameters.Length == 5)
{
_subject = parameters[0].Replace("_", " ");
_books = int.Parse(parameters[1]);
_chapters = int.Parse(parameters[2]);
_examples = int.Parse(parameters[3]);
_images = bool.Parse(parameters[4].Replace("\"", ""));
}
if (parameters.Length == 7)
{
// HERE WE CHECK PARAMS AND WRITE TO BLOB
if (parameters[5].Equals("Publish"))
{
_subject = parameters[0].Replace("_", " ");
_books = int.Parse(parameters[1]);
_chapters = int.Parse(parameters[2]);
_examples = int.Parse(parameters[3]);
_images = bool.Parse(parameters[4].Replace("\"", ""));
string filenamefromcommandline = parameters[6] + ".txt";
string filename = DateTime.Now.ToString("yyyyMMddHHmmssfff");
string chapterTitlesPath = RemoveNonLetters(filenamefromcommandline.Replace("\"", ""))
+ filename + ".txt";
string doBlob = await GlobalMethods.writeFileToBlob(filename
, filenamefromcommandline.Replace("\"", ""));
}
else
{
Console.Write("Wrong command.");
Environment.Exit(0);
}
}
string _subjectt = _subject;
// Make the books
GetSequenceFiles.Add(_subjectt.ToLower());
}
// EARLY EXIT OF THE CODE!!!!
Console.Write("files written");
Environment.Exit(0);
textToSynthesize = "It's time to create the content files.";
GlobalMethods.DoSpeak(client, textToSynthesize);
List<string> listOfFilesForBookConversion = new List<string>();
listOfFilesForBookConversion = await MakeBookWithVariableStrings(
GlobalMethods.DeleteFirstNumber(_subject), _books, _chapters,
_examples, _AITopicOrUser, GetSequenceFiles);
textToSynthesize = "Before I continue creating the books, please check the files.";
GlobalMethods.DoSpeak(client, textToSynthesize);
ConsoleKeyInfo keyInfo;
keyInfo = Console.ReadKey();
string returnMessage = "";
int cList = listOfFilesForBookConversion.Count;
returnMessage = await MakeTheBooks(listOfFilesForBookConversion, "Beginner", false);
// Say goodbye.
if (_speak)
{
if (_VProvider.Equals(1))
{
speechSynthesizer.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Adult, 3, new System.Globalization.CultureInfo("en-US"));
speechSynthesizer.Speak("It's my pleasure to inform you that the books are created.");
speechSynthesizer.Speak("Till next time my dear friend..");
}
else
{
textToSynthesize = "It's my pleasure to inform you that the books are created.";
GlobalMethods.DoSpeak(client, textToSynthesize);
textToSynthesize = "Till next time my dear friend..";
GlobalMethods.DoSpeak(client, textToSynthesize);
}
}
Console.WriteLine("Ready writing books");
GetSequenceFiles.Clear();
}
catch (Exception ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
textToSynthesize = "File done, lines:" + lines.Count.ToString();
GlobalMethods.DoSpeak(client, textToSynthesize);
Console.WriteLine("File done, lines:" + lines.Count.ToString());
}
The above code reads from a file on disk with a certain format. Let's have a look at that file:
"Leading Innovation and Change" "1" "5" "1" "false" "Publish" "Leading Innovation and Change"
"Change and Innovation" "1" "5" "1" "false" "Publish" "Change and Innovation"
"Creativity and Leadership" "1" "5" "1" "false" "Publish" "Creativity and Leadership"
"Agile Transformations""1" "5" "1" "false" "Publish" "Agile Transformations"
"Management Styles for Agile Transformations" "1" "5" "1" "false" "Publish" "Management Styles for Agile Transformations"
In sequence what the parameters are:
The file is picked up and the parameters are put in an array to be worked on accordingly. See the starting code of this article.
Okay, because we write to Blob, I'll give you the Blob writing code:
public static async Task<string> writeFileToBlob(string writeToBlobAIBookEngine, string fileName)
{
try
{
// Initialise client in a different place if you like
string connS = "<connectionstring>";
CloudStorageAccount account = CloudStorageAccount.Parse(connS);
var blobClient = account.CreateCloudBlobClient();
// Make sure container is there
var blobContainer = blobClient.GetContainerReference("<containername>");
blobContainer.CreateIfNotExistsAsync();
WebClient wc = new WebClient();
using (Stream fs = wc.OpenWrite("https://<storage>.blob.core.windows.net/<container>/" + fileName))
{
TextWriter tw = new StreamWriter(fs);
CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(
fileName);
blockBlob.UploadTextAsync(writeToBlobAIBookEngine);
//tw.Flush();
}
return "Success";
}
catch (Exception ex)
{
return ex.Message;
}
}
The above code will write the files to a blob storage container. The content of that container looks something like this:
Certainly, the objective is to ensure that these files are prominently displayed within a web page of an MVC application hosted on Azure. This platform is designed to facilitate various interactive features, including Peer Review, Reading, Voting, and Publishing. The layout and functionality of the page are thoughtfully crafted to enhance user experience and engagement. The interface of this web page can be described as follows:
It's important to clarify that this system is already operational in my environment. To facilitate your understanding and implementation, I will provide the controller and view code of the MVC application responsible for this functionality. The application's core operation involves reading items from a blob container, compiling them into a List, and subsequently displaying this list on the webpage.
Please note that the controller code is configured for a container with anonymous access. You will need to customize it by inserting your specific container names and parameters.
public async Task<ActionResult> Index()
{
List<string> blobs = new List<string>();
List<string> filelist = new List<string>();
using (HttpClient client = new HttpClient())
{
// Set the base URI for the Azure Blob Storage API
client.BaseAddress = new Uri($"https://{AccountName}.blob.core.windows.net/{ContainerName}/");
// Make a GET request to list blobs in the container
HttpResponseMessage response = await client.GetAsync("?restype=container&comp=list");
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
// Parse the JSON response to extract blob names
var blobNames = ParseBlobNamesFromResponse(responseBody);
List<string> blobNamess = GetBlobNamesFromXml(responseBody);
foreach (string blobName in blobNamess)
{
filelist.Add(blobName);
}
}
else
{
ViewBag.Error = $"Failed to list blobs. Status code: {response.StatusCode}";
}
}
return View(filelist);
}
The View code:
领英推荐
// View code of Index
@model List<string>
<style>
td {
padding: 10px; /* Adjust the value to control the amount of space */
}
</style>
<h2>Files in Azure Blob Storage Container</h2>
<table>
<thead>
<tr>
<th>File Name</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var fileName in Model)
{
<tr>
<td>@fileName</td>
<td>
@Html.ActionLink("Read", "Readbook", "Home", new { filename = fileName }, new { @class = "btn btn-primary" })
<button type="button" class="btn btn-success">Publish</button>
@Html.ActionLink("Delete", "Deletebook", "Home", new { filename = fileName }, new { @class = "btn alert-danger" })
@*<button type="button" class="btn alert-info">Peer Review</button>
<button type="button" class="btn btn-warning">Vote</button>*@
</td>
</tr>
}
</tbody>
</table>
The Controller code with the method "ReadBook" getting the file from blob and make it available in a view:
// Getting the file from the view
[HttpGet]
public ActionResult ReadBook(string filename)
{
// Implement your logic to read the book here
// You can use the "filename" parameter to determine which book to read
// Return an appropriate ActionResult, such as a FileResult, JSON result, or a ViewResult
ViewBag.Book = readFileFromBlob(filename);
ViewBag.BookName = filename;
return View();
}
// Reading the blob
public string readFileFromBlob(string fileName)
{
try
{
// Initialise client in a different place if you like
string connS = "<connection>";
CloudStorageAccount account = CloudStorageAccount.Parse(connS);
var blobClient = account.CreateCloudBlobClient();
var blobContainer = blobClient.GetContainerReference("aibookengine");
blobContainer.CreateIfNotExistsAsync();
CloudBlockBlob blob = blobContainer.GetBlockBlobReference($"{fileName}");
string contents = blob.DownloadTextAsync().Result;
return contents;
}
catch (Exception ex)
{
return "0";
}
}
// In the View "ReadBook"
@{
ViewBag.Title = "ReadBook";
}
<h2>@ViewBag.BookName</h2>
@Html.Raw(ViewBag.Book)
The code for deleting the blob file:
// getting the file from the view
[HttpGet]
public ActionResult DeleteBook(string filename)
{
// Implement your logic to read the book here
// You can use the "filename" parameter to determine which book to read
// Return an appropriate ActionResult, such as a FileResult, JSON result, or a ViewResult
ViewBag.Book = deleteFileFromBlob(filename);
ViewBag.BookName = filename;
return View();
}
// Deleting the blob file
public string deleteFileFromBlob(string fileName)
{
try
{
// Initialise client in a different place if you like
string connS = "<conncetion>";
CloudStorageAccount account = CloudStorageAccount.Parse(connS);
var blobClient = account.CreateCloudBlobClient();
var blobContainer = blobClient.GetContainerReference("aibookengine");
blobContainer.CreateIfNotExistsAsync();
CloudBlockBlob blob = blobContainer.GetBlockBlobReference($"{fileName}");
if (blob.DeleteIfExistsAsync().Result)
{
return "File deleted successfully.";
}
else
{
return "File not found or failed to delete.";
}
return "";
}
catch (Exception ex)
{
return "0";
}
}
// The view after deleting the file
@{
ViewBag.Title = "ReadBook";
}
<h2>@ViewBag.BookName</h2>
@Html.Raw(ViewBag.Book)
This code is not just a technological advancement; it represents a paradigm shift in how we create, publish, and ensure the quality of books. It's akin to the Book Creation Global Nervous System I previously mentioned, which facilitates the creation and sharing of books at an unprecedented speed.
Envision the MVC application as a digital library, overflowing with a diverse array of HTML-based books. These books cover various subjects tailored for trimesters for students, and are accessible globally. This eliminates the challenges of exorbitantly priced, hard-to-find traditional textbooks.
The transformation is comparable to the evolution of the telephone industry, which had to adapt drastically with the advent of internet-based social communication platforms, disrupting the old model of expensive national and international calls.
The AI Book Engine is poised to revolutionize the book industry in a similar manner. This change, disruptive yet necessary, aligns with advancements that should have occurred long ago. AI is not only capable of producing books with exceptional content for students, professionals, children, and many more, but it also heralds the dawn of a new distribution system. This system resembles a global Nervous System, wherein books generated by AI are instantly shared at the speed of thought.
Currently, this concept exists in its nascent form, as raw code. However, it holds the potential to be developed and adopted worldwide, enhancing global access to knowledge and learning. The AI Book Engine stands ready to be the cornerstone of this transformative journey.
Here again the full code already given in previous related articles. Just plot the code in this article on the existing code. In the near future I'll update the complete code with all my latest sweeties. Full_Article_Code
Happy coding.