Generate your own favourite TV episode using python
Tags: python, machinelearning, transformers, tutorial
Published: 2021-11-14 17:47:07

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zxjpbibfnmqi300bgm0j.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m1djms656xkc6jpvw9ui.png)

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4e3cljs3luoo1fzm3pzu.png)

### Procedure

- Make a python file named `utils.py` and start writing code. Let's first import the needed dependencies for the project
    
    ![imports](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dxu7t3guvo3y612vcnd9.png)
    
    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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xnf6ol5ec6vbmhm5dno7.png)
    
    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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6edb7uefkgph3g3pp5zj.png)
    
    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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hffz5awevr2njlv6iify.png)
    
    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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kl52vh6m5qh9otqy11ed.png)

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zmplklrqb9kxplf4byk3.gif)

### References

[GitHub Repo](https://github.com/YashMakan/blog-1)

[Twitter](https://twitter.com/Yash_Makan)