streamlit and gradioUse a GenAI app to prototype (turn PDF notes into clinical insight)
Brainstorm end goal
Who is audience? (non-technical people)
What does the app look like?
Features (drag and drop, export to csv, etc.)
be specific (do not give open ended tasks to GenAI apps)
⚠️ NOTE
Give the AI a role act as a product manager
Google Colab
Streamlit
ngrok account and auth token
Sarvam AI API key
Optional
Python
Local files
Click on New app
Here is a deployed app
Push your code to github and deploy
Setup a repository in github
Or run in Google Colab
Run streamlit using
streamlit run script.py
import streamlit as st
import pandas as pd
import numpy as np
st.write("Streamlit supports a wide range of data visualizations")
all_users = ["Alice", "Bob", "Charly"]
with st.container(border=True):
users = st.multiselect("Users", all_users, default=all_users)
rolling_average = st.toggle("Rolling average")
np.random.seed(19)
data = pd.DataFrame(np.random.randn(20, len(users)), columns=users)
if rolling_average:
data = data.rolling(7).mean().dropna()
tab1, tab2 = st.tabs(["Chart", "Dataframe"])
tab1.line_chart(data, height=250)
tab2.dataframe(data, height=250, use_container_width=True)
To save your ngrok authtoken securely in Google Colab, you should use Colab’s Secrets feature. This allows you to store sensitive information like API keys or tokens without embedding them directly in your code, which is important for security and when sharing notebooks.
ngrok authtoken, you could name it YOUR_AUTHTOKEN (or any other descriptive name you prefer, but remember it for later).ngrok authtoken into the “Value” field.Once saved, you can access this secret in your Python code using from google.colab import userdata and then userdata.get('YOUR_AUTHTOKEN') (replacing 'YOUR_AUTHTOKEN' with the name you gave your secret).
st.slider st.button st.text_input() st.selectbox() st.checkbox() st.file__uploader()
Code from deeplearning.ai
# import packages
from dotenv import load_dotenv
import openai
import streamlit as st
# load environment variables from .env file
load_dotenv()
# Initialize OpenAI client
client = openai.OpenAI()
st.title("Hello, GenAI!")
st.write("This is your first Streamlit app.")
response = client.responses.create(
model="gpt-4o",
input=[
{"role": "user", "content": "Explain generative AI in one sentence."} # Prompt
],
temperature=0.7, # A bit of creativity
max_output_tokens=100 # Limit response length
)
# print the response from OpenAI
st.write(response.output[0].content[0].text)
st.bar_chart()
st.scatter_chart(df), works with pandas dataframe
use with matplotlib st.pyplot(fig)
use with plotly st.plotly_chart(fig, use_container_width = True)
streamlitGithub Create new account on github
Create new app and connect to github
Create new repository on github
Upload your code to github (code below)
import streamlit as st
import pandas as pd
import numpy as np
import folium
from folium.plugins import HeatMap
from streamlit_folium import st_folium
st.title("Outbreak Investigator")
st.write("Adjust the settings in the sidebar, then try to identify the source of the outbreak from the map.")
# --- SIDEBAR CONTROLS ---
st.sidebar.header("Settings")
total_cases = st.sidebar.slider("Total cases", 100, 1000, 500)
cluster_pct = st.sidebar.slider("% of cases near the source", 10, 90, 70)
show_source = st.sidebar.checkbox("Reveal the true source")
# --- GENERATE SYNTHETIC DATA ---
market_lat, market_lon = 30.6195, 114.2577
cluster_count = int(total_cases * cluster_pct / 100)
noise_count = total_cases - cluster_count
np.random.seed(420)
cluster_lats = np.random.normal(market_lat, 0.005, cluster_count)
cluster_lons = np.random.normal(market_lon, 0.005, cluster_count)
noise_lats = np.random.uniform(30.50, 30.70, noise_count)
noise_lons = np.random.uniform(114.20, 114.40, noise_count)
cases = pd.DataFrame({
'lat': np.concatenate([cluster_lats, noise_lats]),
'lon': np.concatenate([cluster_lons, noise_lons]),
})
pois = pd.DataFrame({
'name': ['Wuhan International Plaza', 'Huanan Seafood Market', 'Hankou Railway Station', 'Wuhan CDC'],
'lat': [30.584, 30.6195, 30.618, 30.612],
'lon': [114.271, 114.2577, 114.250, 114.265],
'is_source': [False, True, False, False],
})
# --- SHOW STATS ---
st.write(f"**Total cases:** {total_cases} — **Clustered:** {cluster_count} — **Scattered:** {noise_count}")
# --- BUILD MAP ---
m = folium.Map(location=[30.61, 114.28], zoom_start=13, tiles='cartodbpositron')
HeatMap(cases[['lat', 'lon']].values.tolist(), radius=12, blur=15).add_to(m)
for _, poi in pois.iterrows():
if poi['is_source'] and show_source:
color = 'red'
label = f"TRUE SOURCE: {poi['name']}"
else:
color = 'black'
label = poi['name']
folium.Marker(
location=[poi['lat'], poi['lon']],
popup=label,
tooltip=label,
icon=folium.Icon(color=color, icon='question-sign'),
).add_to(m)
st_folium(m, width=900, height=550)
requirements.txt is herestreamlit>=1.35.0
streamlit-folium>=0.20.0
folium>=0.17.0
pandas>=2.0.0
numpy>=1.26.0
Go to Streamlit and connect your github repository
can also deploy on Huggingface spaces
st.session_state()
Gradiohere is python code using gradio and plotly.express libraries to create an interactive web application that visualizes car-sharing data on a map.
Here is a breakdown of what it does:
Loads Data: It starts by loading a built-in spatial dataset called carshare from Plotly, which contains information about car availability, centroids (latitude and longitude), and peak hour usage.
generate_map function: This function takes a peak_hour_threshold as input. It filters the carshare data to only include entries where the peak_hour is less than or equal to the provided threshold. Then, it creates a scatter mapbox plot using plotly.express, displaying car availability (car_hours) at different geographical locations (centroid_lat, centroid_lon). The color and size of the points on the map are determined by car_hours, and the map style is set to carto-positron.
Gradio Interface: It defines a Gradio application using gr.Blocks:
It adds a title and a descriptive markdown text.
It includes a gr.Slider component, allowing users to select a peak_hour value (from 0 to 23). This slider acts as the input to filter the data.
It defines a gr.Plot() component as the output, which will display the Plotly map generated by the generate_map function.
Interaction Logic: The hour_slider.change() method connects the slider’s value to the generate_map function. This means that whenever the slider’s value changes, the generate_map function is called with the new slider value, and the updated map is displayed in map_output.
Initial Load: demo.load() ensures that the map is generated and displayed with the initial slider value (12) when the application first loads.
Launch: Finally, demo.launch() starts the Gradio web server, making the interactive application accessible.
Code is here
import gradio as gr
import plotly.express as px
# 1. Load a built-in spatial dataset from Plotly
df = px.data.carshare()
def generate_map(peak_hour_threshold):
"""
Filters the dataframe based on peak hour and returns a Plotly figure.
"""
# Filter data based on the user's slider input
filtered_df = df[df['peak_hour'] <= peak_hour_threshold]
# Create the spatial scatter plot
fig = px.scatter_mapbox(
filtered_df,
lat="centroid_lat",
lon="centroid_lon",
color="car_hours",
size="car_hours",
color_continuous_scale=px.colors.sequential.Plasma,
size_max=15,
zoom=10,
mapbox_style="carto-positron",
title=f"Car Availability (Peak Hour ≤ {peak_hour_threshold})"
)
fig.update_layout(margin={"r":0,"t":40,"l":0,"b":0})
return fig
# 2. Define the Gradio Interface
with gr.Blocks() as demo:
gr.Markdown("# 🚗 Montreal Ride-Sharing Explorer")
gr.Markdown("Adjust the slider to filter car availability by peak hour usage.")
with gr.Row():
# Input: A slider for filtering
hour_slider = gr.Slider(
minimum=0,
maximum=23,
value=12,
step=1,
label="Filter by Peak Hour"
)
# Output: The Plotly Map
map_output = gr.Plot()
# Trigger logic: When the slider moves, update the map
hour_slider.change(fn=generate_map, inputs=hour_slider, outputs=map_output)
# Run the map on load
demo.load(fn=generate_map, inputs=hour_slider, outputs=map_output)
if __name__ == "__main__":
demo.launch()
streamlit, gradio