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.
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.
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.
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.
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.
main branch clean and production-ready.requirements.txt file is mandatory for the cloud to understand which external libraries your AI app depends on.