NineAI 2.4.2

This commit is contained in:
vastxie
2024-01-17 09:22:28 +08:00
commit bdc48207fc
636 changed files with 41864 additions and 0 deletions

54
dist/modules/draw/draw.controller.js vendored Normal file
View File

@@ -0,0 +1,54 @@
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DrawController = void 0;
const draw_service_1 = require("./draw.service");
const common_1 = require("@nestjs/common");
const swagger_1 = require("@nestjs/swagger");
const jwtAuth_guard_1 = require("../../common/auth/jwtAuth.guard");
const chatDraw_dto_1 = require("./dto/chatDraw.dto");
let DrawController = class DrawController {
constructor(drawService) {
this.drawService = drawService;
}
getEngines() {
return this.drawService.getEngines();
}
textToImage(body) {
return this.drawService.drawTextToImage(body);
}
};
__decorate([
(0, common_1.Get)('engines'),
(0, swagger_1.ApiOperation)({ summary: '获取stable Diffusion 模型' }),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], DrawController.prototype, "getEngines", null);
__decorate([
(0, common_1.Post)('drawTextToImage'),
(0, swagger_1.ApiOperation)({ summary: 'stable Diffusion绘画' }),
(0, common_1.UseGuards)(jwtAuth_guard_1.JwtAuthGuard),
(0, swagger_1.ApiBearerAuth)(),
__param(0, (0, common_1.Body)()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [chatDraw_dto_1.StableDrawDto]),
__metadata("design:returntype", void 0)
], DrawController.prototype, "textToImage", null);
DrawController = __decorate([
(0, swagger_1.ApiTags)('draw'),
(0, common_1.Controller)('draw'),
__metadata("design:paramtypes", [draw_service_1.DrawService])
], DrawController);
exports.DrawController = DrawController;

21
dist/modules/draw/draw.module.js vendored Normal file
View File

@@ -0,0 +1,21 @@
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DrawModule = void 0;
const common_1 = require("@nestjs/common");
const draw_controller_1 = require("./draw.controller");
const draw_service_1 = require("./draw.service");
let DrawModule = class DrawModule {
};
DrawModule = __decorate([
(0, common_1.Module)({
controllers: [draw_controller_1.DrawController],
providers: [draw_service_1.DrawService],
})
], DrawModule);
exports.DrawModule = DrawModule;

81
dist/modules/draw/draw.service.js vendored Normal file
View File

@@ -0,0 +1,81 @@
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DrawService = void 0;
const common_1 = require("@nestjs/common");
const axios_1 = require("axios");
const uuid = require("uuid");
const upload_service_1 = require("../upload/upload.service");
let DrawService = class DrawService {
constructor(uploadService) {
this.uploadService = uploadService;
}
async onModuleInit() {
var _a;
this.apiHost = (_a = process.env.API_HOST) !== null && _a !== void 0 ? _a : 'https://api.stability.ai';
this.apiKey = process.env.STABILITY_API_KEY;
if (!this.apiKey) {
this.apiKey = '*********';
}
this.Authorization = `Bearer ${this.apiKey}`;
}
async getEngines() {
var _a, _b;
const url = `${this.apiHost}/v1/engines/list`;
const res = await (0, axios_1.default)(url, {
method: 'GET',
headers: { Authorization: this.Authorization },
});
if (res.status === 401) {
console.log(`stability api key is invalid, ${(_a = res === null || res === void 0 ? void 0 : res.data) === null || _a === void 0 ? void 0 : _a.message}`);
}
if (res.status !== 200) {
console.log(`${res.status} ${(_b = res === null || res === void 0 ? void 0 : res.data) === null || _b === void 0 ? void 0 : _b.message}}`);
throw new common_1.HttpException('获取列表失败', common_1.HttpStatus.BAD_REQUEST);
}
return res.data;
}
async drawTextToImage(body) {
const { engineId = 'stable-diffusion-768-v2-1' } = body;
const headers = {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: this.Authorization,
};
const url = `${this.apiHost}/v1/generation/${engineId}/text-to-image`;
try {
const response = await axios_1.default.post(url, body, { headers });
if (response.status !== 200) {
throw new common_1.HttpException('绘制失败', common_1.HttpStatus.BAD_REQUEST);
}
const resImageBasetask = [];
for (const item of response.data.artifacts) {
const filename = uuid.v4().slice(0, 10) + '.png';
const buffer = Buffer.from(item.base64, 'base64');
resImageBasetask.push(this.uploadService.uploadFile({ filename, buffer }));
}
const urls = await Promise.all(resImageBasetask);
return urls;
}
catch (error) {
if (!(error === null || error === void 0 ? void 0 : error.response)) {
throw new common_1.HttpException('绘制失败', common_1.HttpStatus.BAD_REQUEST);
}
const { status, data } = error.response;
throw new common_1.HttpException(data.message, status);
}
}
};
DrawService = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [upload_service_1.UploadService])
], DrawService);
exports.DrawService = DrawService;

