在 Hugging Face Spaces 中使用 Writer Framework
Writer Framework 是一个免费的开源框架,用于快速轻松地创建 AI 应用程序。使用可视化、拖放编辑器构建用户界面;用 Python 编写后端代码。它快速灵活,语法简洁、易于测试。它还提供了 UI 和业务逻辑之间的关注点分离,从而支持更复杂的应用程序。
本指南将引导你完成在 Hugging Face Spaces 上设置 Writer Framework 应用程序的过程。
先决条件
- 一个 Hugging Face 账户
- 本地机器上安装了 Git
- 熟悉基本的命令行操作
创建 Writer Framework 应用程序
如果你还没有安装 Writer Framework,请先安装
pip install writer
我们建议使用虚拟环境。
创建你的应用程序
要创建新应用程序,请运行
writer create your-app-name
如果你想立即开始,可以使用演示应用程序
writer hello
你可以查看我们的文档和教程,了解更多关于使用 Writer Framework 进行构建的信息。
可选:创建 Writer Framework API 密钥
你可以免费使用 Writer Framework,并像在任何其他 Python 框架中一样集成 Hugging Face 上可用的任何模型。但是,如果你计划使用 Writer AI 模块(它使用我们的 API 和 Palmyra LLM),你需要注册 Writer AI Studio 并创建一个新的 Framework 应用程序。这将生成一个 API 密钥。你稍后将在 Hugging Face 中将其设置为一个密钥。
创建 Hugging Face Space
首先,创建你的 Hugging Face Space
- 登录你的 Hugging Face 账户。
- 导航到 Spaces 部分。
- 点击“创建新 Space”。
- 为你的 Space 选择一个名称和表情符号,并选择适当的设置,例如 Space 硬件和隐私。
- 对于 Space SDK,选择“Docker”,然后选择“空白”模板。
记下用于将你的 Space 克隆到本地机器的 git 命令。
可选:配置环境变量密钥
如果你创建了 Writer Framework API 密钥以使用 AI 模块,你需要将其添加到你的 Space 设置中。在 Hugging Face Space 设置选项卡中,添加一个新的环境变量密钥
- 键:WRITER_API_KEY
- 值:你的 Writer Framework API 密钥
生成 Hugging Face 认证令牌
要将代码推送到 Hugging Face,你需要一个认证令牌。以下是创建方法
- 进入你的 Hugging Face 账户设置。
- 导航到“访问令牌”部分。
- 点击“创建新令牌”并生成一个新的认证令牌。
- 安全地保存此令牌;你将需要它进行 Git 操作,并且以后将无法再次看到它。
克隆 Hugging Face Space 仓库
在你的终端中执行以下命令
git clone https://huggingface.co/spaces/username/space_name
将 username
替换为你的 Hugging Face 用户名,将 space_name
替换为你为你的空间选择的名称。
当系统提示时
- 使用你的 Hugging Face 用户名作为用户名
- 使用生成的认证令牌作为密码
准备你的应用程序
你现在可以准备你的 Writer Framework 应用程序进行部署了。
首先,准备文件
- 导航到克隆的仓库目录。
- 将你的整个 Writer Framework 应用程序文件夹复制到克隆的仓库中。
- 将
pyproject.toml
文件从应用程序文件夹移动到克隆仓库的顶层。
创建 Dockerfile
Hugging Face Spaces 使用 Docker 部署应用程序。在应用程序文件夹的根目录中,创建一个名为 Dockerfile
的文件。
将以下内容粘贴到 Dockerfile 中,并确保更改末尾的应用程序目录名称
# Build stage
FROM python:3.11-slim-buster AS Build
# Set environment variables for Python and Poetry
ENV PYTHONUNBUFFERED=1 \
PIP_NO_CACHE_DIR=1 \
POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_CREATE=false \
POETRY_VERSION=1.7.1
# Set the working directory in the container
WORKDIR /app
# Copy the dependencies file to the working directory
COPY ./pyproject.toml /app/
# Update, install dependencies, and prepare the Python environment
RUN apt-get update && \
apt-get install -y gcc g++ unixodbc-dev && \
pip install "poetry==$POETRY_VERSION" && \
poetry export --without-hashes --format requirements.txt --output requirements.txt && \
python3 -m pip wheel --no-cache-dir --no-deps -w /app/wheels -r requirements.txt
# Runtime stage
FROM python:3.11-slim-buster AS Run
# Set environment variables for Python and Poetry
ENV HOME=/home/user \
PATH=/home/user/.local/bin:$PATH
# Create a non-root user
RUN useradd -m -u 1000 user
# Switch to the non-root user
USER user
# Copy wheel files from the build stage
COPY --from=build /app/wheels $HOME/app/wheels
# Set the working directory to where the wheels are
WORKDIR $HOME/app/wheels
# Install the wheel files
RUN pip3 --no-cache-dir install *.whl
# Change app name here to copy the application files to the working directory
COPY --chown=user ./your-app-name $HOME/app
# Set the working directory to the application files
WORKDIR $HOME/app
# Specify the command to run the application
ENTRYPOINT [ "writer", "run" ]
# Expose the port the app runs on
EXPOSE 8080
# Set the default command to run the app
CMD [ ".", "--port", "8080", "--host", "0.0.0.0" ]
可根据需要进行调整。
在 README 中暴露必要的端口
在你的 Space 的 README 文件的 frontmatter 中,添加有关应用程序使用的端口的信息
app_port: 8080
部署应用程序
要部署应用程序,你只需将更改推送到仓库。
在终端中,执行以下命令
git add .
git commit -m "Initial setup of Writer Framework"
git push
Space 将根据你推送的文件自动构建和部署你的应用程序。你可以通过 Hugging Face Spaces 界面访问已部署的应用程序。
故障排除
如果你遇到任何问题
- 仔细检查你的认证令牌,并确保它具有必要的权限。
- 验证所有必需的文件都存在于你的仓库中。特别检查
pyproject.toml
是否位于目录的根目录,而不是在你的应用程序文件夹内。 - 检查你是否更改了 Dockerfile 中的应用程序目录名称。
- 检查 Hugging Face Space 中的构建日志,查找任何错误消息。
有关 Hugging Face Spaces 的更多信息,请参阅官方文档。
结论
你的 Writer Framework 应用程序现在已设置为 Hugging Face Space。恭喜!🎉 我们很想看看你的作品。请在 Hugging Face、Twitter 或 LinkedIn 上联系我,让我知道!