Skip to content

Commit 77fd2e0

Browse files
committed
Copied code from huggingface with addition of github actions
1 parent f003874 commit 77fd2e0

File tree

13 files changed

+3278
-0
lines changed

13 files changed

+3278
-0
lines changed

.github/workflows/build-images.yml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
name: Build Docker Images
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
workflow_dispatch:
7+
8+
env:
9+
REGISTRY: ghcr.io
10+
IMAGE_NAME: ${{ github.repository }}
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
permissions:
16+
contents: read
17+
packages: write
18+
19+
steps:
20+
- name: Checkout repository
21+
uses: actions/checkout@v4
22+
23+
- name: Set up Docker Buildx
24+
uses: docker/setup-buildx-action@v3
25+
26+
- name: Log in to GitHub Container Registry
27+
uses: docker/login-action@v3
28+
with:
29+
registry: ${{ env.REGISTRY }}
30+
username: ${{ github.actor }}
31+
password: ${{ secrets.GITHUB_TOKEN }}
32+
33+
# - name: Log in to Docker Hub
34+
# if: ${{ secrets.DOCKERHUB_USERNAME }}
35+
# uses: docker/login-action@v3
36+
# with:
37+
# username: ${{ secrets.DOCKERHUB_USERNAME }}
38+
# password: ${{ secrets.DOCKERHUB_TOKEN }}
39+
40+
- name: Extract metadata
41+
id: meta
42+
uses: docker/metadata-action@v5
43+
with:
44+
images: |
45+
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
46+
# ${{ secrets.DOCKERHUB_USERNAME }}/nemaquant
47+
tags: |
48+
type=raw,value=latest
49+
type=sha,prefix=main-
50+
51+
- name: Build and push Docker image
52+
uses: docker/build-push-action@v5
53+
with:
54+
context: .
55+
platforms: linux/amd64,linux/arm64
56+
push: true
57+
tags: ${{ steps.meta.outputs.tags }}
58+
labels: ${{ steps.meta.outputs.labels }}
59+
cache-from: type=gha
60+
cache-to: type=gha,mode=max
61+
62+
- name: Summary
63+
run: |
64+
echo "## Build Summary" >> $GITHUB_STEP_SUMMARY
65+
echo "- **GitHub Container Registry**: \`${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest\`" >> $GITHUB_STEP_SUMMARY
66+
echo "- **Platforms**: linux/amd64, linux/arm64" >> $GITHUB_STEP_SUMMARY
67+
# if [ -n "${{ secrets.DOCKERHUB_USERNAME }}" ]; then
68+
# echo "- **Docker Hub**: \`${{ secrets.DOCKERHUB_USERNAME }}/nemaquant:latest\`" >> $GITHUB_STEP_SUMMARY
69+
# fi
70+
echo "" >> $GITHUB_STEP_SUMMARY
71+
echo "### Pull the image:" >> $GITHUB_STEP_SUMMARY
72+
echo "\`\`\`bash" >> $GITHUB_STEP_SUMMARY
73+
echo "docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest" >> $GITHUB_STEP_SUMMARY
74+
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
75+

