Skip to content

Commit

Permalink
feat(hfsup): new integration, added back airi deploy to HF Spaces (#6)
Browse files Browse the repository at this point in the history
* fix: pwa issue, special `TARGET_HUGGINGFACE_SPACE` for hf spaces, half width issue
  • Loading branch information
nekomeowww authored Dec 26, 2024
1 parent 0eb72d9 commit de29371
Show file tree
Hide file tree
Showing 25 changed files with 836 additions and 104 deletions.
56 changes: 31 additions & 25 deletions .github/workflows/deploy-huggingface-spaces.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ jobs:
HF_TOKEN: ${{ secrets.HF_TOKEN }}
HF_USERNAME: ${{ secrets.HF_USERNAME }}
# - run: |-
# git clone https://$HF_USERNAME:[email protected]/spaces/moeru-ai/airi --depth 1 packages/stage/dist
# env:
# HF_TOKEN: ${{ secrets.HF_TOKEN }}
# HF_USERNAME: ${{ secrets.HF_USERNAME }}
- run: |-
git clone https://$HF_USERNAME:[email protected]/spaces/moeru-ai/airi --depth 1 packages/stage/dist
env:
HF_TOKEN: ${{ secrets.HF_TOKEN }}
HF_USERNAME: ${{ secrets.HF_USERNAME }}
- uses: pnpm/action-setup@v3
- uses: actions/setup-node@v4
Expand All @@ -40,11 +40,14 @@ jobs:

- run: pnpm install
- run: pnpm build
env:
TARGET_HUGGINGFACE_SPACE: 'true'
VITE_APP_TARGET_HUGGINGFACE_SPACE: 'true'

- id: moonshine_web_diff
working-directory: ./packages/moonshine-web/dist
run: |-
mv ../README.md .
git lfs ls-files --all
git add .
if [[ -n $(git status --porcelain) ]]; then
echo "changes=true" >> "$GITHUB_OUTPUT";
Expand All @@ -59,12 +62,13 @@ jobs:
git config --local user.email "[email protected]"
git config --local user.name "Neko Ayaka"
git commit -m "release: build ${{ github.sha }}"
git lfs push origin main --all
git push -f
- id: whisper_webgpu_diff
working-directory: ./packages/whisper-webgpu/dist
run: |-
mv ../README.md .
git lfs ls-files --all
git add .
if [[ -n $(git status --porcelain) ]]; then
echo "changes=true" >> "$GITHUB_OUTPUT";
Expand All @@ -79,24 +83,26 @@ jobs:
git config --local user.email "[email protected]"
git config --local user.name "Neko Ayaka"
git commit -m "release: build ${{ github.sha }}"
git lfs push origin main --all
git push -f
# - id: airi_diff
# working-directory: ./packages/stage/dist
# run: |-
# mv ../README.md .
# git add .
# if [[ -n $(git status --porcelain) ]]; then
# echo "changes=true" >> "$GITHUB_OUTPUT";
# fi
- id: airi_diff
working-directory: ./packages/stage/dist
run: |-
git lfs ls-files --all
git add .
if [[ -n $(git status --porcelain) ]]; then
echo "changes=true" >> "$GITHUB_OUTPUT";
fi
# - if: steps.airi_diff.outputs.changes == 'true'
# working-directory: ./packages/stage/dist
# env:
# HF_TOKEN: ${{ secrets.HF_TOKEN }}
# HF_USERNAME: ${{ secrets.HF_USERNAME }}
# run: |-
# git config --local user.email "[email protected]"
# git config --local user.name "Neko Ayaka"
# git commit -m "release: build ${{ github.sha }}"
# git push -f
- if: steps.airi_diff.outputs.changes == 'true'
working-directory: ./packages/stage/dist
env:
HF_TOKEN: ${{ secrets.HF_TOKEN }}
HF_USERNAME: ${{ secrets.HF_USERNAME }}
run: |-
git config --local user.email "[email protected]"
git config --local user.name "Neko Ayaka"
git commit -m "release: build ${{ github.sha }}"
git lfs push origin main --all
git push -f
4 changes: 4 additions & 0 deletions cspell.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ words:
- elevenlabs
- formkit
- gltf
- hfspaceup
- hfsup
- histoire
- hiyori
- huggingface
- huggingspace
- iconify
- intlify
- kwaa
Expand Down Expand Up @@ -71,6 +74,7 @@ words:
- unocss
- unplugin
- valibot
- VITE
- vrma
- vueuse
- webgpu
Expand Down
52 changes: 52 additions & 0 deletions packages/hfsup/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<h1 align="center">🧑‍🚀 hfsup</h1>

> `hfsup` stands for HuggingFace Space up, where the `up` is coming from `rollup`, `tsup`, you may think of "to make your work up and running".
>
> A collection of tools to help you deploy, bundle HuggingFace Spaces and related assets with ease.
```shell
pnpm i hfsup -D
```

```ts
import { LFS, SpaceCard } from 'hfsup/vite'
import { defineConfig } from 'vite'

export default defineConfig({
plugins: [
LFS(),
SpaceCard({
title: 'Real-time Whisper WebGPU (Vue)',
emoji: '🎤',
colorFrom: 'gray',
colorTo: 'green',
sdk: 'static',
pinned: false,
license: 'mit',
models: ['onnx-community/whisper-base'],
short_description: 'Yet another Real-time Whisper with WebGPU, written in Vue',
thumbnail: 'https://raw.githubusercontent.com/moeru-ai/airi/refs/heads/main/packages/whisper-webgpu/public/banner.png'
})
]
})
```

After bundling, a dedicated README with merged front-matter header will be generated in the root of your project:

```md
---
title: Real-time Whisper WebGPU (Vue)
emoji: 🎤
colorFrom: gray
colorTo: green
sdk: static
pinned: false
license: mit
models:
- onnx-community/whisper-base
short_description: Yet another Real-time Whisper with WebGPU, written in Vue
thumbnail: https://raw.githubusercontent.com/moeru-ai/airi/refs/heads/main/packages/whisper-webgpu/public/banner.png
---

# Real-time Whisper WebGPU (Vue)
```
42 changes: 42 additions & 0 deletions packages/hfsup/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "hfsup",
"type": "module",
"private": false,
"description": "A collection of tools to help you deploy, bundle HuggingFace Spaces and related assets with ease.",
"author": {
"name": "Neko Ayaka",
"email": "[email protected]",
"url": "https://github.com/nekomeowww"
},
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/moeru-ai/airi.git",
"directory": "packages/huggingspace"
},
"sideEffects": false,
"exports": {
"./vite": {
"types": "./dist/vite/index.d.ts",
"import": "./dist/vite/index.mjs",
"node": "./dist/vite/index.cjs"
}
},
"main": "./dist/vite/index.cjs",
"module": "./dist/vite/index.mjs",
"types": "./dist/vite/index.d.ts",
"files": [
"README.md",
"dist",
"package.json"
],
"scripts": {
"dev": "pnpm run stub",
"stub": "unbuild --stub",
"build": "unbuild"
},
"dependencies": {
"gray-matter": "^4.0.3",
"vite": "^6.0.5"
}
}
25 changes: 25 additions & 0 deletions packages/hfsup/src/utils/fs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { stat } from 'node:fs/promises'

