Python + Flask + ChatGPT + Heroku: A Guide to Deploying a Personalised AI Chatbot in Minutes

Rees Pozzi
4 min readJun 16, 2023

--

The journey of developing and deploying simple web applications has never been easier thanks to powerful tools like ChatGPT, Flask and Heroku. Flask: a lightweight and intuitive web framework that simplifies the development process, allowing developers to focus on the core functionality of their chatbot. With ChatGPT, the integration of AI becomes seamless, enabling dynamic and engaging conversations with users. Finally, Heroku takes the pain out of deployment by providing a user-friendly platform that handles infrastructure management and scalability.

The world of AI is moving fast, the combination of Flask, ChatGPT, and Heroku makes it easier than ever to create and deploy web applications with AI integrations, so you can focus on the what rather than the how.

The Spiteful Programmer

I watched an interesting talk about prompt injection and how easy it is to take a really powerful generic tool and tailor it to certain cases. Based on that, I had some fun going back and forth with ChatGPT until I’d created a prompt to initially set the ‘personality’ of ChatGPT.

In the next few paragraphs I’ll explain how you can go from a similar prompt idea, all the way through to a web app, similar to the one I created here in a matter of minutes. All of the code was generated by ChatGPT.

The only difference in my code to what ChatGPT will generate for you is the prompt injection, where I preset ChatGPT with a personality. You can use this as a base and alter the injection, or follow along and figure it out from scratch!

The Spiteful Programmer Code

The power of AI!

So, how to create the webpage? Well, let’s ask the robots!

You will need to set up an account at https://openai.com/ to gain access to the free text based version of ChatGPT to receive these results, and for communicating to the API, you need an OpenAI API key, you need to sign up for an account on the OpenAI website and follow their instructions to generate an API key.

Nice — right?

After copying and pasting the code and following the instructions, within a few minutes you’re ready for a local deployment of your application!

Don’t forget to inject a personalisation prompt into the request being sent to ChatGPT!

At this point, my website looked rubbish — so again, I asked the robot!

Nice, now we’re really getting somewhere and things are starting to take shape. You may be happy at this point just running your Spiteful Programmer on your local machine, but what if your friends want a go?

Deployment with Heroku

This is a tool I only learned about (found out about through ChatGPT) today, but it’s really cool. I wanted to deploy this application so more people could see it if they wanted.

The rest is amazingly easy!

Firstly, you must sign up for a Heroku account, providing your billing details. Details on pricing.
Secondly, download the Heroku CLI in whichever way you see fit. Then it’s onto the fun stuff.

Point 3 is super important here, ensure you keep capitalisation on Procfile too!

Ensure that you have created a Github repository containing the code that was generated in previous steps, and you have run the relevant commands to map your remote repository in your local host. As ever, you should never commit API Keys to a public repository. Please take care here.

The commands I ran in order to get this to work are as follows:

heroku login
git add .
git commit -m "New website design"
git push
heroku create
git push heroku master
heroku ps:scale web=1
heroku open

And that really is it.

After running those commands, my Python web app opened up in a browser, and I was offered the exact same functionality as I had locally, all in a matter of minutes! I received a rude response from the spiteful programmer running online this time instead of my own machine 😀

Obviously, this is a trivial approach to get you up and running. OpenAI API requests are billed, same with Heroku, so please be careful and consider some extra protection layers if you deploy this to the internet where anyone has unlimited access.

--

--