Streamlit enables you to create interactive visualizations for datasets and machine learning model demos with ease. This guide walks you through hosting models and datasets, and serving Streamlit apps on Hugging Face Spaces.
Building Demos for Your Models
You can load any Hugging Face model and build intuitive UIs using Streamlit. For example, we'll recreate "Write with Transformer", an app that lets you compose text using GPT-2 and XLNet.
Streamlit provides components to capture hyperparameters:
st.text_areafor input textst.sidebar.sliderfor continuous values like temperature and top-kst.sidebar.number_inputfor integer values like number of return sequences
Inference output is displayed with st.write(). The full code is available here.
Showcasing Datasets and Data Visualizations
Streamlit integrates smoothly with Hugging Face Datasets, pandas, matplotlib, seaborn, and bokeh. Use streaming mode for large datasets to avoid downloading all data.
from datasets import load_dataset
import streamlit as st
dataset = load_dataset("merve/poetry", streaming=True)
df = pd.DataFrame.from_dict(dataset["train"])
Display data with st.dataframe(df). For interactive plots, use st.barchart() or embed matplotlib/seaborn figures with st.pyplot().
Example: visualizing most common words in poems:
st.write("Most appearing words including stopwords")
st.bar_chart(words[0:50])
To use seaborn:
st.write("Number of poems for each author")
sns.catplot(x="author", data=df, kind="count", aspect=4)
plt.xticks(rotation=90)
st.pyplot()
Check out the full demo here.
Hosting Your Projects in Hugging Face Spaces
Upload your files by dragging and dropping. Ensure requirements.txt includes all dependencies and the Streamlit version matches your local environment. Refer to the Spaces API reference for details.
Start building your own apps on Hugging Face Spaces.