@xuda.io/ai_module 1.1.4308 → 1.1.4309

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.mjs +38 -1
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -6739,6 +6739,25 @@ const get_business_logo = async function (uid, name, account_profile_info) {
6739
6739
 
6740
6740
  const url = ret.data;
6741
6741
  const file = await return_image_blob_from_downloaded_image(url);
6742
+ // const imageBase64 = Buffer.from(await file.image_blob.arrayBuffer()).toString('base64');
6743
+ const model = 'chatgpt-image-latest';
6744
+ const prompt = `
6745
+ extract the logo from the image
6746
+ `;
6747
+ const logo_response = await client.images.edit({
6748
+ model,
6749
+ image: file.image_blob, //logo
6750
+ prompt,
6751
+ background: 'transparent',
6752
+ });
6753
+ record_chat_usage(uid, logo_response.usage.input_tokens, logo_response.usage.output_tokens, 'business logo', prompt, model, { url }, account_profile_info);
6754
+ imageBase64 = await normalizeBase64To1024(ai_avatar_response.data[0].b64_json);
6755
+
6756
+ const outputBuffer = Buffer.from(imageBase64, 'base64');
6757
+ const outputPath = file.tempOutputPath;
6758
+ await fs.promises.writeFile(outputPath, outputBuffer);
6759
+
6760
+ console.log('Success! Saved →', outputPath);
6742
6761
 
6743
6762
  console.log(file);
6744
6763
  // prompt: `find "${name}" logo base on the following facts "${facts}", Return only png,jpg img tag , nothing else`,
@@ -6750,6 +6769,24 @@ const get_business_logo = async function (uid, name, account_profile_info) {
6750
6769
  // } catch (error) {}
6751
6770
  };
6752
6771
 
6772
+ async function normalizeBase64To1024(base64Image) {
6773
+ const inputBuffer = Buffer.from(base64Image, 'base64');
6774
+
6775
+ const normalizedBuffer = await sharp(inputBuffer)
6776
+ .resize(1024, 1024, {
6777
+ fit: 'contain', // Preserve aspect ratio, fit within 211x211
6778
+ background: { r: 0, g: 0, b: 0, alpha: 0 }, // Transparent background for padding
6779
+ })
6780
+ .png({
6781
+ quality: 100,
6782
+ compressionLevel: 9,
6783
+ force: true,
6784
+ })
6785
+ .toBuffer();
6786
+
6787
+ return normalizedBuffer.toString('base64'); // Return normalized base64 string
6788
+ }
6789
+
6753
6790
  const return_image_blob_from_downloaded_image = async function (url) {
6754
6791
  async function hasTransparentBackground(filePath) {
6755
6792
  const image = sharp(filePath);
@@ -6840,7 +6877,7 @@ const return_image_blob_from_downloaded_image = async function (url) {
6840
6877
  return { is_transparent: img_ret.is_transparent, image_blob: blob };
6841
6878
  }),
6842
6879
  );
6843
- return images[0];
6880
+ return { image_blob: images[0].image_blob, is_transparent: images[0].is_transparent, tempOutputPath };
6844
6881
  } catch (err) {
6845
6882
  console.error(err);
6846
6883
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/ai_module",
3
- "version": "1.1.4308",
3
+ "version": "1.1.4309",
4
4
  "description": "Xuda AI Module",
5
5
  "main": "index.mjs",
6
6
  "type": "module",