.github/workflows/deploy-to-hf.yml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
name: Deploy to Hugging Face Spaces
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
workflow_dispatch:
7+
8+
jobs:
9+
deploy:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: read
13+
14+
steps:
15+
- name: Checkout repository
16+
uses: actions/checkout@v4
17+
18+
- name: Set up Python
19+
uses: actions/setup-python@v4
20+
with:
21+
python-version: '3.12'
22+
23+
- name: Install dependencies
24+
run: |
25+
pip install huggingface_hub
26+
27+
- name: Login to Hugging Face
28+
uses: huggingface/huggingface_hub@v0.19.0
29+
with:
30+
token: ${{ secrets.HUGGINGFACE_TOKEN }}
31+
32+
- name: Push to Hugging Face Space
33+
run: |
34+
# Check if HUGGINGFACE_SPACE_REPO is set
35+
if [ -z "${{ secrets.HUGGINGFACE_SPACE_REPO }}" ]; then
36+
echo "HUGGINGFACE_SPACE_REPO secret not set. Please set it to your space repository name (e.g., 'username/nemaquant')"
37+
exit 1
38+
fi
39+
40+
# Create a temporary directory for the space
41+
mkdir -p space_repo
42+
cd space_repo
43+
44+
# Initialize git repository
45+
git init
46+
git remote add origin https://huggingface.co/spaces/${{ secrets.HUGGINGFACE_SPACE_REPO }}
47+
48+
# Copy necessary files
49+
cp ../README.md .
50+
cp ../app.py .
51+
cp ../requirements.txt .
52+
cp ../Dockerfile .
53+
cp ../yolo_utils.py .
54+
cp -r ../templates .
55+
cp -r ../static .
56+
57+
# Create .gitattributes for LFS if needed
58+
echo "*.pt filter=lfs diff=lfs merge=lfs -text" > .gitattributes
59+
60+
# Add and commit
61+
git add .
62+
git config user.name "GitHub Actions"
63+
git config user.email "actions@github.com"
64+
git commit -m "Update NemaQuant app from GitHub Actions - ${{ github.sha }}"
65+
66+
# Push to Hugging Face Space
67+
git push origin main
68+
69+
- name: Summary
70+
run: |
71+
echo "## Deployment Summary" >> $GITHUB_STEP_SUMMARY
72+
echo "- **Hugging Face Space**: \`https://huggingface.co/spaces/${{ secrets.HUGGINGFACE_SPACE_REPO }}\`" >> $GITHUB_STEP_SUMMARY
73+
echo "- **Commit**: \`${{ github.sha }}\`" >> $GITHUB_STEP_SUMMARY
74+
echo "- **Status**: Successfully deployed to HF Spaces" >> $GITHUB_STEP_SUMMARY

Dockerfile

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Use an official Python runtime as a parent image
2+
FROM python:3.12
3+
# FROM nvidia/cuda:12.2.0-runtime-ubuntu22.04
4+
5+
# run updates before switching over to non-root user
6+
RUN apt-get update && apt-get install -y --no-install-recommends \
7+
libgl1 \
8+
libglib2.0-0 \
9+
libsm6 \
10+
libxrender1 \
11+
libxext6 \
12+
&& rm -rf /var/lib/apt/lists/*
13+
14+
# add new user with ID 1000 to avoid permission issues on HF spaces
15+
RUN useradd -m -u 1000 user
16+
USER user
17+
18+
# Set home to user's home dir and add local bin to PATH
19+
ENV HOME=/home/user \
20+
PATH=/user/user/.local/bin:$PATH
21+
22+
# Set the working directory in the container
23+
WORKDIR $HOME/app
24+
25+
# Try and run pip command after setting the user with `USER user` to avoid permission issues with Python
26+
# NOTE - this is from the HF Spaces docs, not sure if necessary
27+
COPY --chown=user ./requirements.txt .
28+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
29+
30+
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
31+
COPY --chown=user . $HOME/app
32+
33+
# Install any needed packages specified in requirements.txt
34+
# --no-cache-dir: Disables the cache to reduce image size.
35+
# -r requirements.txt: Specifies the file containing the list of packages to install.
36+
# RUN pip install --no-cache-dir -r requirements.txt
37+
38+
# Create the necessary dirs
39+
# we should not need to chown, since we are using USER user above
40+
RUN mkdir -p uploads results annotated .yolo_config
41+
42+
# set the env var for YOLO user config directory
43+
ENV YOLO_CONFIG_DIR=.yolo_config
44+
45+
# Copy the rest of the application code into the container at /app
46+
# This includes app.py, nemaquant.py, templates/, static/, etc.
47+
# COPY . .
48+
49+
# Make port 7860 available to the world outside this container
50+
# This is the port Flask will run on (as configured in app.py)
51+
# Hugging Face Spaces typically uses this port
52+
EXPOSE 7860
53+
54+
# Define environment variables (optional, can be useful)
55+
# ENV NAME=World
56+
57+
# Run app.py when the container launches
58+
# Use gunicorn for production deployment if preferred over Flask's development server
59+
# CMD ["gunicorn", "--bind", "0.0.0.0:7860", "app:app"]
60+
# For simplicity during development and typical HF Spaces use:
61+
CMD ["python", "app.py"]

0 commit comments

Comments
 (0)