Tutorials / pages /2_202_Access_to_HuggingFace_with_Notebook.py
MrJShen's picture
Update pages/2_202_Access_to_HuggingFace_with_Notebook.py
0c5af02
import streamlit as st
from utils import *
if True:
st.markdown("""
## Notebook to Hugging Face Space
##### 1. Install huggingface_hub
```python
!pip install huggingface_hub==0.15.0
```
##### 2. Login to Huggingface with notebook
```python
from huggingface_hub import login
import os
from dotenv import load_dotenv
load_dotenv()
HF_API_KEY = os.getenv('HF_API_KEY') ## This should be set up last week :)
login(token = HF_API_KEY) # You might need to execute this cell twice sometimes
```
The code above have two parts:
1. Import the method, called "login" from the library we just installed; this is the most common way in Python to import library or methods from library.
2. Call the method by passing a parameter "token = ". This token is the **Personal Token** when you sign up Hugging Face.
##### 3. Download a repo from our code space
```python
from huggingface_hub import snapshot_download # This time we use another method
# Call the method and passing three parameters into it
# The 1st parameter is repo_id, which is formed by "Organization Name / Space name", in our case of the tutorial folder,
# it is "TGSAI/Tutorials"
# The 2nd parameter is location(path) on your computer you want the folder to be saved
# The 3rd parameter is repo type, in our case, it is 'space'
snapshot_download(repo_id="TGSAI/Tutorials",
local_dir = "./Tutorials",
repo_type='space',
revision = 'week2') ## We will create branch per week
```
##### 4. Editing our loacl codes
After you download repo from our space, you can edit and testing those files on you own computer, the location/path of the folder is show as the output above.
- if it is a jupyter notebook file, i.e. with ".ipynb" as file extension, you can open that file in the Jupyter Notebook directly
- it it is other form, such as ".py" as extension, you can use [vscode.dev](https://vscode.dev/) as the IDE to edit the file on your machine, or for the sake of simplicity, you can edit them directly in the notebook server by click the file.
- More detailed instruction of vscode.dev can be checked [here](https://google.com)
- For this week, you might only need to edit the app.py file just inside the /Tutorial folder you have just downloaded
- find the section "Our member profile"
- replace the "coming soon ..." with your name and some intro about you
- you should always download the latest version in each branch before you edit code
To check existing branch, using the code snippet below:
```python
from huggingface_hub import HfApi
api = HfApi()
api.list_repo_refs(repo_id = "TGSAI/Tutorials", repo_type = "space")
```
##### 5. Update your changes to the specific branch
1. If you have edited the app.py file with your profile, the change already happened in your local code folder;
2. For simplicity, you will upload the updated folder "Tutorials" to the "main" branch;
2.5. But before you commit change, you should **always** update your local code with newest change at our code space, by:
```python
from huggingface_hub import Repository
repo = Repository(local_dir="<Your local folder>", clone_from="<The HF folder>", revision='main')
repo.git_pull(rebase=True) # This will update your local folder
```
3. We will check your commit and merge it mannualy from HF web interface
Using the code snippet below to update your changes,
- You will need to do detailed editing and check before you submit it, otherwise, your pull request would not be accepted
```python
api.upload_folder(folder_path="./Tutorials", # the folder path at the local machine
repo_id="TGSAI/Tutorials",
repo_type="space",
revision = 'main', # make sure upload to the main branch
commit_message = 'https://app.clickup.com/t/860r79ppd', # add comment, mostly link to Clickup ticket url
commit_description = '', # any discription, or omitted
create_pr = True # Important! You must create Pull Request, then the maintainer will need to check it.
)
```
""")