From f3d05d5be4ffa0a5340284cfc02feb7becab5a58 Mon Sep 17 00:00:00 2001 From: Germey Date: Sat, 7 Oct 2023 21:35:04 +0800 Subject: [PATCH] add mj generation --- src/components/midjourney/ChannelSelector.vue | 128 +++++++++++++ .../midjourney/ElementsSelector.vue | 178 ++++++++++++++++++ src/components/midjourney/IgnoreSelector.vue | 58 ++++++ src/components/midjourney/ImaginePreview.vue | 50 +++++ src/components/midjourney/PresetPanel.vue | 26 ++- src/components/midjourney/PromptInput.vue | 58 ++++++ src/operators/index.ts | 1 + src/operators/midjourney/constants.ts | 30 +++ src/operators/midjourney/index.ts | 3 + src/operators/midjourney/models.ts | 47 +++++ src/operators/midjourney/operator.ts | 32 ++++ src/pages/midjourney/Index.vue | 124 +++++++++++- src/store/index.ts | 3 +- 13 files changed, 725 insertions(+), 13 deletions(-) create mode 100644 src/components/midjourney/ChannelSelector.vue create mode 100644 src/components/midjourney/ElementsSelector.vue create mode 100644 src/components/midjourney/IgnoreSelector.vue create mode 100644 src/components/midjourney/ImaginePreview.vue create mode 100644 src/components/midjourney/PromptInput.vue create mode 100644 src/operators/midjourney/constants.ts create mode 100644 src/operators/midjourney/index.ts create mode 100644 src/operators/midjourney/models.ts create mode 100644 src/operators/midjourney/operator.ts diff --git a/src/components/midjourney/ChannelSelector.vue b/src/components/midjourney/ChannelSelector.vue new file mode 100644 index 0000000..a5a6730 --- /dev/null +++ b/src/components/midjourney/ChannelSelector.vue @@ -0,0 +1,128 @@ + + + + + diff --git a/src/components/midjourney/ElementsSelector.vue b/src/components/midjourney/ElementsSelector.vue new file mode 100644 index 0000000..db4cc18 --- /dev/null +++ b/src/components/midjourney/ElementsSelector.vue @@ -0,0 +1,178 @@ + + + + + diff --git a/src/components/midjourney/IgnoreSelector.vue b/src/components/midjourney/IgnoreSelector.vue new file mode 100644 index 0000000..090b7c2 --- /dev/null +++ b/src/components/midjourney/IgnoreSelector.vue @@ -0,0 +1,58 @@ + + + + + diff --git a/src/components/midjourney/ImaginePreview.vue b/src/components/midjourney/ImaginePreview.vue new file mode 100644 index 0000000..9559617 --- /dev/null +++ b/src/components/midjourney/ImaginePreview.vue @@ -0,0 +1,50 @@ + + + + + diff --git a/src/components/midjourney/PresetPanel.vue b/src/components/midjourney/PresetPanel.vue index a6ad9a7..59eb97a 100644 --- a/src/components/midjourney/PresetPanel.vue +++ b/src/components/midjourney/PresetPanel.vue @@ -3,7 +3,6 @@ - @@ -25,15 +24,28 @@ export default defineComponent({ StylizeSelector, ChaosSelector }, + props: { + modelValue: { + type: Object, + default: () => ({}) + } + }, + emits: ['update:modelValue'], data() { return { - preset: { - ratio: undefined, - version: undefined, - stylize: undefined, - chaos: undefined - } + preset: this.modelValue }; + }, + watch: { + modelValue(val) { + this.preset = val; + }, + preset: { + handler(val) { + this.$emit('update:modelValue', val); + }, + deep: true + } } }); diff --git a/src/components/midjourney/PromptInput.vue b/src/components/midjourney/PromptInput.vue new file mode 100644 index 0000000..dbd02ed --- /dev/null +++ b/src/components/midjourney/PromptInput.vue @@ -0,0 +1,58 @@ + + + + + diff --git a/src/operators/index.ts b/src/operators/index.ts index 0391628..eb7679d 100644 --- a/src/operators/index.ts +++ b/src/operators/index.ts @@ -3,3 +3,4 @@ export * from './application'; export * from './user'; export * from './api'; export * from './chat'; +export * from './midjourney'; diff --git a/src/operators/midjourney/constants.ts b/src/operators/midjourney/constants.ts new file mode 100644 index 0000000..746bb92 --- /dev/null +++ b/src/operators/midjourney/constants.ts @@ -0,0 +1,30 @@ +import { IMidjourneyChannel } from './models'; + +export const MIDJOURNEY_CHANNEL_NAME_FAST = 'Fast'; +export const MIDJOURNEY_CHANNEL_NAME_RELAX = 'Relax'; +export const MIDJOURNEY_CHANNEL_NAME_TURBO = 'Turbo'; + +export const API_ID_MIDJOURNEY_FAST = '9a628863-8879-462b-bbee-5dc46505b733'; +export const API_ID_MIDJOURNEY_RELAX = 'c58713f3-fef7-4c18-824c-9f76b5a07a7f'; +export const API_ID_MIDJOURNEY_TURBO = '62ec82bd-7de3-427f-b71a-ab3551ac7677'; + +export const MIDJOURNEY_CHANNEL_FAST: IMidjourneyChannel = { + icon: 'fa-bolt fa-regular', + apiId: API_ID_MIDJOURNEY_FAST, + name: MIDJOURNEY_CHANNEL_NAME_FAST, + displayName: 'Fast' +}; + +export const MIDJOURNEY_CHANNEL_RELAX: IMidjourneyChannel = { + icon: 'fa-bolt fa-regular', + apiId: API_ID_MIDJOURNEY_RELAX, + name: MIDJOURNEY_CHANNEL_NAME_RELAX, + displayName: 'Relax' +}; + +export const MIDJOURNEY_CHANNEL_TURBO: IMidjourneyChannel = { + icon: 'fa-bolt fa-regular', + apiId: API_ID_MIDJOURNEY_TURBO, + name: MIDJOURNEY_CHANNEL_NAME_TURBO, + displayName: 'Turbo' +}; diff --git a/src/operators/midjourney/index.ts b/src/operators/midjourney/index.ts new file mode 100644 index 0000000..47497e4 --- /dev/null +++ b/src/operators/midjourney/index.ts @@ -0,0 +1,3 @@ +export * from './constants'; +export * from './models'; +export * from './operator'; diff --git a/src/operators/midjourney/models.ts b/src/operators/midjourney/models.ts new file mode 100644 index 0000000..080c1b7 --- /dev/null +++ b/src/operators/midjourney/models.ts @@ -0,0 +1,47 @@ +export interface IMidjourneyChannel { + icon: string; + apiId: string; + name: string; + displayName: string; +} + +export interface IMidjourneyPreset { + model?: string; + ratio?: string; + version?: string; + stylize?: string; + chaos?: string; +} + +export enum MidjourneyImagineAction { + GENERATE = 'generate', + UPSAMPLE1 = 'upsample1' +} + +export enum MidjourneyImagineState { + PENDING = 'pending', + GENERATING = 'generating', + FINISHED = 'finished', + FAILED = 'failed' +} + +export interface IMidjourneyImagineRequest { + action: MidjourneyImagineAction; + prompt: string; + image_id?: string; +} + +export interface IMidjourneyImagineResponse { + task_id: string; + progress: number; + image_id: string; + image_url: string; + actions: MidjourneyImagineAction[]; +} + +export interface IMidjourneyImagineOptions { + stream?: (response: IMidjourneyImagineResponse) => void; + token: string; + endpoint: string; + path: string; +} diff --git a/src/operators/midjourney/operator.ts b/src/operators/midjourney/operator.ts new file mode 100644 index 0000000..bc22834 --- /dev/null +++ b/src/operators/midjourney/operator.ts @@ -0,0 +1,32 @@ +import axios, { AxiosResponse } from 'axios'; +import { IMidjourneyImagineOptions, IMidjourneyImagineRequest, IMidjourneyImagineResponse } from './models'; + +class MidjourneyOperator { + async imagine( + data: IMidjourneyImagineRequest, + options: IMidjourneyImagineOptions + ): Promise> { + return await axios.post(options.path, data, { + headers: { + authorization: `Bearer ${options.token}`, + accept: 'application/x-ndjson', + 'content-type': 'application/json' + }, + baseURL: options.endpoint, + responseType: 'stream', + onDownloadProgress: (event) => { + const response = event.target.response; + const lines = response.split('\r\n').filter((line: string) => !!line); + const lastLine = lines[lines.length - 1]; + if (lastLine) { + const jsonData = JSON.parse(lastLine); + if (options?.stream) { + options?.stream(jsonData as IMidjourneyImagineResponse); + } + } + } + }); + } +} + +export const midjourneyOperator = new MidjourneyOperator(); diff --git a/src/pages/midjourney/Index.vue b/src/pages/midjourney/Index.vue index b87d0b5..4d0b67e 100644 --- a/src/pages/midjourney/Index.vue +++ b/src/pages/midjourney/Index.vue @@ -1,20 +1,131 @@ - @@ -32,6 +143,11 @@ export default defineComponent({ } .main { flex: 1; + padding: 15px; + .title { + font-size: 14px; + margin-bottom: 10px; + } } } diff --git a/src/store/index.ts b/src/store/index.ts index 4ff5e14..4de96d0 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -10,8 +10,7 @@ const store = createStore({ return { user: {}, token: { - access: - 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNjk2NjQ2NjMxLCJqdGkiOiI4ODc1NzhhOTgyYjU0ZjBmODQ4NmU2ODFiYTZhYmVkMCIsInVzZXJfaWQiOiJlOGRjYWUyNC0yMmQwLTQ3MTItOTIxNy0zN2MyMzc0MTIwM2EiLCJwZXJtaXNzaW9ucyI6WyJhZGRfbG9nZW50cnkiLCJja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