112
dist/modules/draw/dto/chatDraw.dto.js vendored Normal file
View File

@@ -0,0 +1,112 @@
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StableDrawDto = exports.TextPromptDto = void 0;
const class_validator_1 = require("class-validator");
const swagger_1 = require("@nestjs/swagger");
const class_transformer_1 = require("class-transformer");
class TextPromptDto {
}
__decorate([
(0, class_validator_1.IsString)(),
__metadata("design:type", String)
], TextPromptDto.prototype, "text", void 0);
__decorate([
(0, class_validator_1.IsNumber)(),
__metadata("design:type", Number)
], TextPromptDto.prototype, "weight", void 0);
exports.TextPromptDto = TextPromptDto;
class StableDrawDto {
constructor() {
this.samples = 1;
this.width = 512;
this.height = 512;
this.cfg_scale = 7;
this.steps = 30;
this.clip_guidance_preset = 'NONE';
}
}
__decorate([
(0, swagger_1.ApiProperty)({ example: 'stable-diffusion-512-v2-1', default: 512, description: '模型id', required: true }),
(0, class_validator_1.IsDefined)({ message: '模型id是必传参数' }),
__metadata("design:type", String)
], StableDrawDto.prototype, "engineId", void 0);
__decorate([
(0, swagger_1.ApiProperty)({
example: [
{
text: 'Draw a cute little dog',
weight: 0.5,
},
],
description: '绘画描述信息',
}),
(0, class_transformer_1.Type)(() => TextPromptDto),
(0, class_validator_1.ValidateNested)({ each: true }),
__metadata("design:type", Array)
], StableDrawDto.prototype, "text_prompts", void 0);
__decorate([
(0, swagger_1.ApiProperty)({ example: 1, description: '绘画张数', required: true }),
__metadata("design:type", Object)
], StableDrawDto.prototype, "samples", void 0);
__decorate([
(0, swagger_1.ApiProperty)({ example: 512, default: 512, description: '图片尺寸宽度' }),
(0, class_validator_1.Max)(1024, { message: '图片尺寸最大宽度1024' }),
(0, class_validator_1.Min)(512, { message: '图片尺寸最小宽度512' }),
__metadata("design:type", Object)
], StableDrawDto.prototype, "width", void 0);
__decorate([
(0, swagger_1.ApiProperty)({ example: 512, default: 512, description: '图片尺寸高度' }),
(0, class_validator_1.Max)(1024, { message: '图片高度尺寸最大宽度1024' }),
(0, class_validator_1.Min)(512, { message: '图片高度尺寸最小宽度512' }),
__metadata("design:type", Object)
], StableDrawDto.prototype, "height", void 0);
__decorate([
(0, swagger_1.ApiProperty)({ example: 15, default: 7, description: '图片绘制扩散思维[值越高,图像越接近提示]', required: true }),
(0, class_validator_1.Max)(35, { message: '扩散思维值最大为35' }),
(0, class_validator_1.Min)(0, { message: '扩散思维值最小为0' }),
__metadata("design:type", Object)
], StableDrawDto.prototype, "cfg_scale", void 0);
__decorate([
(0, swagger_1.ApiProperty)({ example: 50, description: '绘制步骤', required: true }),
(0, class_validator_1.Max)(150, { message: '最大步骤不大于150' }),
(0, class_validator_1.Min)(10, { message: '步骤不小于10' }),
__metadata("design:type", Object)
], StableDrawDto.prototype, "steps", void 0);
__decorate([
(0, swagger_1.ApiProperty)({ example: 'anime', description: '样式预设', required: true }),
(0, class_validator_1.IsIn)([
'3d-model',
'analog-film',
'anime',
'cinematic',
'comic-book',
'digital-art',
'enhance',
'fantasy-art',
'isometric',
'line-art',
'low-poly',
'modeling-compound',
'neon-punk',
'origami',
'photographic',
'pixel-art',
'tile-texture',
]),
__metadata("design:type", String)
], StableDrawDto.prototype, "style_preset", void 0);
__decorate([
(0, swagger_1.ApiProperty)({ example: 'NONE', description: '裁剪指南预设', required: true }),
(0, class_validator_1.IsIn)(['NONE', 'FAST_BLUE', 'FAST_GREEN', 'SIMPLE', 'SLOW', 'SLOWER', 'SLOWEST']),
__metadata("design:type", Object)
], StableDrawDto.prototype, "clip_guidance_preset", void 0);
exports.StableDrawDto = StableDrawDto;