save current changes

This commit is contained in:
2025-08-17 17:47:54 +02:00
parent 60a5272054
commit dc4e57db2c
15 changed files with 944 additions and 68 deletions

53
src/lib/db/schema.ts Normal file
View File

@ -0,0 +1,53 @@
import { query } from '../mysql';
async function createSchema() {
try {
console.log('Creating tables...');
await query(`
CREATE TABLE IF NOT EXISTS video (
id INT AUTO_INCREMENT PRIMARY KEY,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
modified_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
genre VARCHAR(255),
sub_genre VARCHAR(255),
scene TEXT,
action TEXT,
camera TEXT,
image_prompt TEXT,
video_prompt TEXT,
image_path VARCHAR(255),
video_path VARCHAR(255)
)
`);
await query(`
CREATE TABLE IF NOT EXISTS tag (
id INT AUTO_INCREMENT PRIMARY KEY,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
modified_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
tag VARCHAR(255) UNIQUE
)
`);
await query(`
CREATE TABLE IF NOT EXISTS tag_video (
id INT AUTO_INCREMENT PRIMARY KEY,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
modified_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
tag_id INT,
video_id INT,
FOREIGN KEY (tag_id) REFERENCES tag(id),
FOREIGN KEY (video_id) REFERENCES video(id)
)
`);
console.log('Tables created successfully.');
} catch (error) {
console.error('Error creating tables:', error);
} finally {
process.exit();
}
}
createSchema();

58
src/lib/db/tag.ts Normal file
View File

@ -0,0 +1,58 @@
import { query } from '../mysql';
export interface Tag {
id: number;
created_at: Date;
modified_at: Date;
tag: string;
}
export class TagModel {
static async create(tag: Omit<Tag, 'id' | 'created_at' | 'modified_at'>): Promise<number> {
const sql = 'INSERT INTO tag (tag) VALUES (?)';
const params = [tag.tag];
const result: any = await query(sql, params);
return result.insertId;
}
static async getById(id: number): Promise<Tag | null> {
const sql = 'SELECT * FROM tag WHERE id = ?';
const params = [id];
const rows: any = await query(sql, params);
return rows.length > 0 ? rows[0] as Tag : null;
}
static async getByTag(tag: string): Promise<Tag | null> {
const sql = 'SELECT * FROM tag WHERE tag = ?';
const params = [tag];
const rows: any = await query(sql, params);
return rows.length > 0 ? rows[0] as Tag : null;
}
static async getAll(): Promise<Tag[]> {
const sql = 'SELECT * FROM tag';
const rows: any = await query(sql);
return rows as Tag[];
}
static async update(id: number, tag: Partial<Omit<Tag, 'id' | 'created_at' | 'modified_at'>>): Promise<boolean> {
const fields = Object.keys(tag);
const values = Object.values(tag);
if (fields.length === 0) {
return false;
}
const sql = `UPDATE tag SET ${fields.map(field => `${field} = ?`).join(', ')} WHERE id = ?`;
const params = [...values, id];
const result: any = await query(sql, params);
return result.affectedRows > 0;
}
static async delete(id: number): Promise<boolean> {
const sql = 'DELETE FROM tag WHERE id = ?';
const params = [id];
const result: any = await query(sql, params);
return result.affectedRows > 0;
}
}

46
src/lib/db/tag_video.ts Normal file
View File

@ -0,0 +1,46 @@
import { query } from '../mysql';
export interface TagVideo {
id: number;
created_at: Date;
modified_at: Date;
tag_id: number;
video_id: number;
}
export class TagVideoModel {
static async create(tagVideo: Omit<TagVideo, 'id' | 'created_at' | 'modified_at'>): Promise<number> {
const sql = 'INSERT INTO tag_video (tag_id, video_id) VALUES (?, ?)';
const params = [tagVideo.tag_id, tagVideo.video_id];
const result: any = await query(sql, params);
return result.insertId;
}
static async getByVideoId(videoId: number): Promise<TagVideo[]> {
const sql = 'SELECT * FROM tag_video WHERE video_id = ?';
const params = [videoId];
const rows: any = await query(sql, params);
return rows as TagVideo[];
}
static async getByTagId(tagId: number): Promise<TagVideo[]> {
const sql = 'SELECT * FROM tag_video WHERE tag_id = ?';
const params = [tagId];
const rows: any = await query(sql, params);
return rows as TagVideo[];
}
static async delete(id: number): Promise<boolean> {
const sql = 'DELETE FROM tag_video WHERE id = ?';
const params = [id];
const result: any = await query(sql, params);
return result.affectedRows > 0;
}
static async deleteByTagAndVideo(tagId: number, videoId: number): Promise<boolean> {
const sql = 'DELETE FROM tag_video WHERE tag_id = ? AND video_id = ?';
const params = [tagId, videoId];
const result: any = await query(sql, params);
return result.affectedRows > 0;
}
}

