Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 更新项目结构以支持 pypi 包发布 & 版本更新 #1403

Merged
merged 10 commits into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 0 additions & 69 deletions .github/quickstarts/windows/scripts/初始化.cmd

This file was deleted.

2 changes: 1 addition & 1 deletion .github/quickstarts/windows/scripts/启动.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ TITLE [Kirara AI] AI 系统正在运行...

SET PATH=%cd%\python3.11;%cd%\ffmpeg\bin;%PATH%

cd kirara_ai && python main.py
python -m kirara_ai

TITLE [Kirara AI] AI 系统已停止运行

Expand Down
152 changes: 81 additions & 71 deletions .github/workflows/quickstart-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,109 +3,119 @@ name: Windows Quickstart
on:
workflow_dispatch:
push:
branches:
- 'browser-version-dev'
- 'master'
- 'dev'
- 'refactoring-v3-mvp'
tags:
- '**'
branches: ['browser-version-dev', 'master', 'dev', 'refactoring-v3-mvp']
tags: ['**']
pull_request:
branches:
- 'master'
- 'dev'
- 'refactoring-v3-mvp'
branches: ['master', 'dev', 'refactoring-v3-mvp']

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

env:
PYTHON_VERSION: "3.11.2"
DIST_DIR: "C:/dist"
BUILD_DIR: "C:/build"
PACKAGE_NAME: "quickstart-windows-kirara-ai-amd64"

jobs:
build:
name: Windows Quickstart
runs-on: Windows-latest

runs-on: windows-latest
steps:
- name: Checkout
- name: Checkout repository
uses: actions/checkout@v4
- name: Generate files
run: |
mkdir C:/generated_files
mkdir C:/tmp_files
echo "Creating folders..."
cd C:/generated_files
mkdir kirara_ai
mkdir ffmpeg

cp -r D:\a\chatgpt-mirai-qq-bot\chatgpt-mirai-qq-bot\* C:\generated_files\kirara_ai\
- name: Set up Python for building
uses: actions/setup-python@v5
with:
python-version: '3.11'
cache: 'pip'

echo "Downloading ffmpeg ..."
Invoke-WebRequest https://www.gyan.dev/ffmpeg/builds/packages/ffmpeg-7.0.2-essentials_build.7z -OutFile C:/tmp_files/ffmpeg.7z
echo "Appreciate the contribution of gyan.dev"
- name: Build wheel package
run: |
python -m pip install build
python -m build
# 获取生成的wheel文件名
$WheelFile = Get-ChildItem -Path "dist" -Filter "*.whl" | Select-Object -First 1 -ExpandProperty Name
echo "WHEEL_FILE=$WheelFile" >> $env:GITHUB_ENV

7z x C:/tmp_files/ffmpeg.7z -r -oC:/generated_files/ffmpeg
- name: Prepare distribution environment
run: |
# 创建必要的目录
mkdir ${{ env.DIST_DIR }}
mkdir ${{ env.BUILD_DIR }}

# 下载嵌入式Python
$pythonUrl = "https://www.python.org/ftp/python/${{ env.PYTHON_VERSION }}/python-${{ env.PYTHON_VERSION }}-embed-amd64.zip"
Invoke-WebRequest -Uri $pythonUrl -OutFile "${{ env.BUILD_DIR }}/python.zip"
Expand-Archive "${{ env.BUILD_DIR }}/python.zip" -DestinationPath "${{ env.DIST_DIR }}/python3.11"

# 配置Python环境
echo "import site" >> "${{ env.DIST_DIR }}/python3.11/python311._pth"
Invoke-WebRequest -Uri "https://bootstrap.pypa.io/get-pip.py" -OutFile "${{ env.DIST_DIR }}/python3.11/get-pip.py"

echo "Downloading Python3.11 ..."
Invoke-WebRequest https://www.python.org/ftp/python/3.11.2/python-3.11.2-embed-amd64.zip -OutFile C:/tmp_files/python.zip
7z x C:/tmp_files/python.zip -r -oC:/generated_files/python3.11
- name: Install project and dependencies
run: |
cd ${{ env.DIST_DIR }}
./python3.11/python.exe python3.11/get-pip.py
./python3.11/python.exe -m pip install "${{ github.workspace }}/dist/${{ env.WHEEL_FILE }}"

