Generate your own favourite TV episode using python
3 min read
I started watching "Friends" during lunch. I was not able to find any new episodes that I don't remember as I have already watched all the episodes once. I am sure that this situation must have happened with you guys too when you started scrolling like what to watch. Don't worry at all... In today's blog we are going to develop our own screenwriter bot which will write scripts for any TV show you like. So without any further due let's dive in.
We are going to use one of the most popular python libraries for machine learning that is transformers. Here we will provide a beginning of the episode and the program will complete the program with the maximum length provided below is an image representing the same procedure.
Make a python file named
utils.pyand start writing code. Let's first import the needed dependencies for the project
here we are importing transformers to load the model and the tokenizer from hugging face and also we are importing os to check if the model and tokenizer are already installed in the directory or not.
Then make a function named
load_modelwhich will load the model and tokenizer. Both model and tokenizer are required to generate results.
Here the program will check if the tokenizer and model are installed in the system or not. When you run the program for the first time then it will download the model and tokenizer from humming face i.e
cpierse/gpt2_film_scriptsand store them locally in the system so that next time you don't have to install it again. After that, it's simply returning the tokenizer and model.
After this, we will be making a function for generating the script.
In this function, we trained the model and now using
model.generate()we are getting the script. As you can see the model.generate() function takes
input_idswhich is nothing but the context of the initial script provided.
After this, create another python file and name it as main.py. Here add the following code.
main.pyfile, we are importing the load_model and generating functions from
utils.pyand simply calling the functions to get the result.
The results below are generated in 93 seconds on my pc with 500 as max length. The left side is the input initial script and the right side is the generated script.
Now obviously the results are not that amazing but still, it's a great program which can write screenplays like humans. This project is just made for a fun time and I thought will be worth sharing with the community.
Also if you are angry with me to adding the images instead of the code then don't worry. Below, you will find the github repo in the references. Also I would love to thank you guys for reading this blog. will surely return again with other exciting content. Till then b-bye!