Writing

Using HuggingFace without sharing your code!

In short: you can run a Python project on HuggingFace Spaces while the source stays in a private GitHub repo. Here is the three-step setup, with the code that does the cloning. Ever wanted to breathe…

published
read time
2 min
words
367
lang
en

In short: you can run a Python project on HuggingFace Spaces while the source stays in a private GitHub repo. Here is the three-step setup, with the code that does the cloning.

Ever wanted to breathe life into one of your Python projects? HuggingFace Spaces is a great place to showcase and share a machine learning model. But what if you want to keep the code private? Here is how.

  1. Push the code somewhere privateUpload the project to a private GitHub repository so it is not publicly readable.
  2. Clone it at runtimeGive the Space your credentials as environment variables and pull the repo when the app starts.
  3. Deploy the wrapper to SpacesOnly the thin launcher lives on HuggingFace. The real code arrives at runtime.

Step 1: Put the code in a private GitHub repo

Upload your project to a private GitHub repository. That keeps the code out of public view. Store your GitHub Personal Access Token (PAT) and any other credentials securely.

Step 2: Set up the environment and clone the repo

Next, set up your environment to use those credentials. Here is one way to do it in Python.

from git import Repo
import os

# Retrieve the environment variables
GITHUB_PAT = os.getenv('GITHUB_PAT')
GIT_id = os.getenv('GIT_id')
GIT_repo = os.getenv('GIT_repo')

if GITHUB_PAT:
    print("GITHUB_PAT set")

# Ensure the cloned_repo directory does not already exist
if not os.path.exists('cloned_repo'):  
    # Clone the repository using the Personal Access Token for authentication
    Repo.clone_from(f'https://{GIT_id}:{GITHUB_PAT}@github.com/{GIT_id}/{GIT_repo}.git', './cloned_repo')

# Import the main module from the cloned repository
import cloned_repo.main as main
from cloned_repo.main import *

What the script does:

  1. Reads your GitHub PAT, repository ID and repository name from environment variables.
  2. Checks that GITHUB_PAT is set.
  3. Makes sure the cloned_repo directory does not already exist, to avoid conflicts.
  4. Clones the private repository, authenticating with the PAT.
  5. Imports the main module from the clone.
CarefulThe PAT goes in the clone URL. Keep it in an environment variable, never in the file you upload to the Space.

Step 3: Upload to HuggingFace

Now push this launcher to HuggingFace Spaces. You get to run your models and share their outputs without exposing the source. All the features of Spaces, none of the code on display.