forked from erictik/midjourney-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanime.ts
206 lines (175 loc) · 5.64 KB
/
anime.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import "dotenv/config";
import { Midjourney } from "../src";
import * as fs from "fs/promises";
import * as path from "path";
interface Character {
name: string;
description: string;
stylePrompt: string;
referenceImage?: string; // Store the generated reference image path
}
interface Scene {
sceneNumber: number;
description: string;
characters: string[];
setting: string;
mood: string;
cameraAngle?: string;
action: string;
}
export interface AnimeScript {
title: string;
style: string;
artStyle: string;
characters: Character[];
scenes: Scene[];
}
class AnimeSceneGenerator {
private client: Midjourney;
private outputDir: string;
constructor() {
this.client = new Midjourney({
ServerId: <string>process.env.SERVER_ID,
ChannelId: <string>process.env.CHANNEL_ID,
SalaiToken: <string>process.env.SALAI_TOKEN,
HuggingFaceToken: <string>process.env.HUGGINGFACE_TOKEN,
Debug: true,
Ws: true,
});
this.outputDir = "./anime_output";
}
private async createOutputDirectory() {
try {
await fs.mkdir(this.outputDir, { recursive: true });
} catch (error) {
console.error("Error creating output directory:", error);
}
}
private buildCharacterPrompt(character: Character): string {
return `${character.description}, ${character.stylePrompt}, full body character reference sheet, character design, high detail --v 6 --style raw`;
}
private buildScenePrompt(
scene: Scene,
script: AnimeScript,
characterRefs: string[]
): string {
const characterPrompts = scene.characters
.map((charName) => {
const char = script.characters.find((c) => c.name === charName);
return char ? `${char.name} ${char.description}` : charName;
})
.join(", ");
// Build the base prompt
let prompt = `${scene.action}, ${scene.setting}, ${
scene.mood
}, ${characterPrompts}, ${scene.cameraAngle || ""}, ${
script.artStyle
}, highly detailed, professional lighting --ar 16:9 --v 6 --style raw`;
// Add character references
characterRefs.forEach((ref, index) => {
prompt += ` --cref ${ref}`;
});
// Add style reference from the first generated image if it exists
if (scene.sceneNumber > 1) {
const firstSceneRef = path.join(this.outputDir, "scene_001.png");
if (fs.existsSync(firstSceneRef)) {
prompt += ` --sref ${firstSceneRef}`;
}
}
return prompt;
}
private async generateImage(
prompt: string,
outputPath: string
): Promise<string | null> {
try {
const imagine = await this.client.Imagine(
prompt,
(uri: string, progress: string) => {
console.log(`Generating image: ${progress}%`);
}
);
if (!imagine) {
console.error("Failed to generate image");
return null;
}
// Upscale the first variation for best quality
const upscale = await this.client.Upscale({
index: 1,
msgId: <string>imagine.id,
hash: <string>imagine.hash,
flags: imagine.flags,
loading: (uri: string, progress: string) => {
console.log(`Upscaling image: ${progress}%`);
},
});
// Save metadata
const metadata = {
prompt,
imagineId: imagine.id,
upscaleId: upscale?.id,
timestamp: new Date().toISOString(),
};
await fs.writeFile(
`${outputPath}.json`,
JSON.stringify(metadata, null, 2)
);
console.log(`Generated image saved: ${outputPath}`);
return `${outputPath}.png`; // Return the path of the generated image
} catch (error) {
console.error("Error generating image:", error);
return null;
}
}
async generateCharacterReferences(
script: AnimeScript
): Promise<Map<string, string>> {
const characterRefs = new Map<string, string>();
for (const character of script.characters) {
const prompt = this.buildCharacterPrompt(character);
const outputPath = `${
this.outputDir
}/character_${character.name.toLowerCase()}`;
const imagePath = await this.generateImage(prompt, outputPath);
if (imagePath) {
characterRefs.set(character.name, imagePath);
}
}
return characterRefs;
}
async generateScenes(
script: AnimeScript,
characterRefs: Map<string, string>
): Promise<void> {
for (const scene of script.scenes) {
// Get relevant character references for this scene
const relevantRefs = scene.characters
.map((charName) => characterRefs.get(charName))
.filter((ref): ref is string => ref !== undefined);
const prompt = this.buildScenePrompt(scene, script, relevantRefs);
const outputPath = `${this.outputDir}/scene_${scene.sceneNumber
.toString()
.padStart(3, "0")}`;
await this.generateImage(prompt, outputPath);
// Wait a bit between generations to avoid rate limiting
await new Promise((resolve) => setTimeout(resolve, 2000));
}
}
async generate(scriptPath: string): Promise<void> {
try {
const scriptContent = await fs.readFile(scriptPath, "utf-8");
const script: AnimeScript = JSON.parse(scriptContent);
await this.createOutputDirectory();
await this.client.Connect();
console.log("Generating character references...");
const characterRefs = await this.generateCharacterReferences(script);
console.log("Generating scenes...");
await this.generateScenes(script, characterRefs);
this.client.Close();
} catch (error) {
console.error("Error in generate:", error);
this.client.Close();
}
}
}
export default AnimeSceneGenerator;