From ff47d0c3a51ff4f5004ebec667b83a11e06a1b61 Mon Sep 17 00:00:00 2001 From: Dayenne Souza Date: Thu, 7 Nov 2024 10:14:25 -0300 Subject: [PATCH] add streamlit cloud approach to deploy --- DEPLOYING.md | 127 +++++++++++++++++++++++++++++++---- app/Home.py | 5 +- app/components/app_loader.py | 11 ++- 3 files changed, 121 insertions(+), 22 deletions(-) diff --git a/DEPLOYING.md b/DEPLOYING.md index 219793a..18d5909 100644 --- a/DEPLOYING.md +++ b/DEPLOYING.md @@ -1,5 +1,89 @@ # Deploying the app +Deploying your app effectively is crucial for making it accessible to users. We present three robust options that cater to different requirements and preferences, whether you're looking for simplicity, scalability, or specific infrastructure. + +- **Using Streamlit Cloud:** Ideal for those who prefer a straightforward and cost-effective deployment solution. Streamlit Cloud is perfect for quickly deploying Streamlit apps with minimal setup and no need for managing infrastructure. + +- **Using Azure:** A powerful option for those who are already integrated into the Microsoft ecosystem or need advanced cloud services. Azure offers robust performance, scalability, and a suite of tools that are beneficial for apps requiring Microsoft-specific integrations or high availability. + +- **Using AWS:** Known for its versatility and comprehensive cloud solutions, AWS is suitable for developers seeking a highly scalable and flexible deployment environment. It is apt for those with diverse app requirements and offers a range of services, from simple hosting to complex machine learning models. + +Each of these options has its own strengths, and your choice will depend on the specific needs and constraints of your project. The following sections provide detailed guides on deploying your app using each of these platforms. + + +# Requirements for the app + +To deploy your app, you will need: + +1. An active OpenAI account ([create here](https://platform.openai.com/login)). +2. An OpenAI API key ([create here](https://platform.openai.com/account/api-keys)). + +# Streamlit Cloud +This guide will walk you through deploying our app that uses [Streamlit](https://streamlit.io/), a Python package for building web apps. You can host your Streamlit apps for free with [Streamlit Community Cloud](https://streamlit.io/cloud). + +## Security: + +Streamlit prioritizes security by hosting data in secure cloud facilities and using encryption for data protection. Permission management follows GitHub rules, and regular vulnerability scans ensure the platform remains secure and up to date. + +[Read more](https://docs.streamlit.io/deploy/streamlit-community-cloud/get-started/trust-and-security) + +## Resource Limits + +While Streamlit Community Cloud is free to use, it has resource limits that apply to all users. Exceeding these limits may result in throttling or app nonfunctionality. As of February 2024, the approximate limits are: + +- **CPU:** 0.078 cores minimum, 2 cores maximum +- **Memory:** 690MB minimum, 2.7GB maximum +- **Storage:** No minimum, 50GB maximum + +For optimal performance, especially with high concurrent usage, a higher allocation of CPU and memory may be beneficial. + +[Read more](https://docs.streamlit.io/deploy/streamlit-community-cloud/manage-your-app#app-resources-and-limits) + + +## Increased Resources for NGOs + +If you're part of an NGO, you might be eligible for increased resources by applying [here](https://info.snowflake.com/streamlit-resource-increase-request.html). Eligible apps often serve educational institutions, are part of open-source projects, or provide societal benefits. For-profit companies are generally not eligible. + +## Optional requirement for Streamlit Cloud + +- A forked GitHub repository: + - Fork the repository to create your own instance with custom modifications. Ensure you keep your repository updated with the main repository to incorporate new code. + - You need to have a [github account](https://github.com) + - Alternatively, you can use the main repository directly, which will automatically receive updates. + +## Deployment Steps + +1. **Streamlit Setup:** + - [Create or sign in to your Streamlit account](https://share.streamlit.io/signup). + - Click on `Create an app` in the top right corner. + - Choose to deploy a public app from GitHub and configure it with the following: + + - **Repository:** `{your_forked_one}` or `[microsoft/intelligence-toolkit](https://github.com/microsoft/intelligence-toolkit)` + - **Branch:** `master` + - **Main file path:** `app/Home.py` + - **App URL (optional):** Customize the URL for accessing your app. + +2. **Advanced Settings:** + - Add the following secrets: + ```plaintext + OPENAI_API_KEY="your-key" + + HIDE_SETTINGS="TRUE" # Hides settings page to prevent changes affecting all users. + + AUTH_ENABLED="TRUE" # Allows access only to authorized users. + + [passwords] + "username" = "user_pwd" + "username2" = "user_pwd2" + ... + ``` + +3. **Deploy:** + - After setting up, click deploy. The deployment process takes a few minutes, and your app will be ready for access. + + +# Azure + #### Recommended configuration: - *Minimum disk space*: 8GB @@ -87,7 +171,7 @@ You can modify the code and deploy the container, or use our default container h `.azurewebsites.net` -## AWS +# AWS Wait for step 1 to be set as complete before starting step 2. The whole process will take up to 20 minutes. @@ -105,26 +189,43 @@ Wait for step 1 to be set as complete before starting step 2. The whole process Once step 2 it's complete, in the output tab, you'll see the deployed URL. -**Note: This code doesn't have auth, so this URL will be open to the internet.** - ## Environment configuration -`MODE: CLOUD` to hide Settings page so users can't change configuration that would affect other users experiences. +To effectively manage user access and configuration settings in your app's deployment environment, here are some key parameters and steps for setting up your environment configuration. This guide covers how to handle these settings in Streamlit Cloud, Azure, and AWS deployments. + +## Environment Configuration + +- **Hiding the Settings Page** + + Use `HIDE_SETTINGS: TRUE` to conceal the Settings page. This prevents users from altering configurations that could impact the experience for other users. + +- **OpenAI API Key** + + Set `OPENAI_API_KEY="your-key"` to ensure secure access to OpenAI services. This key allows interaction with OpenAI's API while keeping it confidential. + +- **Authentication** + + Enable `AUTH_ENABLED: TRUE` to restrict app access to authorized users with credentials defined in a `.secrets.toml` file under the `.streamlit` directory: + + ```toml + [passwords] + user_test = "user123" + ... + ``` -`AUTH_ENABLED: TRUE` if you would like to limit access into the app by user and password defined in a .secrets.toml file inside .streamlit: +### Inserting the `secrets.toml` file into Web App Deployments (Azure or AWS) -``` -[passwords] -user_test = "user123" -... -``` +When deploying your app on Azure or AWS, you may need to configure user credentials as environment variables. Here's how you can accomplish this: -Inserting secrets.toml file to the web app: +- **User Credentials Environment Variable** -Add a value of user=password separating each user by `;` to your web app environment. + Add credentials in the format `user=password`, separated by semicolons (`;`), to your web app's environment variables: -`USER_CREDENTIALS="user1=pass1;user2=pass2"` + ```plaintext + USER_CREDENTIALS="user1=pass1;user2=pass2" + ``` +This setup ensures secure handling of user authentication and sensitive configurations across different deployment platforms. By using these configurations, you can maintain control over user access and protect essential settings. diff --git a/app/Home.py b/app/Home.py index 2857e54..4580611 100644 --- a/app/Home.py +++ b/app/Home.py @@ -7,9 +7,8 @@ import os.path import streamlit as st - -import app.util.mermaid as mermaid -from app.components.app_loader import load_multipage_app +import util.mermaid as mermaid +from components.app_loader import load_multipage_app filename = inspect.getframeinfo(inspect.currentframe()).filename path = os.path.dirname(os.path.abspath(filename)) diff --git a/app/components/app_loader.py b/app/components/app_loader.py index b7d1463..bf6b0b9 100644 --- a/app/components/app_loader.py +++ b/app/components/app_loader.py @@ -3,23 +3,22 @@ # import os +import components.app_mode as am +import components.app_user as au import streamlit as st +from components.app_auth import check_password, load_passwords +from javascript.styles import add_styles from streamlit.source_util import ( _on_pages_changed, get_pages, ) -import app.components.app_mode as am -import app.components.app_user as au -from app.components.app_auth import check_password, load_passwords -from app.javascript.styles import add_styles - def load_multipage_app(sv=None): if os.getenv("AUTH_ENABLED") == "TRUE": load_passwords() check_password() - if os.getenv("MODE") == "CLOUD": + if os.getenv("HIDE_SETTINGS") == "TRUE": current_pages = get_pages("Home.py") for key, value in current_pages.items():