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…


This content originally appeared on DEV Community and was authored by Yash Makan

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


This content originally appeared on DEV Community and was authored by Yash Makan


Print Share Comment Cite Upload Translate Updates
APA

Yash Makan | Sciencx (2021-11-14T17:47:07+00:00) Generate your own favourite TV episode using python. Retrieved from https://www.scien.cx/2021/11/14/generate-your-own-favourite-tv-episode-using-python/

MLA
" » Generate your own favourite TV episode using python." Yash Makan | Sciencx - Sunday November 14, 2021, https://www.scien.cx/2021/11/14/generate-your-own-favourite-tv-episode-using-python/
HARVARD
Yash Makan | Sciencx Sunday November 14, 2021 » Generate your own favourite TV episode using python., viewed ,<https://www.scien.cx/2021/11/14/generate-your-own-favourite-tv-episode-using-python/>
VANCOUVER
Yash Makan | Sciencx - » Generate your own favourite TV episode using python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/14/generate-your-own-favourite-tv-episode-using-python/
CHICAGO
" » Generate your own favourite TV episode using python." Yash Makan | Sciencx - Accessed . https://www.scien.cx/2021/11/14/generate-your-own-favourite-tv-episode-using-python/
IEEE
" » Generate your own favourite TV episode using python." Yash Makan | Sciencx [Online]. Available: https://www.scien.cx/2021/11/14/generate-your-own-favourite-tv-episode-using-python/. [Accessed: ]
rf:citation
» Generate your own favourite TV episode using python | Yash Makan | Sciencx | https://www.scien.cx/2021/11/14/generate-your-own-favourite-tv-episode-using-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.