Files
ai/README.md
2023-05-12 22:53:49 +08:00

412 lines
13 KiB
Markdown

# ai 繪圖
ai 繪圖服務端(快速重構)
TEST:
- [x] GET [/api/users](/api/users) 用戶列表
- [x] GET [/api/sessions](/api/sessions) 會話列表
- [x] GET [/api/servers](/api/servers) 服務器列表
- [x] GET [/api/models](/api/models) 模型列表
- [x] GET [/api/images](/api/images) 圖片列表
- [x] GET [/api/tasks](/api/tasks) 任務列表
- [x] GET [/api/tags](/api/tags) 標籤列表
TEST:
- [x] POST [/api/users](/api/users) 創建用戶
- [x] POST [/api/sesions](/api/sessions) 創建會話
列表接口:
- [ ] /api/tags [#標籤詳情](#標籤列表)
- [ ] /api/users [#用戶詳情](#用戶列表)
- [ ] /api/tasks [#任務詳情](#任務列表)
- [x] /api/models [#模型列表](#模型列表)
- [x] GET /api/models/{id}
- [x] PATCH /api/models/{id}
- [x] DELETE /api/models/{id}
- [ ] WebSocket /api/models/{id}
- [ ] /api/images [#圖片列表](#圖片列表)
- [ ] /api/params [#參數列表](#參數列表)
列表接口-請求方式
Method | URL | Info
-------|--------------------------------|------------------
GET | /api/{name} | 分頁:顺序查询
GET | /api/{name}?page=1&pageSize=20 | 分頁:页码和數量
GET | /api/{name}?user_id=1234 | 篩選:按用戶
GET | /api/{name}?tag=xxx | 篩選:按標籤
GET | /api/{name}?public=true | 篩選:公開狀態(true|false)
POST | /api/{name} | 篩選:創建新對象
* 選取條件有多個的,以逗號分隔(並集)
* 過濾條件有多個的,複寫query(交集)
詳情接口:
- [ ] /api/tags/{tag_id} [#標籤詳情](#標籤詳情)
- [ ] /api/users/{user_id} [#用戶詳情](#用戶詳情)
- [ ] /api/tasks/{task_id} [#任務詳情](#任務詳情)
- [ ] /api/models/{model_id} [#模型詳情](#模型詳情)
- [ ] /api/images/{image_id} [#圖片詳情](#圖片詳情)
- [ ] /api/params/{params_id} [#參數詳情](#參數詳情)
- [ ] /api/server/{server_id} [#服務詳情](#服務詳情)
詳情接口-請求方式:
Method | URL | Info
-------|--------------------------------|------------------
GET | /api/{name}/{item_id} | 獲取對象詳情
PATCH | /api/{name}/{item_id} | 修改對象字段
DELETE | /api/{name}/{item_id} | 刪除對象
WS | /api/{name}/{item_id} | Websocket 連接對象
* GET查詢以外的操作都必須在headers中攜帶token驗證身份權限
* GET查詢私有模型也必須登錄, 否則不會被展示
對象模型:
```go
type ListView struct {
Page int `json:"page"` // 当前页码
PageSize int `json:"page_size"` // 分頁大小
Total int `json:"total"` // 數據總量
Next bool `json:"next"` // 可否翻頁
List []interface{} `json:"list"` // 數據列表
}
type Model struct {
ID int `json:"id"`
Name string `json:"name"`
Type string `json:"type"` // (lora|ckp|hyper|ti)
TriggerWords string `json:"trigger_words"` // 觸發詞
BaseModel string `json:"base_model"` // (SD1.5|SD2)
ModelPath string `json:"model_path"` // 模型路徑
Status string `json:"status"` // (initial|ready|waiting|running|success|error)
Progress int `json:"progress"` // (0-100)
Image string `json:"image"` // 封面圖片實際地址
Tags string `json:"tags"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
UserID int `json:"user_id"`
}
type Task struct {
ID int `json:"id"`
Name string `json:"name"`
Type string `json:"type"` // 任務類型(訓練|推理)
Status string `json:"status"` // 任務狀態(initial|ready|waiting|running|success|error)
Progress int `json:"progress"` // 任務進度(0-100)
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
UserID int `json:"user_id"`
}
type Image struct {
ID int `json:"id"`
Name string `json:"name"`
Width int `json:"width"`
Height int `json:"height"`
Prompt string `json:"prompt"` // 描述詞
NegativePrompt string `json:"negative_prompt"` // 排除描述詞
NumInferenceSteps int `json:"num_inference_steps"` // (1~500)
GuidanceScale float32 `json:"guidance_scale"` // (1~20)
Scheduler string `json:"scheduler"` // (DDIM|K_EULER|DPMSolverMultistep|K_EULER_ANCESTRAL|PNDM|KLMS)
Seed int `json:"seed"` // 隨機數種子
FromImage string `json:"from_image"` // 圖片地址(以圖繪圖)
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
UserID int `json:"user_id"`
}
type Tag struct {
ID int `json:"id"`
Name string `json:"name"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
type Param struct {
// 字段不固定
}
```
```javascript
// ListView 列表分頁
{
page: 1,
pageSize: 20,
next: true,
list: []
}
// Image 圖片對象
{
id: 1234, // 原图ID
width: 512, // 原图宽度
height: 512, // 原图高度
createdAt: '', // 創建時間
updatedAt: '', // 更新時間
user: { // 来源用户
id: 1234,
user_name: 'LAST',
createdAt: '',
updatedAt: '',
},
article: { // 来源文章
id: 1234,
title: 'GAMEX',
}
}
// Task 任務對象
{
id: 'xxxxx', // 任務ID
name: 'xxx', // 任務名稱
status: 'xxx', // 任務狀態(waiting|running|success|error)
progress: 100, // 任務進度(0~100)
type: '', // 任務類型(train|Inference)
data: { // 任務數據
id: '', // 模型ID
... // 其它參數
},
createdAt: '', // 創建時間
updatedAt: '', // 更新時間
}
```
---------------------------------------------------------------
### 圖片列表
GET /api/images
```javascript
{
page: 1, // 当前页码
pageSize: 20, // 分页数
next: true, // 是否存在下一页
list: [{
id: 1234, // 原图ID
width: 512, // 原图宽度
height: 512, // 原图高度
createdAt: '', // 創建時間
updatedAt: '', // 更新時間
user: { // 来源用户
id: 1234,
user_name: 'LAST',
},
article: { // 来源文章
id: 1234,
title: 'GAMEX',
}
}]
}
```
列表视图:(输出控制)
Method | URL | Info
-------|--------------------------------|----------------------------------
GET | /api/images | 标准顺序查询
GET | /api/images?page=1&pageSize=20 | 指定页码和指定分页大小
DELETE | /api/images/{image_id} | 刪除指定圖片
筛选规则:(数据过滤)
Method | URL | Info
-------|--------------------------------|----------------------------------
GET | /api/images?user=1234 | 筛选指定某用户发表的图片
GET | /api/images?choice=1234 | 筛选指定精选集下的图片
GET | /api/images?like=1234 | 筛选指定用户点赞的图片
GET | /api/images?tag=1234 | 筛选含有指定标签的图片
GET | /api/images?tag=1234,1235 | 筛选含有多个标签之一的图片(并集)
GET | /api/images?tag=1234&tag=1235 | 筛选含有指定多个标签的图片(交集)
GET | /api/images?user=1234&tag=123 | 筛选指定用户的指定标签图片(交集)
GET | /api/images?date=20220214+ | 时间范围(之后)
GET | /api/images?date=20220214- | 时间范围(之前)
GET | /api/images?date=2022~2023 | 时间范围(之间)
排序规则:(权重强化)
Method | URL | Info
-------|--------------------------------|----------------------------------
GET | /api/images?similar=1234 | 根据指定图片的相似图片(指定图片ID)
GET | /api/images?sort=date+ | 排序规则(相似图片查询时此项无效)
GET | /api/images?sort=like | 根据用户偏好推荐(指定用户的偏好)
GET | /api/images?sort=history | 根据浏览记录推荐(指定用户的记录)
GET | /api/images?sort=choice | 根据精选集推荐(指定精选集ID,取一组权重)
* 注意, 筛选规则为多条件取交集, 单条件的复数取并集
* 权重强化属于排序规则而非过滤规则
### 模型列表
GET /api/models
```javascript
{
page: 1, // 当前页码
pageSize: 20, // 分页数
next: true, // 是否存在下一页
list: [{
id: 'xxxxx', // 模型ID
name: 'xxx', // 模型名稱
createdAt: '', // 創建時間
updatedAt: '', // 更新時間
}]
}
```
篩選規則:(數據過濾)
Method | URL | Info
-------|--------------------------------|----------------------------------
GET | /api/models | 獲取所有模型
GET | /api/models?user=1234 | 僅取指定用戶的(按用戶ID過濾)
GET | /api/models?tag=xxx | 按標籤分類篩選
GET | /api/models?public=true | 僅取公開的
GET | /api/models?public=false | 僅取私有的
POST | /api/models | 創建一個模型
* 如果未登錄, 返回的結果將過濾掉無權限查看的內容
* 如果已登錄, 返回的結果將包含私有的
POST /api/models
```javascript
// 發送數據
{
name: '', // 設定名稱
type: '', // 指定訓練類型
source: '', // 指定源模型ID
type: '', // 模型類型(RoLa|SD2|SD1.5)
data: {
oss: [], // 直接上傳到OSS的圖片地址列表
images: [], // 指定圖片的ID們
choices: [], // 精選集的ID們
}, // 指定數據集(可以是上傳到OSS的文件列表, 也可以是已有的圖片ID, 也可以是精選集ID)
}
// 返回數據
{
id: 'xxx', // 模型ID
name: '', // 模型名稱
status: '', // 模型狀態(必須訓練完成的才可用)
createdAt: '', // 創建時間
updatedAt: '', // 更新時間
}
```
### 模型詳情
GET /api/models/{model_id}
```javascript
{
id: 'xxxxx', // 模型ID
name: 'xxx', // 模型名稱
createdAt: '', // 創建時間
updatedAt: '', // 更新時間
}
```
PATCH /api/models/{model_id}
```javascript
{
name: 'xxx', // 修改模型名稱
status: 'ready', // (initial|ready|waiting|running|success|error)
}
```
* 修改指定字段, 只傳遞需要修改的字段
* status 初始默認爲 'initial', 修改爲 'ready' 即就緒狀態, 服務器將爲此任務自動排期
* 當模型訓練任務被加入等待隊列時, 其狀態自動變更爲 'waiting'
* 當模型訓練任務被執行時, 其狀態自動變更爲 'running', 並開始更新進度條 Progress
* 當模型訓練完成時狀態自動變更爲'success', 失敗時爲'error'
* 訓練中的模型將被鎖定, 無法修改刪除等操作
DELETE /api/models/{model_id}
* 刪除指定模型
### 獲取標籤
GET /api/tags
```javascript
{
page: 1, // 当前页码
pageSize: 20, // 分页数
next: true, // 是否存在下一页
list: [{
id: 'xxxxx', // 標籤ID
name: 'xxx', // 標籤名稱
createdAt: '', // 創建時間
updatedAt: '', // 更新時間
}]
}
```
篩選規則:(數據過濾)
Method | URL | Info
-------|--------------------------------|------------------
GET | /api/tags?user=1234 | 按用戶ID篩選
GET | /api/tags?tag=xxx | 按標籤分類篩選
DELETE | /api/tags/{tag_id} | 刪除指定標籤
### 獲取任務
GET /api/tasks
```javascript
{
page: 1, // 当前页码
pageSize: 20, // 分页数
next: true, // 是否存在下一页
list: [{
id: 'xxxxx', // 任務ID
name: 'xxx', // 任務名稱
status: 'xxx', // 任務狀態(waiting|running|success|error)
progress: 100, // 任務進度(0~100)
data: {}, // 任務數據
createdAt: '', // 創建時間
updatedAt: '', // 更新時間
}]
}
```
篩選規則:(數據過濾)
Method | URL | Info
-------|--------------------------------|------------------
GET | /api/tasks?user=1234 | 按用戶ID篩選
GET | /api/tasks?tag=xxx | 按標籤分類篩選
DELETE | /api/tasks/{task_id} | 刪除指定任務
### 監聽任務
WebSocket /api/tasks/{task_id}
```javascript
{
id: 'xxxx', // 任務ID
status: 'xxx', // 任務狀態(waiting|running|success|error)
progress: 100, // 任務進度(0~100)
data: {}, // 返回數據
}
```
* 使用websocket建立連接以對指定任務監聽狀態變化
* 離開頁面時,或是任務結束時, 應斷開websocket連接
* 當任務狀態發生變化時, 服務端向瀏覽器主動發送消息
* 返回數據:任務執行中爲預覽, 任務完成時爲生成結果