echo "Downloading get-pip.py ..."
Invoke-WebRequest -URI https://bootstrap.pypa.io/get-pip.py -OutFile C:/generated_files/python3.11/get-pip.py
echo "import site" >> C:/generated_files/python3.11/python311._pth
- name: Download and setup FFmpeg
run: |
Invoke-WebRequest -Uri "https://www.gyan.dev/ffmpeg/builds/packages/ffmpeg-7.0.2-essentials_build.7z" -OutFile "${{ env.BUILD_DIR }}/ffmpeg.7z"
7z x "${{ env.BUILD_DIR }}/ffmpeg.7z" -o"${{ env.DIST_DIR }}/ffmpeg"
mv "${{ env.DIST_DIR }}/ffmpeg/ffmpeg-7.0.2-essentials_build" "${{ env.DIST_DIR }}/ffmpeg/bin"

echo "Moving files..."
mv D:\a\chatgpt-mirai-qq-bot\chatgpt-mirai-qq-bot\.github\quickstarts\windows\ C:/generated_files/files/
- name: Download VC++ Runtime
run: |
Invoke-WebRequest -Uri "https://aka.ms/vs/17/release/vc_redist.x64.exe" -OutFile "${{ env.DIST_DIR }}/【语音功能依赖】vc_redist.x64.exe"

echo "Replacing..."
dir C:/generated_files/files/scripts/

cp C:/generated_files/files/scripts/初始化.cmd C:/generated_files/
- name: Setup Web UI
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# 下载 Web UI 压缩包到临时目录
$release = Invoke-RestMethod -Uri "https://api.github.com/repos/DarkSkyTeam/chatgpt-for-bot-webui/releases" -Headers @{Authorization = "Bearer $env:GH_TOKEN"}
$web_ui_url = $release[0].assets[0].browser_download_url
$zip_file = "${{ env.BUILD_DIR }}/webui.zip"
Invoke-WebRequest -Uri $web_ui_url -OutFile $zip_file

echo "Downloading vc_redist.exe..."
Invoke-WebRequest -URI https://aka.ms/vs/17/release/vc_redist.x64.exe -OutFile "C:\generated_files\【语音功能依赖】vc_redist.x64.exe"
# 解压到临时目录
$temp_dir = "${{ env.BUILD_DIR }}/webui_temp"
mkdir $temp_dir
Expand-Archive -Path $zip_file -DestinationPath $temp_dir

echo "Downloading packages..."
cd C:/generated_files/kirara_ai
..\python3.11\python.exe C:/generated_files/python3.11/get-pip.py
..\python3.11\python.exe -m pip install -r requirements.txt
..\python3.11\python.exe -c exec("""from pycloudflared import try_cloudflare \ntry: try_cloudflare(-1)\nexcept:... """)
New-Item -ItemType Directory -Force -Path "${{ env.DIST_DIR }}/web"

cd C:/generated_files
# 移动 dist 文件夹到目标位置
Copy-Item -Path "$temp_dir/dist/*" -Destination "${{ env.DIST_DIR }}/web" -Force -Recurse

- name: Download and extract web dist.zip
shell: pwsh
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Copy startup scripts
run: |
# 获取最新的 release 信息
$latestRelease = Invoke-RestMethod -Uri "https://api.github.com/repos/DarkSkyTeam/chatgpt-for-bot-webui/releases" -Headers @{ Authorization = "Bearer $($env:GH_TOKEN)" }
$zipUrl = $latestRelease[0].assets[0].browser_download_url

# 下载 dist.zip 文件
Invoke-WebRequest -Uri $zipUrl -OutFile "C:\generated_files\dist.zip"
Copy-Item ".github/quickstarts/windows/scripts/*" -Destination "${{ env.DIST_DIR }}/" -Recurse
# 拷贝 data 文件夹
Copy-Item -Path "${{ github.workspace }}/data" -Destination "${{ env.DIST_DIR }}/" -Recurse

# 解压 dist.zip 文件到 C:/generated_files 目录
Expand-Archive -Path "C:\generated_files\dist.zip" -DestinationPath "C:\generated_files\kirara_ai\web"
rm C:\generated_files\dist.zip
- name: Archive production artifacts
- name: Upload workflow artifacts
uses: actions/upload-artifact@v4
with:
name: quickstart-windows-kirara-ai-amd64
path: |
C:\generated_files\
- name: Create Release Archive
name: ${{ env.PACKAGE_NAME }}
path: ${{ env.DIST_DIR }}

