Skip to content

使用labelStudio

代码仓库 官方网站

安装本地开发参考文章

1.在代码仓库中下载代码

bash
# 为了和上面我下载的源码做区分,我把文件夹命名为了label-studio-master
git clone --depth 1 https://gitclone.com/github.com/HumanSignal/label-studio.git label-studio-master
cd label-studio-master
# 将浅克隆转换为完整克隆
git fetch --unshallow

cd C:\Code\OpenSource\label-studio-master
# 安装poetry 需要10多分钟
pip install poetry
# 查看poetry 的配置
poetry config --list
poetry config virtualenvs.in-project true

安装依赖

bash
# 创建虚拟环境
poetry shell
# 安装依赖 需要10多分钟
poetry install
# Run database migrations
python label_studio/manage.py migrate
python label_studio/manage.py collectstatic
# Start the server in development mode at http://localhost:8080
python label_studio/manage.py runserver

首次运行项目需要执行python label_studio/manage.py migrate和python label_studio/manage.py collectstatic,下次运行的时候只需要创建虚拟环境(poetry shell)和运行服务(python label_studio/manage.py runserver)即可

前端项目

1.先在web文件夹的根目录创建环境变量文件 .env

shell
# .env
FRONTEND_HMR=true
FRONTEND_HOSTNAME=http://localhost:8010
DJANGO_HOSTNAME=http://localhost:8080

2.需要修改webpack.config.js中的devServer

js
 devServer: {
        host: "127.0.0.1",
        // Port for the Webpack dev server
        port: HMR_PORT,
        // Enable HMR
        hot: true,
        // Allow cross-origin requests from Django
        headers: { "Access-Control-Allow-Origin": "*" },
        static: {
          directory: path.resolve(__dirname, "../label_studio/core/static/"),
          publicPath: "/static"
        },
        devMiddleware: {
          publicPath: `${FRONTEND_HOSTNAME}/react-app/`
        },
        allowedHosts: "all", // Allow access from Django's server
        proxy: {
          "/api": {
            target: `${DJANGO_HOSTNAME}/api`,
            changeOrigin: true,
            pathRewrite: { "^/api": "" },
            secure: false
          },
          "/": {
            target: `${DJANGO_HOSTNAME}`,
            changeOrigin: true,
            secure: false
          }
        }
      }

3.添加依赖 (windows上)主要解决'NODE_ENV' is not recognized as an internal or external command,报错

bash
yarn add cross-env --save-dev

修改package.json:

修改script配置项中的dev和build命令

json
 "scripts": {
    "ui:serve": "nx storybook ui",
    "ui:test:component": "nx test-component ui",
    "lint": "biome check --apply .",
    "lint-scss": "yarn stylelint '**/*.scss' --fix",
    "ls:dev": "nx run labelstudio:serve:development",
    "ls:watch": "nx run labelstudio:build:development --watch",
    "ls:build": "nx run labelstudio:build:production",
    "ls:unit": "nx run labelstudio:unit",
    "ls:e2e": "nx run labelstudio-e2e:e2e",
    "lsf:watch": "nx run editor:build:development --watch",
    "lsf:serve": "FRONTEND_HOSTNAME=http://localhost:3000 MODE=standalone nx run editor:serve:development",
    "lsf:e2e": "cd libs/editor/tests/e2e && yarn test",
    "lsf:integration": "nx run editor:integration",
    "lsf:integration:watch": "nx run editor:integration --watch",
    "lsf:unit": "nx run editor:unit",
    "dm:watch": "nx run datamanager:build:development --watch",
    "dm:unit": "nx run datamanager:unit",
    "build": "cross-env NODE_ENV=production yarn ls:build",
    "version:libs": "nx run-many --target=version",
    "docs": "SCHEMA_JSON_PATH=$PWD/apps/labelstudio/src/pages/CreateProject/Config/schema.json; nx docs editor && biome check --apply $SCHEMA_JSON_PATH",
    "watch": "cross-env NODE_ENV=development BUILD_NO_SERVER=true yarn ls:watch",
    "dev": "cross-env NODE_ENV=development BUILD_NO_SERVER=true yarn ls:dev",
    "test:e2e": "yarn ls:e2e && yarn lsf:e2e",
    "test:integration": "yarn lsf:integration",
    "test:unit": "nx run-many --target=unit",
    "test:coverage": "yarn test:unit --coverage",
    "test:affected": "nx affected --target=test:unit --base=develop",
    "test:watch": "yarn test:unit --all --watch",
    "test:watch:affected": "yarn test --watch",
    "postinstall": "npx -y copy-files-from-to"
  },

清理 Poetry 缓存(管理员)

bash
poetry cache clear --all pypi

windwos 下修改代码重新运行

构建本地的docker镜像

labelStudio 本地构建命令:docker build -t heartexlabs/label-studio:latest .

运行构建的本地镜像:docker run -it -p 8080:8080 -v C:\path\to\mydata:/label-studio/data heartexlabs/label-studio