59
src/lib/db/video.ts Normal file
View File

@ -0,0 +1,59 @@
import { query } from '../mysql';
export interface Video {
id: number;
created_at: Date;
modified_at: Date;
genre: string;
sub_genre: string;
scene: string;
action: string;
camera: string;
image_prompt: string;
video_prompt: string;
image_path: string;
video_path: string;
}
export class VideoModel {
static async create(video: Omit<Video, 'id' | 'created_at' | 'modified_at'>): Promise<number> {
const sql = 'INSERT INTO video (genre, sub_genre, scene, action, camera, image_prompt, video_prompt, image_path, video_path) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)';
const params = [video.genre, video.sub_genre, video.scene, video.action, video.camera, video.image_prompt, video.video_prompt, video.image_path, video.video_path];
const result: any = await query(sql, params);
return result.insertId;
}
static async getById(id: number): Promise<Video | null> {
const sql = 'SELECT * FROM video WHERE id = ?';
const params = [id];
const rows: any = await query(sql, params);
return rows.length > 0 ? rows[0] as Video : null;
}
static async getAll(): Promise<Video[]> {
const sql = 'SELECT * FROM video';
const rows: any = await query(sql);
return rows as Video[];
}
static async update(id: number, video: Partial<Omit<Video, 'id' | 'created_at' | 'modified_at'>>): Promise<boolean> {
const fields = Object.keys(video);
const values = Object.values(video);
if (fields.length === 0) {
return false;
}
const sql = `UPDATE video SET ${fields.map(field => `${field} = ?`).join(', ')} WHERE id = ?`;
const params = [...values, id];
const result: any = await query(sql, params);
return result.affectedRows > 0;
}
static async delete(id: number): Promise<boolean> {
const sql = 'DELETE FROM video WHERE id = ?';
const params = [id];
const result: any = await query(sql, params);
return result.affectedRows > 0;
}
}

View File

@ -1,66 +1,7 @@
import fs from 'fs';
import path from 'path';
import dotenv from 'dotenv';
import { logger } from './logger';
dotenv.config();
const LLM_BASE_URL = process.env.LLM_BASE_URL;
import { callLMStudioWithFile } from './lmstudio';
async function describeImage(imagePath: string, prompt: string): Promise<any> {
if (!LLM_BASE_URL) {
throw new Error('LLM_BASE_URL is not defined in the .env file');
}
const imageBuffer = fs.readFileSync(imagePath);
const base64Image = imageBuffer.toString('base64');
for (let i = 0; i < 10; i++) {
let llmResponse = "";
try {
const requestUrl = new URL('v1/chat/completions', LLM_BASE_URL);
const response = await fetch(requestUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'local-model',
messages: [
{
role: 'user',
content: [
{ type: 'image_url', image_url: { url: `data:image/jpeg;base64,${base64Image}` } },
{ type: 'text', text: prompt },
],
},
],
temperature: 0.7,
}),
});
const data = await response.json();
if (data.choices && data.choices.length > 0) {
const content = data.choices[0].message.content;
llmResponse = content;
const jsonMatch = content.match(/\{[\s\S]*\}/);
if (jsonMatch) {
return JSON.parse(jsonMatch[0]);
}
} else {
logger.error('Unexpected API response:', data);
}
} catch (error) {
logger.error(`Attempt ${i + 1} failed:`, error);
if (error instanceof TypeError && error.message.includes('fetch failed')) {
logger.error('Could not connect to the LM Studio server. Please ensure the server is running and accessible at the specified LLM_BASE_URL.');
}
logger.debug(`LLM response: ${llmResponse}`)
}
}
throw new Error('Failed to describe image after 10 attempts');
return await callLMStudioWithFile(imagePath, prompt);
}
export { describeImage };

View File

