Skip to content

Commit

Permalink
feat: ✨ 支持Huggingface Space部署方式使用Cloudflare Tunnels自定义访问域名 (#707)
Browse files Browse the repository at this point in the history
* feat: ✨ 支持Huggingface Space部署方式使用Cloudflare Tunnels自定义访问域名

* 在中英文之间添加空格

* 添加空格

* 删除无关文件

* Update Dockerfile

* 将cloudflared日志输出到标准输出

* 优化Dockerfile

* 将日志输出到标准输出
  • Loading branch information
Hoshino-Yumetsuki authored May 28, 2024
1 parent b1f3c86 commit 20203bb
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 1 deletion.
23 changes: 22 additions & 1 deletion docs/backend.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
| [Railway 部署](#railway-部署) | ★★☆☆☆ | 有免费额度但不足以支持一个月连续运行,部署简单,适合全球访问。 |
| [Zeabur 部署](#zeabur-部署) | ★☆☆☆☆ | 需要绑定支付宝或信用卡,部署简单,适合中国大陆访问,免费计划环境随时可能会被删除。 |
| [Netlify 部署](#netlify-部署) | ★★★★☆ | 有充足的免费额度,中国大陆访问速度不错。 |
| [Hugging Face 部署](#hugging-face-部署) | ★★★★☆ | 免费,中国大陆访问速度不错。 |
| [Hugging Face 部署](#hugging-face-部署) | ★★★★☆ | 免费,中国大陆访问速度不错。允许通过 Cloudflare Tunnels 自定义域名。 |
| [AWS Lambda 部署](#aws-lambda-部署) | ★★★☆☆ | 全球最大的云平台,适合已经使用 AWS 全家桶的用户。 |
| [Cloudflare workers 部署](#cloudflare-workers-部署) | ★★☆☆☆ | 部署需使用命令行,冷启动时间较短,功能有部分限制。 |
| [私有部署](#私有部署) | ★★☆☆☆ | 适用于有服务器的用户,需要自行申请 HTTPS 证书。 |
Expand Down Expand Up @@ -208,6 +208,27 @@ EXPOSE 7860

![](./static/hugging-6.png)

### 如果你需要自定义域名

> ps:除了 `CF_ZERO_TRUST_TOKEN` 这个环境变量以外,其他环境变量的配置方式与上一步相同
1. 申请 Cloudflare Zero Trust,关于申请方式请自行查找

![](./static/hugging-7.png)

2. 添加一条隧道,连接方式选择 Cloudflared,名称任意

![](./static/hugging-8.png)

3.添加一个 Public Hostname,回源选择 HTTP,端口选择8080
4.Clone Twikoo 仓库,找到src\server\hf-space
5.去 Huggingface 创建一个 Space,然后Clone下来,将 hf-space 文件夹内的所有内容复制进去
6.在 Huggingface Space 的设置中添加一个环境变量,变量名 `CF_ZERO_TRUST_TOKEN` ,值是 Tunnels 给的令牌(删掉 `cloudflared.exe service install`,只保留令牌部分)

![](./static/hugging-9.png)

7.Push 到 Huggingface Space 仓库

## AWS Lambda 部署

1. 注册 AWS 账号并配置 Terraform CLI。
Expand Down
Binary file added docs/static/hugging-7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/static/hugging-8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/static/hugging-9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions src/server/hf-space/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM node:lts-slim AS build

RUN useradd -m app

USER root

RUN npm i -g tkserver

ADD https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64 /usr/local/bin/cloudflared
RUN chmod +x /usr/local/bin/cloudflared

COPY ./src/start.sh /usr/bin/start.sh

USER app
WORKDIR /home/app/twikoo

CMD ["sh", "/usr/bin/start.sh"]
9 changes: 9 additions & 0 deletions src/server/hf-space/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Twikoo Huggingface Space
emoji: 📚
colorFrom: yellow
colorTo: indigo
sdk: docker
pinned: false
app_port: 8080
---
4 changes: 4 additions & 0 deletions src/server/hf-space/src/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

nohup /usr/local/bin/cloudflared tunnel --no-autoupdate run --token $CF_ZERO_TRUST_TOKEN >> /dev/stdout 2>&1 &
tkserver

0 comments on commit 20203bb

Please sign in to comment.