Skip to content

Commit 3038352

Browse files
authored
Make demo app deploy-able to Hugging Face spaces (#685)
1 parent a1fb295 commit 3038352

File tree

4 files changed

+121
-3
lines changed

4 files changed

+121
-3
lines changed

demo/Dockerfile

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
FROM python:3.10.14-bullseye
2+
3+
RUN apt-get update && \
4+
apt-get install -y \
5+
# General dependencies
6+
locales \
7+
locales-all && \
8+
# Clean local repository of package files since they won't be needed anymore.
9+
# Make sure this line is called after all apt-get update/install commands have
10+
# run.
11+
apt-get clean && \
12+
# Also delete the index files which we also don't need anymore.
13+
rm -rf /var/lib/apt/lists/*
14+
15+
ENV LC_ALL en_US.UTF-8
16+
ENV LANG en_US.UTF-8
17+
ENV LANGUAGE en_US.UTF-8
18+
19+
# Install dependencies
20+
COPY requirements.txt .
21+
RUN pip install -r requirements.txt
22+
23+
# Create non-root user
24+
RUN groupadd -g 900 mesop && useradd -u 900 -s /bin/bash -g mesop mesop
25+
USER mesop
26+
27+
# Add app code here
28+
COPY . /srv/mesop-app
29+
WORKDIR /srv/mesop-app
30+
31+
# Run Mesop through gunicorn. Should be available at localhost:8080
32+
CMD ["gunicorn", "--bind", "0.0.0.0:8080", "main:me"]

demo/README.md

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
---
2+
title: Mesop Demo Gallery
3+
emoji: 👓
4+
colorFrom: blue
5+
colorTo: purple
6+
sdk: docker
7+
pinned: false
8+
license: apache-2.0
9+
app_port: 8080
10+
---
11+
112
# Mesop demo app
213

314
This app demonstrates Mesop's various components and features. Create your own Cloud Run app by following: https://google.github.io/mesop/guides/deployment/
@@ -12,7 +23,7 @@ From workspace root:
1223
rm -rf demo/venv && \
1324
virtualenv --python python3 demo/venv && \
1425
source demo/venv/bin/activate && \
15-
pip install -r demo/requirements.txt --no-binary pydantic
26+
pip install -r demo/requirements.txt
1627
```
1728

1829
### Run
@@ -35,7 +46,12 @@ If you add more demos and want to re-generate screenshots, do the following step
3546

3647
## Deployment
3748

38-
> Note: make sure you [generate screenshots](#generate-screenshots) before deploying!
49+
**Pre-requisites:**
50+
51+
- Make sure you [generate screenshots](#generate-screenshots) before deploying!
52+
- Ensure a recent version of Mesop has been published to pip, otherwise the demos may not work (because they rely on a new API).
53+
54+
### Deploy to Cloud Run
3955

4056
This app is deployed to Google Cloud Run.
4157

@@ -44,3 +60,17 @@ gcloud run deploy mesop --source .
4460
```
4561

4662
See our Mesop deployment [docs](https://google.github.io/mesop/guides/deployment/#deploy-to-google-cloud-run) for more background.
63+
64+
### Deploy to Hugging Face Spaces
65+
66+
> NOTE: You need to update demo/requirements.txt to point to the latest Mesop version because Hugging Face Spaces may use a cached version of Mesop which is too old.
67+
68+
Because Hugging Face Spaces has restrictions on not having binary files (e.g. image files), we cannot push the full Mesop Git repo to Hugging Face Spaces. Instead, we copy just the `demo` directory and turn it into a standalone Git repo which we deploy.
69+
70+
```sh
71+
./demo/deploy_to_hf.sh ../hf_demo
72+
```
73+
74+
You can change `../hf_demo` to any dir path outside of your Mesop repo.
75+
76+
> Note: if you get an error in Hugging Face Spaces "No app file", then you can create an "app.py" file in the Spaces UI to manually trigger a build. This seems like a bug with Hugging Face.

demo/deploy_to_hf.sh

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
error_handler() {
6+
echo "Error: An error occurred. Exiting script."
7+
exit 1
8+
}
9+
10+
# Set up error handling
11+
trap error_handler ERR
12+
13+
if [ $# -eq 0 ]; then
14+
echo "Error: Please provide a destination path as an argument."
15+
exit 1
16+
fi
17+
18+
DEST_PATH="$1"
19+
20+
if [ ! -d "$DEST_PATH" ]; then
21+
echo "Destination path does not exist. Creating it now."
22+
mkdir -p "$DEST_PATH"
23+
fi
24+
25+
# Get the path of this script which is the demo dir.
26+
DEMO_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
27+
28+
cp -R "$DEMO_DIR/" "$DEST_PATH"
29+
echo "Demo files have been copied to $DEST_PATH"
30+
31+
cd "$DEST_PATH"
32+
echo "Changed directory to $DEST_PATH"
33+
34+
echo "Updating allowed iframe parents to include hugging face spaces site..."
35+
# Find all .py files and update the allowed_iframe_parents list
36+
find . -name "*.py" -type f | while read -r file; do
37+
# Use sed with -i.bak so it woroks on MacOs
38+
sed -i.bak 's/allowed_iframe_parents=\["https:\/\/google\.github\.io"\]/allowed_iframe_parents=["https:\/\/google.github.io", "https:\/\/huggingface.co"]/' "$file"
39+
# Remove the backup file created by sed
40+
rm "${file}.bak"
41+
done
42+
echo "Update complete."
43+
44+
git init
45+
46+
git add .
47+
48+
git commit -m "Commit"
49+
50+
# The hf remote may already exist if the script has been run
51+
# on this dest directory before.
52+
git remote add hf https://huggingface.co/spaces/wwwillchen/mesop || true
53+
54+
git push hf --force
55+
56+
echo "Pushed to: https://huggingface.co/spaces/wwwillchen/mesop. Check the logs to see that it's deployed correctly."

demo/requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
mesop
1+
mesop>=0.10.0
22
Flask==3.0.0
33
gunicorn==22.0.0
44
Werkzeug==3.0.1

0 commit comments

Comments
 (0)