├── .github └── workflows │ └── llamafile.yml ├── README.md ├── hf.py └── requirements.txt /.github/workflows/llamafile.yml: -------------------------------------------------------------------------------- 1 | name: Build llamafile 2 | 3 | on: 4 | workflow_dispatch: 5 | inputs: 6 | model_url: 7 | description: "URL to the model to be used" 8 | required: true 9 | 10 | huggingface_repo: 11 | description: "HuggingFace repo to upload to. Example: rabil/llamafile. It is assumed that you have added your HuggingFace token with write access to your action secrets." 12 | required: true 13 | 14 | 15 | jobs: 16 | build: 17 | runs-on: ubuntu-latest 18 | steps: 19 | - name: Download Llamafile latest 20 | run: | 21 | mkdir -p /usr/local/bin 22 | TAG=$(curl -s "https://api.github.com/repos/Mozilla-Ocho/llamafile/tags" | grep '"name":' | sed -E 's/.*"([^"]+)".*/\1/' | head -n 1) 23 | LLAMAFILE_FILE_URL="https://github.com/Mozilla-Ocho/llamafile/releases/download/$TAG/llamafile-$TAG" 24 | ZIPALIGN_FILE_URL="https://github.com/Mozilla-Ocho/llamafile/releases/download/$TAG/zipalign-$TAG" 25 | LLAMAFILE_FILE="/usr/local/bin/llamafile" 26 | ZIPALIGN_FILE="/usr/local/bin/zipalign" 27 | 28 | wget -O "$LLAMAFILE_FILE" "$LLAMAFILE_FILE_URL" 29 | wget -O "$ZIPALIGN_FILE" "$ZIPALIGN_FILE_URL" 30 | chmod +x "$LLAMAFILE_FILE" 31 | chmod +x "$ZIPALIGN_FILE" 32 | 33 | mkdir -p llamafile 34 | 35 | - uses: actions/checkout@v4 36 | with: 37 | path: "llamafile-builder" 38 | 39 | # Add build cache 40 | - uses: actions/cache@v3 41 | with: 42 | path: | 43 | ~/.cache/ 44 | key: ${{ runner.os }}-cache 45 | restore-keys: | 46 | ${{ runner.os }}- 47 | 48 | - name: llamafile gotchas error 49 | run: | 50 | sudo wget -O /usr/bin/ape https://cosmo.zip/pub/cosmos/bin/ape-$(uname -m).elf 51 | sudo chmod +x /usr/bin/ape 52 | sudo sh -c "echo ':APE:M::MZqFpD::/usr/bin/ape:' >/proc/sys/fs/binfmt_misc/register" 53 | sudo sh -c "echo ':APE-jart:M::jartsr::/usr/bin/ape:' >/proc/sys/fs/binfmt_misc/register" 54 | 55 | - name: Find model name 56 | id: model_name 57 | run: | 58 | modeln=$(echo ${{ github.event.inputs.model_url }} | grep -o '[^/]*$' | sed 's/?download=true//') 59 | echo "model=$modeln" >> $GITHUB_OUTPUT 60 | 61 | - name: Download model 62 | run: | 63 | cd llamafile 64 | wget -O ${{ steps.model_name.outputs.model }} ${{ github.event.inputs.model_url }} 65 | 66 | - name: Create .args 67 | run: | 68 | cd llamafile 69 | echo "-m" > .args 70 | echo "${{ steps.model_name.outputs.model }}" >> .args 71 | echo "--host" >> .args 72 | echo "0.0.0.0" >> .args 73 | echo "..." >> .args 74 | 75 | - name: Prepare llamafile 76 | run: | 77 | # remove extension from model name 78 | cd llamafile 79 | file_ext_removed_model_name=$(echo ${{ steps.model_name.outputs.model }} | sed 's/\.[^.]*$//') 80 | cp /usr/local/bin/llamafile "$file_ext_removed_model_name".llamafile 81 | zipalign -j0 "$file_ext_removed_model_name".llamafile ${{ steps.model_name.outputs.model }} .args 82 | 83 | - name: Setup python 84 | uses: actions/setup-python@v5 85 | with: 86 | python-version: "3.11" 87 | cache: "pip" 88 | 89 | - name: Install dependencies 90 | run: | 91 | python -m pip install huggingface_hub 92 | 93 | - name: Upload to HuggingFace 94 | env: 95 | HF_TOKEN: ${{ secrets.HF_TOKEN }} 96 | run: | 97 | cp llamafile-builder/hf.py llamafile/hf.py 98 | cd llamafile 99 | file_ext_removed_model_name=$(echo ${{ steps.model_name.outputs.model }} | sed 's/\.[^.]*$//') 100 | python3 hf.py ${{ github.event.inputs.model_url }} ${{ github.event.inputs.huggingface_repo }} "$file_ext_removed_model_name".llamafile 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # llamafile-builder 2 | 3 | A simple github actions script to build a llamafile from a given gguf file download url and uploads it to huggingface repo. 4 | 5 | llamafile lets you distribute and run LLMs with a single file. [announcement blog post](https://hacks.mozilla.org/2023/11/introducing-llamafile/) 6 | 7 | ## Inputs 8 | 9 | ### `model_url` 10 | 11 | **Required** The url to the gguf file to download. 12 | 13 | ### `huggingface_repo` 14 | 15 | **Required** The huggingface repo to upload the llamafile to. 16 | 17 | Add your huggingface token with write access to the repo as a actions secret with the name `HF_TOKEN`. 18 | 19 | ## Usage 20 | 21 | 1. Head over to the actions tab. 22 | 2. Select the action `Build llamafile` 23 | 3. Fill in the required inputs. 24 | 4. Click on `Run workflow` and wait for the action to complete. 25 | 5. Check your huggingface repo for the llamafile. -------------------------------------------------------------------------------- /hf.py: -------------------------------------------------------------------------------- 1 | import sys, os, re 2 | from huggingface_hub import HfApi,login, logout 3 | api = HfApi() 4 | 5 | file_url = sys.argv[1] 6 | repo = sys.argv[2] 7 | file = sys.argv[3] 8 | 9 | token = os.environ.get("HF_TOKEN", None) 10 | 11 | def extract_repo_id(file_url): 12 | pattern = r'huggingface\.co/([^/]+)/([^/]+)' 13 | match = re.search(pattern, file_url) 14 | if match: 15 | return f"{match.group(1)}/{match.group(2)}" 16 | else: 17 | return None 18 | 19 | login(token=token, write_permission=True) 20 | 21 | api.create_repo( 22 | repo_id=repo, 23 | private=False, 24 | exist_ok=True, 25 | repo_type="model" 26 | ) 27 | 28 | api.upload_file( 29 | path_or_fileobj=file, 30 | path_in_repo=file, 31 | repo_id=repo, 32 | repo_type="model", 33 | ) 34 | 35 | repo_files = api.list_repo_files(repo, repo_type="model") 36 | 37 | llama_files = [] 38 | 39 | for ifile in repo_files: 40 | if ".llamafile" in ifile: 41 | llama_files.append(f"\n - [{ifile}](https://huggingface.co/{repo}/resolve/main/{ifile})") 42 | 43 | # Create README.md 44 | README_CONTENT = f""" 45 | --- 46 | tags: 47 | - llamafile 48 | - GGUF 49 | base_model: {extract_repo_id(file_url)} 50 | --- 51 | ## {"".join(repo.split("/")[1:])} 52 | 53 | llamafile lets you distribute and run LLMs with a single file. [announcement blog post](https://hacks.mozilla.org/2023/11/introducing-llamafile/) 54 | 55 | #### Downloads 56 | {''.join(llama_files)} 57 | 58 | This repository was created using the [llamafile-builder](https://github.com/rabilrbl/llamafile-builder) 59 | """ 60 | 61 | with open("HF_README.md", "w") as f: 62 | f.write(README_CONTENT) 63 | 64 | api.upload_file( 65 | path_or_fileobj="HF_README.md", 66 | path_in_repo="README.md", 67 | repo_id=repo, 68 | repo_type="model", 69 | ) 70 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | huggingface_hub --------------------------------------------------------------------------------