25:00
Focus
Sign in to save your learning paths. Guest paths may be lost if you clear your browser data.Sign in
Lesson 11

Deploying Your App to the Cloud

~18 min125 XP

Introduction

Transitioning from a local development script to a live, functional web application is the most satisfying milestone for any AI developer. In this lesson, we will bridge the gap between your machine and the world by leveraging version control and cloud-hosted deployment.

The Foundation: Version Control with Git

Before you can deploy your application, your code must live in a repository. Version Control is the practice of tracking and managing changes to software code. We use Git to create a snapshot of your project, allowing you to track history, experiment safely, and link your code to cloud-hosting platforms like GitHub.

Think of Git as a "save system" for your project. Instead of just saving one folder, you commit specific changes. This ensures that when you push your code to GitHub, your cloud environment sees the exact sequence of versions you want to run. When you upload your files, ensure you include a requirements.txt file. This text file lists every external library your project needs (such as streamlit, openai, or pandas) using their version numbers. Without this, the cloud server will not know how to "rebuild" your environment to execute your code successfully.

Exercise 1Multiple Choice
Why is a 'requirements.txt' file essential for cloud deployment?

Connecting to Streamlit Cloud

Once your code resides in a public or private GitHub repository, Streamlit Cloud acts as the bridge between that storage and a live, accessible URL. Streamlit Cloud is an integrated hosting platform that watches your GitHub repository for new "commits." Every time you push an update to your main branch, the platform automatically detects the change, rebuilds your application, and pushes it live.

To get started, you must connect your GitHub account within the Streamlit dashboard. After connecting, you simply select your repository and the specific script (the .py file) you want to run. The platform handles the underlying Containerizationโ€”the process of bundling your application and its dependenciesโ€”so you don't have to manage complex virtual servers or manually configure Infrastructure.

Managing Secrets in the Cloud

A common pitfall for new developers is hardcoding sensitive information directly into their source code. Never write your OpenAI API key or database credentials inside your Python file. If you upload your key to a public GitHub repository, malicious actors can find and use it, leading to unexpected costs.

Instead, use Environment Variables. Streamlit Cloud provides a dedicated "Secrets" management UI. By pasting your keys into the [secrets] section of your app's dashboard, you allow your app to access those strings during runtime without ever storing them in the repository files. Within your Python code, you can access these values securely using the st.secrets object.

Exercise 2True or False
Storing your API keys in a text file labeled 'secrets.txt' inside your public GitHub repository is a safe practice.

Troubleshooting and Deployment Logs

When your deployment attempt results in an error, do not panic. Deployment logs are your primary source of truth. If your app fails to build, Streamlit provides a real-time console that displays the output of your pip install commands and your Python script's startup sequence.

Common issues often stem from two places. First, a typo in the requirements.txt file (e.g., requesting a version of a library that does not exist). Second, incorrect file path references. When running locally, you might refer to a file as data/my_file.csv. If that file is not pushed to the repository, the cloud environment will throw a FileNotFoundError. Always verify that your repository reflects the exact structure your code expects.

Exercise 3Fill in the Blank
___ reflect the real-time installation and execution errors occurring during the app's startup process in the cloud.

Key Takeaways

  • Use Git to commit your code changes frequently and keep your main branch clean and production-ready.
  • A requirements.txt file is mandatory for the cloud to understand which external libraries your AI app depends on.
  • Never hardcode sensitive API keys; always use the host's dedicated Secrets manager to keep your credentials private.
  • When an app fails to deploy, the build Logs are your best tool for identifying missing dependencies or path errors.
Finding tutorial videos...
Go deeper
  • What happens if I forget a library in requirements.txt?๐Ÿ”’
  • Can I use platforms other than GitHub for deployment?๐Ÿ”’
  • How do I update my app after the initial deployment?๐Ÿ”’
  • Are private GitHub repositories free to host on Streamlit?๐Ÿ”’
  • How does Streamlit Cloud handle API keys securely?๐Ÿ”’