- name: Create release archive
if: startsWith(github.ref, 'refs/tags/')
shell: pwsh
run: |
Write-Host "Creating archive for release..."
Compress-Archive -Path C:\generated_files\* -DestinationPath C:\generated_files\quickstart-windows-kirara-ai-amd64.zip
- name: Upload compressed files to release
Compress-Archive -Path "${{ env.DIST_DIR }}/*" -DestinationPath "${{ env.BUILD_DIR }}/${{ env.PACKAGE_NAME }}.zip"

- name: Upload release archive
if: startsWith(github.ref, 'refs/tags/')
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: C:\generated_files\quickstart-windows-kirara-ai-amd64.zip
file: ${{ env.BUILD_DIR }}/${{ env.PACKAGE_NAME }}.zip
asset_name: Windows-quickstart-kirara-ai-${{ github.ref_name }}.zip
tag: ${{ github.ref_name }}
overwrite: false
body: "quickstart-windows-kirara-ai-amd64.zip 是为 Windows x64 用户准备的快速启动包"
body: "Windows x64 用户的快速启动包"
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ jobs:
docker build -t test-image .
- name: Run tests in Docker
run: |
docker run test-image sh -c "python -m pip install pytest coverage pytest-cov && python -m pytest -v --cov=framework --cov-report=term-missing"
docker run -v $(pwd):/app test-image sh -c "python -m pip install pytest coverage pytest-cov && python -m pytest /app/tests -v --cov=kirara_ai --cov-report=term-missing"
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ venv/
.vscode/
config.yaml
config.yaml.bak
data/config.yaml
data/config.yaml.bak
logs/
**/password.hash
dist/
build/
*.egg-info/
.coverage
/web/
44 changes: 34 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,36 +1,60 @@
FROM python:3.10.13-slim-bullseye
# 第一阶段:构建wheel包
FROM python:3.11-slim AS builder

WORKDIR /build
COPY . .
RUN python -m pip install build && \
python -m build

# 第二阶段:运行环境
FROM python:3.11-slim-bullseye

ENV DEBIAN_FRONTEND=noninteractive

# 复制字体文件
COPY ./data/fonts/sarasa-mono-sc-regular.ttf /usr/share/fonts/

# 安装系统依赖
RUN apt-get -yqq update && \
apt-get -yqq install --no-install-recommends xvfb binutils build-essential qtbase5-dev wkhtmltopdf ffmpeg dbus curl jq unzip && \
apt-get -yqq install --no-install-recommends \
xvfb \
qtbase5-dev \
wkhtmltopdf \
ffmpeg \
dbus \
curl \
jq \
unzip && \
(strip --remove-section=.note.ABI-tag /usr/lib/x86_64-linux-gnu/libQt5Core.so.5 || true) && \
apt-get -yq clean && \
apt-get -yq purge --auto-remove -o APT::AutoRemove::RecommendsImportant=false && \
rm -rf /var/lib/apt/lists/*

# 配置dbus
RUN export DBUS_SESSION_BUS_ADDRESS=`dbus-daemon --fork --config-file=/usr/share/dbus-1/session.conf --print-address`

RUN mkdir -p /app

# 创建应用目录
WORKDIR /app

COPY requirements.txt /app
# 复制第一阶段构建的wheel包并安装
COPY --from=builder /build/dist/*.whl /app/

# 下载Web UI并安装依赖
RUN LATEST_RELEASE_URL=$(curl -s https://api.github.com/repos/DarkSkyTeam/chatgpt-for-bot-webui/releases | jq -r '.[0].assets[] | select(.name == "dist.zip") | .browser_download_url') \
&& curl -L -o dist.zip "$LATEST_RELEASE_URL" \
&& unzip dist.zip -d web \
&& rm dist.zip && \
pip install --no-cache-dir -r requirements.txt && \
pip install --no-cache-dir *.whl && \
pip cache purge && \
python -c "from pycloudflared import try_cloudflare; try_cloudflare(-1)" || true

RUN apt-get -yqq remove --purge binutils unzip curl jq
python -c "from pycloudflared import try_cloudflare; try_cloudflare(-1)" || true && \
rm *.whl

COPY . /app
# 移除不再需要的包
RUN apt-get -yqq remove --purge curl jq unzip

# 复制应用代码
COPY ./docker/start.sh /app/docker/
COPY ./data /tmp/data
EXPOSE 8080

CMD ["/bin/bash", "/app/docker/start.sh"]
Loading