export async function exists(path: string) {
try {
await stat(path)
return true
}
catch (error) {
if (isENOENTError(error))
return false

throw error
}
}

export function isENOENTError(error: unknown): boolean {
if (!(error instanceof Error))
return false
if (!('code' in error))
return false
if (error.code !== 'ENOENT')
return false

return true
}
2 changes: 2 additions & 0 deletions packages/hfsup/src/vite/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './lfs'
export * from './space-card'
74 changes: 74 additions & 0 deletions packages/hfsup/src/vite/lfs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import type { Plugin, ResolvedConfig } from 'vite'

const defaultGitAttributes = `# Default
*.7z filter=lfs diff=lfs merge=lfs -text
*.arrow filter=lfs diff=lfs merge=lfs -text
*.bin filter=lfs diff=lfs merge=lfs -text
*.bz2 filter=lfs diff=lfs merge=lfs -text
*.ckpt filter=lfs diff=lfs merge=lfs -text
*.ftz filter=lfs diff=lfs merge=lfs -text
*.gz filter=lfs diff=lfs merge=lfs -text
*.h5 filter=lfs diff=lfs merge=lfs -text
*.joblib filter=lfs diff=lfs merge=lfs -text
*.lfs.* filter=lfs diff=lfs merge=lfs -text
*.mlmodel filter=lfs diff=lfs merge=lfs -text
*.model filter=lfs diff=lfs merge=lfs -text
*.msgpack filter=lfs diff=lfs merge=lfs -text
*.npy filter=lfs diff=lfs merge=lfs -text
*.npz filter=lfs diff=lfs merge=lfs -text
*.onnx filter=lfs diff=lfs merge=lfs -text
*.ot filter=lfs diff=lfs merge=lfs -text
*.parquet filter=lfs diff=lfs merge=lfs -text
*.pb filter=lfs diff=lfs merge=lfs -text
*.pickle filter=lfs diff=lfs merge=lfs -text
*.pkl filter=lfs diff=lfs merge=lfs -text
*.pt filter=lfs diff=lfs merge=lfs -text
*.pth filter=lfs diff=lfs merge=lfs -text
*.rar filter=lfs diff=lfs merge=lfs -text
*.safetensors filter=lfs diff=lfs merge=lfs -text
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
*.tar.* filter=lfs diff=lfs merge=lfs -text
*.tar filter=lfs diff=lfs merge=lfs -text
*.tflite filter=lfs diff=lfs merge=lfs -text
*.tgz filter=lfs diff=lfs merge=lfs -text
*.wasm filter=lfs diff=lfs merge=lfs -text
*.xz filter=lfs diff=lfs merge=lfs -text
*.zip filter=lfs diff=lfs merge=lfs -text
*.zst filter=lfs diff=lfs merge=lfs -text
*tfevents* filter=lfs diff=lfs merge=lfs -text
`

export function LFS(options?: {
enableFn?: (config: ResolvedConfig) => boolean | Promise<boolean>
extraGlobs?: string[]
extraAttributes?: string[]
withDefault?: boolean
}): Plugin {
let _config: ResolvedConfig

return {
name: 'huggingspace:lfs-gitattributes',
configResolved(config) {
_config = config
},
async generateBundle() {
if (options?.enableFn && !(await options.enableFn(_config))) {
return
}

const extraGlobs = options?.extraGlobs ?? []
const extraAttributes = options?.extraAttributes ?? []
const withDefault = options?.withDefault ?? true
const gitAttributes = withDefault ? defaultGitAttributes : ''
const extraGlobsIntoGitAttributes = extraGlobs.map((glob) => {
return `${glob} filter=lfs diff=lfs merge=lfs -text`
})

this.emitFile({
type: 'asset',
fileName: '.gitattributes',
source: `${extraAttributes.join('\n')}${extraGlobsIntoGitAttributes.join('\n')}\n${gitAttributes}`,
})
},
}
}
Loading

0 comments on commit de29371

Please sign in to comment.