How do I configure and troubleshoot Streamlit and Gradio applications in my SageMaker AI notebook instances?
I want to configure and troubleshoot Streamlit and Gradio applications in my Amazon SageMaker AI notebook instances.
Resolution
Make sure that you meet the following requirements:
- You have an active SageMaker AI notebook instance.
- Your notebook instance has internet access to download and install Python packages and dependencies. If you turned off internet access, then update the scripts and make sure that your packages are available offline in the notebook instance.
- The user that runs the notebook has sudo permissions to install the system dependencies. By default, the SageMaker AI notebook instance environment provides sudo permissions.
- Your notebook instance has Python 3.6 or later. By default, SageMaker AI notebook instances have Python preinstalled.
- Other processes don't use Ports 8501 for Streamlit and 7861 for Gradio in your notebook instance. You can update your default ports in the scripts based on your use case.
- The security group that's associated with your notebook instance allows inbound traffic on ports 8501 and 7861.
Note: The following scripts are for a Jupyter notebook code cell.
Install and configure your Streamlit application
Complete the following steps:
-
Run the following one-time setup script:
%%bash #!/bin/bash CURRENTDATE=$(date +"%Y-%m-%d %T") CYAN='\033[1;36m' NC='\033[0m' # Create the app.py file for Streamlit echo -e "${CYAN}${CURRENTDATE}: [INFO]:${NC}Creating app_streamlit.py for streamlit\n" cat << EOF > app_streamlit.py import streamlit as st st.title("Hello World") EOF echo -e "app_streamlit.py has been created successfully \n" echo -e "${CYAN}${CURRENTDATE}: [INFO]:${NC}Setup completed\n"The preceding script uses pip and the appropriate package manager to install Streamlit. Also, the script creates a basic app_streamlit.py Streamlit application file with a "Hello World" title.
-
Run the following script:
%%bash #!/bin/bash CURRENTDATE=$(date +"%Y-%m-%d %T") RED='\033[0;31m' CYAN='\033[1;36m' GREEN='\033[1;32m' NC='\033[0m' echo -e "${CYAN}${CURRENTDATE}: [INFO]:${NC}Getting the URL to view your Streamlit app in the browser\n" PORT=8501 echo -e "${CYAN}${CURRENTDATE}: [INFO]:${NC} Port Number ${PORT}\n" RESOURCE_NAME=$(jq .ResourceName /opt/ml/metadata/resource-metadata.json || exit 1) RESOURCE_ARN=$(jq .ResourceArn /opt/ml/metadata/resource-metadata.json || exit 1) RESOURCE_NAME=$(sed -e 's/^"//' -e 's/"$//' <<< "$RESOURCE_NAME") RESOURCE_ARN=$(sed -e 's/^"//' -e 's/"$//' <<< "$RESOURCE_ARN") RESOURCE_ARN_ARRAY=($(echo "$RESOURCE_ARN" | tr ':' '\n')) REGION=$(echo "${RESOURCE_ARN_ARRAY[3]}") NOTEBOOK_URL="https://${RESOURCE_NAME}.notebook.${REGION}.sagemaker.aws" link="${NOTEBOOK_URL}/proxy/${PORT}/" echo -e "${CYAN}${CURRENTDATE}: [INFO]:${NC} Starting Streamlit App" echo -e "${CYAN}${CURRENTDATE}: [INFO]: ${GREEN}${link}${NC}" # Run the Streamlit app and save the output to "streamlit.txt" echo -e "${CYAN}${CURRENTDATE}: [INFO]:${NC}Allow upto 10 seconds before clicking the link for app launch to be successful" streamlit run app_streamlit.py --browser.serverPort=${PORT} --server.port=${PORT} >streamlit.txtThe preceding script runs the Streamlit application on port 8501 and saves the output to a temporary file. The script uses SageMaker notebook instance information from the metadata to display the Streamlit URL.
For more information about how to install Streamlit, see Install Streamlit on the Streamlit website.
Troubleshoot issues with your Streamlit application
Take the following actions:
-
If the Streamlit application doesn't start, then run the following command to check whether you correctly installed the application:
pip list | grep streamlit -
Run the following command to make sure that other processes don't use port 8501:
lsof -i :8501 -
Make sure that your SageMaker AI notebook instance's security group allows inbound traffic on the port 8501. If needed, you can update the PORT number in the script to update the port that Streamlit uses.
-
To debug Streamlit applications, add the --logger.level=debug argument to the last line of the script, such as in the following example:
streamlit run app_streamlit.py --browser.serverPort=${PORT} --logger.level=debug --server.port=${PORT} >streamlit_logs.txt
Install and configure your Gradio application
Complete the following steps:
-
Run the following one-time setup script:
%%bash #!/bin/bash CURRENTDATE=`date +"%Y-%m-%d %T"` RED='\033[0;31m' CYAN='\033[1;36m' GREEN='\033[1;32m' NC='\033[0m' PORT=7861 # Install python dependencies echo -e "${CYAN}${CURRENTDATE}: [INFO]:${NC}Running pip install gradio\n" pip install --no-cache-dir -q gradio # Create the app.py file for Gradio echo -e "${CYAN}${CURRENTDATE}: [INFO]:${NC}Creating app_gradio.py for Gradio\n" cat << EOF > app_gradio.py import gradio as gr def greet(name): return "Hello " + name + "!" demo = gr.Interface(fn=greet, inputs= "text", outputs = "text") demo.launch(inline=False, debug=True, server_port=${PORT}, root_path="/proxy/${PORT}") EOF echo -e "${CYAN}${CURRENTDATE}: [INFO]:${NC}Setup completed. "The preceding script uses pip and the appropriate package manager to install Gradio and other operating system (OS) dependencies. Also, the script creates a basic app_gradio.py Gradio application file with a greeting function.
-
Run the following script:
%%bash CYAN='\033[1;36m' GREEN='\033[1;32m' RED='\033[0;31m' NC='\033[0m' PORT=7861 MAX_RETRIES=15 RETRY_INTERVAL=3 GRADIO_PID="" cleanup() { echo -e "\n${CYAN}[INFO]: Cleaning up processes...${NC}" if [ ! -z "$GRADIO_PID" ]; then pkill -P $GRADIO_PID kill -9 $GRADIO_PID 2>/dev/null fi pkill -f "python app_gradio.py" exit 0 } check_gradio() { netstat -tuln | grep ":${PORT}" >/dev/null } trap cleanup EXIT INT TERM echo -e "${CYAN}[INFO]: Starting Gradio App${NC}" python app_gradio.py & GRADIO_PID=$! echo -e "${CYAN}[INFO]: Waiting for Gradio to start on port ${PORT}...${NC}" retries=0 while ! check_gradio; do if [ $retries -ge $MAX_RETRIES ]; then echo -e "${RED}[ERROR]: Gradio failed to start after ${MAX_RETRIES} attempts${NC}" cleanup exit 1 fi sleep $RETRY_INTERVAL ((retries++)) done RESOURCE_NAME=$(jq -r .ResourceName /opt/ml/metadata/resource-metadata.json) RESOURCE_ARN=$(jq -r .ResourceArn /opt/ml/metadata/resource-metadata.json) REGION=$(echo "$RESOURCE_ARN" | cut -d':' -f4) echo -e "${GREEN}Gradio is running at:${NC}" echo -e "${GREEN}https://${RESOURCE_NAME}.notebook.${REGION}.sagemaker.aws/proxy/${PORT}/${NC}" while true; do if ! check_gradio; then echo -e "${RED}[ERROR]: Gradio process is no longer running${NC}" cleanup exit 1 fi sleep 10 doneThe preceding script sets up a cleanup function to remove the Gradio process on exit. The script runs the Gradio app in the background, and then retrieves the SageMaker AI notebook instance information from the metadata. Then, it constructs and displays the URL to access the Gradio application in the browser.
For more information about how to install Gradio, see Installation on the Gradio website.
Troubleshoot your Gradio application
Take the following actions:
-
If the Gradio app doesn't start, then run the following command to check that you correctly installed the application:
pip list | grep gradio -
Run the following command to make sure that other processes don't use port 7861:
lsof -i :7861 -
If you can't generate a URL, then check the file permissions on the metadata file.
-
Make sure that the SageMaker AI notebook instance's security group allows inbound traffic on the required port.
-
If you can't access the application, then make sure that the Gradio launch configuration root_path matches the URL proxy path.
-
If you receive the "URL 500: Internal Error" message, then stop and rerun the script from step 2 in the Configure your Gradio application section. Or, run the following command to reinstall your application:
pip install --no-cache-dir -q gradiohttps://www.gradio.app/guides/quickstart
- Topics
- Machine Learning & AI
- Tags
- Amazon SageMaker
- Language
- English

Relevant content
asked 8 months ago
- Accepted Answer
asked 3 years ago
asked 3 years ago