Ai Coding Tools

How to Deploy an AI Model in 2 Hours Using Python and TensorFlow

By BTW Team5 min read

Comparing tools? See the AI tools tested on the show.

In short

A step-by-step guide to help developers deploy their first AI model within just two hours using Python and TensorFlow.

Deploying an AI model can often feel like a daunting task. Many builders, especially solo founders and indie hackers, struggle with the complexity of setting up an environment, creating APIs, and ensuring everything runs smoothly. But what if I told you that you can deploy an AI model in just two hours using Python and TensorFlow? In this guide, I’ll walk you through the process step-by-step, sharing the tools we use and the limitations we've encountered along the way.

Prerequisites

Before we dive in, here are a few things you'll need:

  1. Basic Knowledge of Python: Familiarity with Python syntax and libraries is essential.
  2. TensorFlow Installed: Make sure you have TensorFlow installed. You can do this by running pip install tensorflow in your terminal.
  3. Flask Installed: We’ll use Flask to create a simple web server. Install it with pip install flask.
  4. A Trained Model: You should have a pre-trained TensorFlow model ready to deploy.

Step 1: Set Up Your Environment (30 Minutes)

First, let's make sure everything is set up correctly. You can do this on your local machine or use a cloud service like Google Colab or AWS.

  1. Create a New Directory: Organize your project files.

    mkdir ai_model_deployment
    cd ai_model_deployment
    
  2. Create a Virtual Environment: This helps manage dependencies.

    python -m venv venv
    source venv/bin/activate  # On Windows use venv\Scripts\activate
    
  3. Install Required Packages:

    pip install tensorflow flask
    
  4. Place Your Model: Ensure your trained model is in the project directory.

Step 2: Build the API with Flask (30 Minutes)

Now that your environment is ready, let’s create a simple Flask application to serve your model.

  1. Create a app.py file:

    from flask import Flask, request, jsonify
    import tensorflow as tf
    
    app = Flask(__name__)
    
    # Load your model
    model = tf.keras.models.load_model('your_model.h5')
    
    @app.route('/predict', methods=['POST'])
    def predict():
        data = request.get_json(force=True)
        prediction = model.predict(data['inputs'])
        return jsonify(prediction.tolist())
    
    if __name__ == '__main__':
        app.run(debug=True)
    
  2. Run Your Flask Application:

    python app.py
    
  3. Testing the API: Use a tool like Postman or cURL to send a POST request to http://127.0.0.1:5000/predict with a JSON body:

    {
        "inputs": [[1, 2, 3, 4]]
    }
    

Step 3: Containerize with Docker (30 Minutes)

If you want to make your deployment more robust, consider using Docker. This makes your application portable and easy to deploy anywhere.

  1. Create a Dockerfile:

    FROM python:3.9
    
    WORKDIR /app
    
    COPY . .
    
    RUN pip install tensorflow flask
    
    CMD ["python", "app.py"]
    
  2. Build the Docker Image:

    docker build -t ai_model_deployment .
    
  3. Run the Docker Container:

    docker run -p 5000:5000 ai_model_deployment
    

Step 4: Deploy to a Cloud Service (30 Minutes)

To make your model accessible online, you can deploy it to a cloud service like Heroku or AWS.

Heroku Deployment Steps:

  1. Create a requirements.txt:

    tensorflow
    flask
    
  2. Install Heroku CLI and log in:

    heroku login
    
  3. Create a New Heroku App:

    heroku create your-app-name
    
  4. Deploy Your Code:

    git init
    git add .
    git commit -m "Initial commit"
    git push heroku master
    
  5. Open Your App:

    heroku open
    

Troubleshooting

  • Common Issues: If your model fails to load, ensure the path is correct and that the model file is present.
  • Flask Not Responding: Check if the server is running and accessible on the specified port.

What's Next?

Once your model is deployed, consider these next steps:

  1. Monitor Performance: Use tools like Prometheus to monitor the API performance.
  2. Scale Up: If you expect high traffic, consider using load balancers and multiple instances.
  3. Explore Advanced Features: Implement user authentication or a frontend interface for better usability.

Conclusion

Deploying an AI model doesn’t have to be a complex, time-consuming process. With Python and TensorFlow, you can get up and running in about two hours. Start with the steps outlined above, and feel free to iterate and improve your deployment as you go.

If you're looking for tools to help with this process, here’s a quick summary of what we actually use:

| Tool | Pricing | Best For | Limitations | Our Take | |-------------------|-----------------------|------------------------------|-------------------------------------|------------------------------------------------| | TensorFlow | Free | Model training & deployment | Can be resource-intensive | We use this for all our AI models. | | Flask | Free | Creating APIs | Limited scalability out of the box | Great for quick prototypes and small projects. | | Docker | Free | Containerization | Learning curve for beginners | Essential for ensuring consistent environments. | | Heroku | Free tier + $7/mo | Simple app deployment | Limited free tier resources | Good for small apps, but costs can add up. | | AWS EC2 | $0-20/mo for small instances | Scalable deployments | Complexity in setup | Powerful but requires more setup time. | | Postman | Free tier + $12/mo pro | API testing | Free tier limits API calls | Essential for testing APIs effectively. |

For a more comprehensive deployment strategy, consider keeping an eye on our weekly podcast, Built This Week, where we share tools and lessons learned from real projects.

Watch real AI builds every week

Built This Week is a weekly podcast where Jordan Metzner and Sam Nadler build something real with AI every episode, test new AI tools and talk with founders who build with AI.

See the tools

Watch real AI builds every week

Built This Week is a weekly podcast where Jordan Metzner and Sam Nadler build something real with AI every episode, test new AI tools and talk with founders who build with AI.

See the tools
Ai Coding Tools

3 AI Coding Tools That Are Overrated (and Why You Should Avoid Them)

3 AI Coding Tools That Are Overrated (and Why You Should Avoid Them) In the everevolving landscape of AI coding tools, it's easy to get swept up in the hype. As indie hackers and s

Nov 30, 20263 min read
Ai Coding Tools

5 Ways AI Coding Tools Can Accelerate Your MVP Development in 2026

5 Ways AI Coding Tools Can Accelerate Your MVP Development in 2026 In 2026, the landscape for indie hackers and solo founders has evolved significantly, especially with the advent

Nov 26, 20264 min read
Ai Coding Tools

5 AI Coding Tools That Make Legacy Code Feel Brand New in 2026

5 AI Coding Tools That Make Legacy Code Feel Brand New in 2026 If you’re a developer stuck maintaining legacy code, you know the frustration. It’s like navigating a maze blindfolde

Nov 23, 20264 min read
Ai Coding Tools

10 AI Coding Tools That Will Save You Hours of Debugging in 2026

10 AI Coding Tools That Will Save You Hours of Debugging in 2026 Debugging can feel like a neverending cycle of frustration. Whether you're a solo founder or an indie hacker, spend

Nov 18, 20265 min read
Ai Coding Tools

10 Common Mistakes When Using AI Coding Tools for MVP Development

10 Common Mistakes When Using AI Coding Tools for MVP Development As a founder in 2026, you’re likely aware that AI coding tools can significantly speed up the MVP development proc

Nov 16, 20264 min read
Ai Coding Tools

5 AI Coding Myths Every Beginner Developer Should Ditch in 2026

5 AI Coding Myths Every Beginner Developer Should Ditch in 2026 As a beginner developer, you’ve probably come across a ton of buzz surrounding AI coding tools. But with all the hyp

Nov 12, 20263 min read