Building Your First AI Chatbot: A Step‑by‑Step Tutorial for Beginners
By the end of this guide you will have a functional, generative AI chatbot running on your local machine, exposed through a public URL for testing, and deployed to a low‑cost cloud service. The tutorial walks through every concrete step — from installing the correct Python version to configuring a Hugging Face model, wiring a Flask API, creating a temporary tunnel with ngrok, and pushing the application to Heroku. All figures, timings, and costs are taken from publicly available specifications, vendor documentation, or independent reports, so you can reproduce the exact same setup without guessing.
Prerequisites and System Requirements
Start with a recent release of Python because the Hugging Face Transformers library relies on features introduced in 3.10. According to the official Python download page, Python 3.11.2 was released on October 24, 2022 and includes performance improvements for the interpreter’s startup time, shaving roughly 0.12 seconds off the launch of a typical script compared with 3.10.12.
You will also need a terminal with access to pip, the Python package installer. The PyPI statistics page shows that pip version 23.2.1 was downloaded over 150 million times in the last quarter, indicating wide adoption and reliability. Install the virtualenv tool to create an isolated environment; the virtualenv documentation states that version 20.22.1 (released March 2023) adds support for Python 3.11 out of the box.
Finally, a free tier account on Heroku and ngrok are required for deployment and public testing. Heroku’s platform specifications note that a hobby dyno provides 512 MB of RAM and a shared‑CPU environment, sufficient for a small‑scale transformer model. Ngrok’s free tier documentation allows up to four simultaneous tunnels, each limited to 8 hours per session, which is adequate for iterative development.
Choosing the Chatbot Architecture
For a beginner project, a generative transformer model offers the best balance of capability and simplicity. Rule‑based bots require extensive intent‑matching tables that grow quickly with domain size, while retrieval‑based systems need a pre‑built corpus and similarity search infrastructure. According to a 2023 survey of 1,200 AI practitioners published by O’Reilly, 68 % preferred generative models for prototyping because they eliminate the need for handcrafted dialogue flows.
We selected the gpt2 model from Hugging Face because it is openly licensed, well documented, and modest in size. The model card on the Hugging Face hub reports that gpt2 contains 124 million parameters and occupies approximately 500 MB of disk storage when serialized in PyTorch format. This size enables the model to be loaded on a typical laptop CPU without exceeding memory limits.
Although larger models such as GPT‑Neo‑2.7B produce more fluent responses, they demand >4 GB of VRAM and incur higher inference latency. According to an independent benchmark performed by Lambda Labs and published on their blog in July 2023, running GPT‑Neo‑2.7B on a CPU results in an average response time of 23 seconds per query, whereas gpt2 averages 1.9 seconds under the same conditions. For a learning exercise, the speed‑quality trade‑off of gpt2 makes it the obvious pick.
Setting Up the Development Environment
Open a terminal and create a dedicated folder for the project, for example mkdir ai-chatbot && cd ai-chatbot. Then initialize a virtual environment with the command python3 -m venv venv. The virtualenv documentation specifies that this command creates a directory named venv containing a separate Python interpreter and pip instance, isolating dependencies from the system Python.
Activate the environment: on macOS/Linux run source venv/bin/activate, on Windows run venv\Scripts\activate. Once activated, your shell prompt will prefixed with (venv). Upgrade pip to the latest version to avoid compatibility issues; the PyPI changelog notes that pip 23.3.1, released August 2023, resolved several wheels‑building errors on Apple silicon.
Install the core libraries with exact versions that have been verified to work together. According to the Transformers release notes, version 4.30.0 (published September 2023) adds support for the newer PyTorch 2.0 execution engine. Run:
pip install torch==2.0.1 transformers==4.30.0 flask==2.3.2
The PyPI pages for these packages confirm the respective releases: torch 2.0.1 (March 2023), transformers 4.30.0 (September 2023), and Flask 2.3.2 (June 2023). Installing these specific versions prevents the “module not found” errors that often arise when mixing bleeding‑edge builds.
Finally, install ngrok as a standalone binary. Download the stable release for your operating system from the ngrok website; the version 3.0.0 binary (released January 2023) is approximately 22 MB in size. After extracting, place the executable in your project folder or add its location to your PATH so you can invoke ngrok directly from the terminal.
Building the Chatbot Logic with Flask and Hugging Face Transformers
Create a file named app.py in the project root. Begin by importing the necessary modules:
from flask import Flask, request, jsonify from transformers import AutoModelForCausalLM, AutoTokenizer import torch
According to the Flask documentation, version 2.3.2 introduces built‑in support for asynchronous view functions, although we will use the synchronous style for simplicity. The Transformers library’s AutoTokenizer and AutoModelForCausalLM classes automatically select the correct tokenizer and model architecture based on the model name string.
Load the model and tokenizer once at startup to avoid re‑loading on each request, which would add unnecessary latency. Insert the following lines after the imports:
model_name = "gpt2" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForCausalLM.from_pretrained(model_name) model.eval()
The model card specifies that gpt2 uses the GPT‑2 tokenizer with a vocabulary of 50,257 tokens. Loading the model on a CPU‑only machine typically consumes about 480 MB of RAM, as reported in the Hugging Face model card’s “Resources” section.
Define a route that accepts a POST request containing a JSON payload with a prompt field, generates a response, and returns it as JSON:
@app.route('/chat', methods=['POST'])
def chat():
data = request.get_json()
prompt = data.get('prompt', '')
input_ids = tokenizer.encode(prompt, return_tensors='pt')
# Generate up to 50 new tokens, using nucleus sampling with p=0.9
output_ids = model.generate(
input_ids,
max_length=input_ids.shape[1] + 50,
do_sample=True,
top_p=0.9,
temperature=0.7,
pad_token_id=tokenizer.eos_token_id
)
response = tokenizer.decode(output_ids[0], skip_special_tokens=True)
return jsonify({'response': response})
The generation parameters follow the recommendations in the Hugging Face “How to generate text” guide, which suggests a temperature of 0.7 and top‑p of 0.9 for balanced creativity and coherence. According to the guide, these settings reduce the likelihood of repetitive loops while preserving meaningful continuations.
Initialize the Flask app and run it on the default port 5000:
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
According to the Flask release notes, binding to With the Flask app ready, start it in one terminal window: You should see output similar to: According to the Flask development server documentation, the built‑in server is single‑threaded and not suitable for production, but it is adequate for iterative testing. In a second terminal, launch ngrok to create a public HTTPS tunnel to port 5000: Ngrok will display a forwarding address, for example Test the endpoint using The server will return a JSON object containing a generated reply. According to a latency study posted on the Hugging Face forum in August 2023, the average round‑trip time for a request sent from a laptop in New York to an ngrok tunnel hosted in the same region is approximately 320 ms, of which ~210 ms is network overhead and ~110 ms is model inference on a CPU. If the responses appear overly repetitive or off‑topic, adjust the generation parameters: lower To move beyond local testing, push the application to Heroku. First, create a free Heroku account if you do not already have one. Then install the Heroku CLI; the CLI changelog notes that version 7.60.0 (released June 2023) added support for Python 3.11 buildpacks out of the box. Initialize a Git The tools, tutorials, and trends that actually pay — no hype.0.0.0.0
Testing the Chatbot Locally with ngrok
python app.py
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
ngrok http 5000
https://a1b2c3d4.ngrok.io. The ngrok documentation states that free‑tier tunnels are secured with TLS 1.2 and automatically renew every 8 hours, after which you must restart the command.curl or any HTTP client. Example request:curl -X POST https://a1b2c3d4.ngrok.io/chat \
-H "Content-Type: application/json" \
-d '{"prompt":"Hello, how are you?"}'
temperature to 0.5 for more deterministic output, or increase top_p to 0.95 for greater diversity. These knobs are documented in the Transformers generation strategy guide, which cites empirical results from the GPT‑2 paper showing that temperature controls the sharpness of the softmax distribution.Deploying to Heroku and Ongoing Costs
Get the AI Edge, Weekly



