|

|  How to Integrate Hugging Face with GitHub

How to Integrate Hugging Face with GitHub

January 24, 2025

Master seamless integration of Hugging Face and GitHub with this concise guide, enhancing your AI project workflows and collaboration effortlessly.

How to Connect Hugging Face to GitHub: a Simple Guide

 

Setting Up Your Environment

 

  • Ensure you have a GitHub account. If not, create one at GitHub.
  •  

  • Sign up for a Hugging Face account at Hugging Face.
  •  

  • Install Git on your system if not already installed: Download Git.

 

git --version

 

Creating a Repository on GitHub

 

  • Log in to your GitHub account and navigate to the "Repositories" tab.
  •  

  • Click on "New" to create a new repository.
  •  

  • Provide a repository name and description, and decide if it should be public or private.
  •  

  • Click "Create repository" to finalize.

 

Cloning Your GitHub Repository Locally

 

  • Navigate to your repository page on GitHub and copy the repository URL.
  •  

  • Open a terminal and run the following command to clone the repository:

 

git clone <Your-GitHub-Repo-URL>

 

Setting Up Hugging Face Transformers Library

 

  • Ensure Python is installed. You can verify or download it from python.org.
  •  

  • Install the Hugging Face Transformers library:

 

pip install transformers

 

Creating a Sample Model Script

 

  • Navigate to your cloned repository directory:

 

cd <Your-Repo-Name>

 

  • Create a new file named `model_script.py` and add the following code to load a pre-trained model from Hugging Face:

 

from transformers import pipeline

classifier = pipeline('sentiment-analysis')

result = classifier('I love using Hugging Face!')
print(result)

 

Committing and Pushing Changes to GitHub

 

  • Stage your changes:

 

git add model_script.py

 

  • Commit your changes with a descriptive message:

 

git commit -m "Add sentiment analysis script using Hugging Face"

 

  • Push your changes to the GitHub repository:

 

git push origin main

 

Integrating Hugging Face Inference API

 

  • Navigate to your Hugging Face account and create a new token for authorization.
  •  

  • Save this token securely for API usage.
  •  

  • Update your script to use Hugging Face's Inference API:

 

import requests

API_URL = "https://api-inference.huggingface.co/models/distilbert-base-uncased-finetuned-sst-2-english"
headers = {"Authorization": "Bearer <Your-Hugging-Face-Token>"}

def query(payload):
    response = requests.post(API_URL, headers=headers, json=payload)
    return response.json()

data = query({"inputs": "I love using Hugging Face!"})
print(data)

 

Final Commit and Push

 

  • Stage, commit, and push your changes with an appropriate message:

 

git add model_script.py
git commit -m "Integrate Hugging Face Inference API"
git push origin main

 

Verifying Integration

 

  • Check your GitHub repository to verify the presence of your recent commits and files.
  •  

  • Run the script locally to ensure it integrates properly with both your local setup and the Hugging Face API.

 

This step-by-step guide provides a thorough integration process between Hugging Face and GitHub, covering setting up the environment, using the Transformers library, and leveraging the Inference API. Feel free to expand upon this workflow according to your specific needs and use cases.

Omi Necklace

The #1 Open Source AI necklace: Experiment with how you capture and manage conversations.

Build and test with your own Omi.

How to Use Hugging Face with GitHub: Usecases

 

Integrating Hugging Face Models with GitHub Actions

 

  • Host and version your machine learning models using Hugging Face's model repository, which allows seamless management and deployment of models.
  •  

  • Create a GitHub repository to store your application's source code and any relevant scripts for integrating and using the Hugging Face models.

 

git init
git add .
git commit -m "Initial commit with application source code"

 

Automate Model Deployment with GitHub Actions

 

  • Set up a GitHub Actions workflow file in your repository to automate the process of testing and deploying your application with the Hugging Face model.
  •  

  • Utilize Hugging Face's inference endpoint to directly pull the model for inference during the automated workflow, reducing the need for manual updates.

 

name: Deploy Application

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        
      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: '3.x'
      
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
      
      - name: Run Deploy Script
        run: python deploy_with_huggingface.py

 

Monitor and Improve the Workflow

 

  • Regularly review the logs and feedback from GitHub Actions runs to identify improvements in the deployment and integration processes.
  •  

  • Collaborate and manage the development process efficiently through GitHub Issues and Projects, aligning your model development lifecycle with your application development timeline.

 

# Review logs using the GitHub Actions console
# Discuss model performance improvements in GitHub Issues

 

 

Collaborative Model Training and Deployment

 

  • Utilize Hugging Face's `datasets` library to load and preprocess datasets directly within your model training scripts, allowing for efficient collaborative development.
  •  

  • Store your training scripts and configurations in a shared GitHub repository, enabling multiple collaborators to contribute to the model training process efficiently.

 

from datasets import load_dataset

dataset = load_dataset('imdb')

 

Version Control with GitHub

 

  • Leverage GitHub's version control capabilities to track changes made to your Hugging Face model training and experimentation scripts.
  •  

  • Create branches for different experimentations or model improvements, and merge successful changes to the main branch for stable deployments.

 

git checkout -b improve-model

 

Continuous Integration for Model Quality

 

  • Set up continuous integration workflows using GitHub Actions to automatically test model changes and ensure high-quality outputs with each commit.
  •  

  • Use these workflows to run unit tests, data validation checks, and model evaluation scripts to maintain a robust development lifecycle.

 

name: Model Testing Workflow

on: [pull_request]

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2
        
      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: '3.x'
      
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
      
      - name: Run Tests
        run: pytest tests/

 

Deploy and Use Trained Models

 

  • Deploy trained models on Hugging Face's model hub for easy access and integration into your applications, streamlining model usage across different platforms.
  •  

  • Document the model usage in the GitHub repository to guide fellow developers on how to efficiently use and incorporate the models into their solutions.

 

```md

Access models from Hugging Face Model Hub

Follow integration guidelines in README.md

```

 

Omi App

Fully Open-Source AI wearable app: build and use reminders, meeting summaries, task suggestions and more. All in one simple app.

Github →

OMI NECKLACE + OMI APP
First & only open-source AI wearable platform

a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded
a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded a person looks into the phone with an app for AI Necklace, looking at notes Friend AI Wearable recorded
online meeting with AI Wearable, showcasing how it works and helps online meeting with AI Wearable, showcasing how it works and helps
online meeting with AI Wearable, showcasing how it works and helps online meeting with AI Wearable, showcasing how it works and helps
App for Friend AI Necklace, showing notes and topics AI Necklace recorded App for Friend AI Necklace, showing notes and topics AI Necklace recorded
App for Friend AI Necklace, showing notes and topics AI Necklace recorded App for Friend AI Necklace, showing notes and topics AI Necklace recorded