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
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.