feat: Add Jimeng image generation service and related functionality
- Implemented JimengService for image generation with API integration. - Created OSSService for handling image uploads to S3. - Developed PBService for managing PocketBase interactions. - Added task management for image creation and downloading using BullMQ. - Introduced routes for creating image generation tasks. - Implemented logging and error handling for image processing. - Added configuration management for Redis and other services. - Created scripts for testing image generation and PocketBase integration. - Updated package dependencies and added new scripts for worker management.
This commit is contained in:
12
prompts/test/common.ts
Normal file
12
prompts/test/common.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { app } from '../src/index.ts'
|
||||
|
||||
export {
|
||||
app
|
||||
}
|
||||
|
||||
const res = await app.run({
|
||||
path: 'image-creator',
|
||||
key: 'create-task',
|
||||
})
|
||||
|
||||
console.log('Route run result:', res)
|
||||
18
prompts/test/jimeng.ts
Normal file
18
prompts/test/jimeng.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { JimengService } from '../src/services/jimeng.service.js';
|
||||
import { useConfig } from '@kevisual/use-config';
|
||||
const config = useConfig();
|
||||
|
||||
export const jimengService = new JimengService({
|
||||
apiKey: config.JIMENG_API_KEY,
|
||||
baseUrl: config.JIMENG_API_URL,
|
||||
timeout: parseInt(config.JIMENG_TIMEOUT || '30000'),
|
||||
});
|
||||
|
||||
const createImage = async () => {
|
||||
const response = await jimengService.generateImage({
|
||||
prompt: 'A beautiful landscape with mountains and a river, in the style of a watercolor painting',
|
||||
});
|
||||
console.log('Generated Image URL:', response);
|
||||
};
|
||||
|
||||
createImage();
|
||||
43
prompts/test/pb.ts
Normal file
43
prompts/test/pb.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { createStorage } from 'unstorage';
|
||||
import { config } from '../src/app.ts'
|
||||
|
||||
import { PBService } from '../src/services/pb.service.ts'
|
||||
import path from "node:path";
|
||||
import fsDriver from "unstorage/drivers/fs";
|
||||
|
||||
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||
|
||||
const storage = createStorage({
|
||||
driver: fsDriver({ base: 'storage' }),
|
||||
});
|
||||
const pbService = new PBService({
|
||||
url: config.POCKETBASE_URL,
|
||||
token: config.POCKETBASE_TOKEN,
|
||||
});
|
||||
|
||||
async function main() {
|
||||
// const listStorage = await storage.getKeys();
|
||||
// const keys = listStorage.filter(key => key !== 'usage.json');
|
||||
// for (const key of keys) {
|
||||
// const value = await storage.getItem<any>(key);
|
||||
// console.log(`Generating PB record for key: ${value}`, value);
|
||||
// const { id, perfect: description, value: summary } = value;
|
||||
// pbService.collection.create({
|
||||
// title: '',
|
||||
// summary,
|
||||
// description,
|
||||
// tags: [],
|
||||
// data: {},
|
||||
// status: '计划中',
|
||||
// });
|
||||
// console.log(`Created record for prompt ID: ${id}`);
|
||||
// await sleep(100); // To avoid hitting rate limits
|
||||
// }
|
||||
|
||||
const list = await pbService.collection.getFullList({
|
||||
sort: '-created',
|
||||
fields: 'id,title,summary,description,tags,status',
|
||||
})
|
||||
console.log('PocketBase Records:', list.length);
|
||||
}
|
||||
main();
|
||||
15
prompts/test/upload-image.ts
Normal file
15
prompts/test/upload-image.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { jimengService, ossService } from '../src/index.ts'
|
||||
const url = 'https://p3-dreamina-sign.byteimg.com/tos-cn-i-tb4s082cfz/4947076125c64a999e4c392de03048f8~tplv-tb4s082cfz-aigc_resize:360:360.webp?lk3s=43402efa&x-expires=1770336000&x-signature=Fjeeb3qloxxzxmHJpmqu6v8fwrM%3D&format=.webp'
|
||||
|
||||
|
||||
const uploadImage = async () => {
|
||||
const response = await jimengService.downloadImage(url);
|
||||
const filename = `uploaded_image_${Date.now()}.png`;
|
||||
await ossService.putObject(filename, response);
|
||||
const ossUrl = ossService.getLink(filename);
|
||||
// console.log('Uploaded Image URL:', response)
|
||||
// const uploadJons = await ossService.putObject('a1.json', { b: '123' })
|
||||
// console.log('Upload JSON Result:', uploadJons)
|
||||
return ossUrl;
|
||||
}
|
||||
uploadImage();
|
||||
Reference in New Issue
Block a user