DEV Community

Yash Makan
Yash Makan

Posted on

Generate your own favourite TV episode using python

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...
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.
dive in

Introduction

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.
how it works

Procedure

  • Make a python file named utils.py and start writing code. Let's first import the needed dependencies for the project

    imports

    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_model which will load the model and tokenizer. Both model and tokenizer are required to generate results.

    Load model and tokenizer

    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_scripts and 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.

    generate model

    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 max_length and input_ids which 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.py

    In main.py file, we are importing the load_model and generating functions from utils.py and simply calling the functions to get the result.

Results

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.

Results

Conclusion

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!

bye bye

References

GitHub Repo

Twitter

Discussion (0)