@ -5,9 +5,6 @@ import dotenv from 'dotenv';
dotenv.config();
const COMFY_BASE_URL = process.env.COMFY_BASE_URL?.replace(/\/$/, '');
const COMFY_OUTPUT_DIR = process.env.COMFY_OUTPUT_DIR;
interface ImageSize {
width: number;
height: number;
@ -16,9 +13,13 @@ interface ImageSize {
async function generateImage(
prompt: string,
newFileName: string,
comfyBaseUrl: string,
comfyOutputDir: string,
imageModel: 'qwen' | 'flux' = 'qwen',
size: ImageSize = { width: 720, height: 1280 }
): Promise<string> {
const COMFY_BASE_URL = comfyBaseUrl.replace(/\/$/, '');
const COMFY_OUTPUT_DIR = comfyOutputDir;
let workflow;
if (imageModel === 'qwen') {
workflow = JSON.parse(await fs.readFile('src/comfyworkflows/generate_image.json', 'utf-8'));

115
src/lib/lmstudio.ts Normal file
View File

@ -0,0 +1,115 @@
import fs from 'fs';
import dotenv from 'dotenv';
import { logger } from './logger';
dotenv.config();
const LLM_BASE_URL = process.env.LLM_BASE_URL;
async function callLMStudio(prompt: string): Promise<any> {
if (!LLM_BASE_URL) {
throw new Error('LLM_BASE_URL is not defined in the .env file');
}
for (let i = 0; i < 10; i++) {
let llmResponse = "";
try {
const requestUrl = new URL('v1/chat/completions', LLM_BASE_URL);
const response = await fetch(requestUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'local-model',
messages: [
{
role: 'user',
content: prompt,
},
],
temperature: 0.7,
}),
});
const data = await response.json();
if (data.choices && data.choices.length > 0) {
const content = data.choices[0].message.content;
llmResponse = content;
const jsonMatch = content.match(/\{[\s\S]*\}/);
if (jsonMatch) {
return JSON.parse(jsonMatch[0]);
}
} else {
logger.error('Unexpected API response:', data);
}
} catch (error) {
logger.error(`Attempt ${i + 1} failed:`, error);
if (error instanceof TypeError && error.message.includes('fetch failed')) {
logger.error('Could not connect to the LM Studio server. Please ensure the server is running and accessible at the specified LLM_BASE_URL.');
}
logger.debug(`LLM response: ${llmResponse}`)
}
}
throw new Error('Failed to get response from LLM after 10 attempts');
}
async function callLMStudioWithFile(imagePath: string, prompt: string): Promise<any> {
if (!LLM_BASE_URL) {
throw new Error('LLM_BASE_URL is not defined in the .env file');
}
const imageBuffer = fs.readFileSync(imagePath);
const base64Image = imageBuffer.toString('base64');
for (let i = 0; i < 10; i++) {
let llmResponse = "";
try {
const requestUrl = new URL('v1/chat/completions', LLM_BASE_URL);
const response = await fetch(requestUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'local-model',
messages: [
{
role: 'user',
content: [
{ type: 'image_url', image_url: { url: `data:image/jpeg;base64,${base64Image}` } },
{ type: 'text', text: prompt },
],
},
],
temperature: 0.7,
}),
});
const data = await response.json();
if (data.choices && data.choices.length > 0) {
const content = data.choices[0].message.content;
llmResponse = content;
const jsonMatch = content.match(/\{[\s\S]*\}/);
if (jsonMatch) {
return JSON.parse(jsonMatch[0]);
}
} else {
logger.error('Unexpected API response:', data);
}
} catch (error) {
logger.error(`Attempt ${i + 1} failed:`, error);
if (error instanceof TypeError && error.message.includes('fetch failed')) {
logger.error('Could not connect to the LM Studio server. Please ensure the server is running and accessible at the specified LLM_BASE_URL.');
}
logger.debug(`LLM response: ${llmResponse}`)
}
}
throw new Error('Failed to describe image after 10 attempts');
}
export { callLMStudio, callLMStudioWithFile };

33
src/lib/mysql.ts Normal file
View File

@ -0,0 +1,33 @@
import mysql from 'mysql2/promise';
import dotenv from 'dotenv';
dotenv.config();
const pool = mysql.createPool({
host: process.env.MYSQL_HOST,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWOWD,
database: process.env.MYSQL_DB,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
});
export async function query(sql: string, params: any[] = []) {
const [rows] = await pool.execute(sql, params);
return rows;
}
export async function testConnection() {
try {
const connection = await pool.getConnection();
console.log('Successfully connected to the database.');
connection.release();
return true;
} catch (error) {
console.error('Failed to connect to the database:', error);
return false;
}
}
export default pool;

View File

@ -5,9 +5,6 @@ import dotenv from 'dotenv';
dotenv.config();
const COMFY_BASE_URL = process.env.COMFY_BASE_URL?.replace(/\/$/, '');
const COMFY_OUTPUT_DIR = process.env.COMFY_OUTPUT_DIR;
interface VideoSize {
width: number;
height: number;
@ -17,8 +14,12 @@ async function generateVideo(
prompt: string,
imagePath: string,
newFileName: string,
comfyBaseUrl: string,
comfyOutputDir: string,
size: VideoSize = { width: 720, height: 1280 }
): Promise<string> {
const COMFY_BASE_URL = comfyBaseUrl.replace(/\/$/, '');
const COMFY_OUTPUT_DIR = comfyOutputDir;
const workflow = JSON.parse(await fs.readFile('src/comfyworkflows/generate_video.json', 'utf-8'));
workflow['6']['inputs']['text'] = prompt;
workflow['52']['inputs']['image'] = imagePath;