yuanjiajun commited on
Commit ·
b3d0047
1
Parent(s): c13766b
测试
Browse files- src/service/article-service.ts +16 -16
- src/utils/common.ts +9 -35
- src/utils/file-utils.ts +15 -17
- src/utils/hugging-face.ts +36 -0
- src/utils/index.ts +1 -0
src/service/article-service.ts
CHANGED
|
@@ -2,20 +2,28 @@ import fs from 'fs';
|
|
| 2 |
import htmlDocx from 'html-docx-js';
|
| 3 |
import path from 'path';
|
| 4 |
|
| 5 |
-
import { delay, getImageBuffer, getSummaryText, getTranslatedText } from '@/utils';
|
| 6 |
|
| 7 |
const uploadDir = path.join(__dirname, '../../uploads');
|
| 8 |
|
| 9 |
async function getImageBufferByText(text: string, apiKey: string) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
// 使用axios以async/await形式调用API接口生成图片
|
| 11 |
-
const
|
| 12 |
-
|
| 13 |
-
console.log(summaryText);
|
| 14 |
-
// 使用axios以async/await形式调用API接口生成图片
|
| 15 |
-
const buffer = await getImageBuffer(summaryText, apiKey);
|
| 16 |
return buffer;
|
| 17 |
}
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
export const processArticleServe = async (data: { title: string; content: string; config: { output: string; hfApiKey: string } }) => {
|
| 20 |
const { title, content, config } = data;
|
| 21 |
|
|
@@ -24,10 +32,7 @@ export const processArticleServe = async (data: { title: string; content: string
|
|
| 24 |
|
| 25 |
let htmlWithImages = '';
|
| 26 |
|
| 27 |
-
|
| 28 |
-
try {
|
| 29 |
-
await getImageBufferByText('预热', config.hfApiKey);
|
| 30 |
-
} catch (error) {}
|
| 31 |
|
| 32 |
// 遍历段落,根据段落内容调用接口生成图片并插入到HTML中
|
| 33 |
for (let i = 0; i < paragraphs.length; i++) {
|
|
@@ -84,13 +89,8 @@ export const processArticleServe = async (data: { title: string; content: string
|
|
| 84 |
const outputFilename = `docx-${Date.now()}.docx`;
|
| 85 |
const outputPath = path.join(uploadDir, outputFilename);
|
| 86 |
|
| 87 |
-
const
|
| 88 |
-
// 将 Blob 转换为 ArrayBuffer
|
| 89 |
-
const arrayBuffer = await docxBlob.arrayBuffer();
|
| 90 |
-
// 将 ArrayBuffer 转换为 Buffer
|
| 91 |
const docxBuffer = Buffer.from(arrayBuffer);
|
| 92 |
-
|
| 93 |
-
// 将 DOCX Buffer 保存为文件
|
| 94 |
fs.writeFileSync(outputPath, docxBuffer);
|
| 95 |
|
| 96 |
return {
|
|
|
|
| 2 |
import htmlDocx from 'html-docx-js';
|
| 3 |
import path from 'path';
|
| 4 |
|
| 5 |
+
import { delay, getImageBuffer, getSummaryText, getTranslatedText, retryOnce } from '@/utils';
|
| 6 |
|
| 7 |
const uploadDir = path.join(__dirname, '../../uploads');
|
| 8 |
|
| 9 |
async function getImageBufferByText(text: string, apiKey: string) {
|
| 10 |
+
console.log(`------------ 开始获取图片,原文案:${text} ---------------`);
|
| 11 |
+
const translatedText = await retryOnce(() => getTranslatedText(text, apiKey));
|
| 12 |
+
console.log(`translatedText:${translatedText}`);
|
| 13 |
+
const summaryText = await retryOnce(() => getSummaryText(translatedText, apiKey));
|
| 14 |
+
console.log(`summaryText:${summaryText}`);
|
| 15 |
// 使用axios以async/await形式调用API接口生成图片
|
| 16 |
+
const buffer = await retryOnce(() => getImageBuffer(summaryText, apiKey));
|
| 17 |
+
console.log('------------- 图片buffer获取成功 ---------------');
|
|
|
|
|
|
|
|
|
|
| 18 |
return buffer;
|
| 19 |
}
|
| 20 |
|
| 21 |
+
async function preheat(apiKey: string) {
|
| 22 |
+
try {
|
| 23 |
+
await retryOnce(() => getImageBufferByText('预热', apiKey));
|
| 24 |
+
} catch (error) {}
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
export const processArticleServe = async (data: { title: string; content: string; config: { output: string; hfApiKey: string } }) => {
|
| 28 |
const { title, content, config } = data;
|
| 29 |
|
|
|
|
| 32 |
|
| 33 |
let htmlWithImages = '';
|
| 34 |
|
| 35 |
+
await preheat(config.hfApiKey);
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
// 遍历段落,根据段落内容调用接口生成图片并插入到HTML中
|
| 38 |
for (let i = 0; i < paragraphs.length; i++) {
|
|
|
|
| 89 |
const outputFilename = `docx-${Date.now()}.docx`;
|
| 90 |
const outputPath = path.join(uploadDir, outputFilename);
|
| 91 |
|
| 92 |
+
const arrayBuffer = htmlDocx.asBlob(htmlContent) as ArrayBuffer;
|
|
|
|
|
|
|
|
|
|
| 93 |
const docxBuffer = Buffer.from(arrayBuffer);
|
|
|
|
|
|
|
| 94 |
fs.writeFileSync(outputPath, docxBuffer);
|
| 95 |
|
| 96 |
return {
|
src/utils/common.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
import axios from 'axios';
|
| 2 |
import util from 'util';
|
| 3 |
const setTimeoutPromise = util.promisify(setTimeout);
|
| 4 |
|
|
@@ -7,38 +6,13 @@ export async function delay(time: number) {
|
|
| 7 |
console.log(`延迟${time}毫秒后执行`);
|
| 8 |
}
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
{
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
Authorization: `Bearer ${apiKey}`,
|
| 20 |
-
}
|
| 21 |
-
},
|
| 22 |
-
);
|
| 23 |
-
const translatedText = translatedData.data[0].translation_text as string;
|
| 24 |
-
|
| 25 |
-
return translatedText
|
| 26 |
}
|
| 27 |
-
|
| 28 |
-
export async function getSummaryText(text:string, apiKey:string){
|
| 29 |
-
const summaryData = await axios.post(
|
| 30 |
-
'https://api-inference.huggingface.co/models/csebuetnlp/mT5_multilingual_XLSum',
|
| 31 |
-
{ inputs: text },
|
| 32 |
-
{
|
| 33 |
-
headers: {
|
| 34 |
-
'Content-Type': 'application/json',
|
| 35 |
-
// 如果需要认证
|
| 36 |
-
Authorization: `Bearer ${apiKey}`,
|
| 37 |
-
}
|
| 38 |
-
},
|
| 39 |
-
);
|
| 40 |
-
|
| 41 |
-
const summaryText = summaryData.data[0].summary_text as string;
|
| 42 |
-
|
| 43 |
-
return summaryText
|
| 44 |
-
}
|
|
|
|
|
|
|
| 1 |
import util from 'util';
|
| 2 |
const setTimeoutPromise = util.promisify(setTimeout);
|
| 3 |
|
|
|
|
| 6 |
console.log(`延迟${time}毫秒后执行`);
|
| 7 |
}
|
| 8 |
|
| 9 |
+
export async function retryOnce(func: () => Promise<any>, timeout = 10 * 1000) {
|
| 10 |
+
try {
|
| 11 |
+
return await func();
|
| 12 |
+
} catch (error) {
|
| 13 |
+
await delay(timeout);
|
| 14 |
+
return await func().catch((error) => {
|
| 15 |
+
throw `重试失败:${error.message}`;
|
| 16 |
+
});
|
| 17 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/utils/file-utils.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import fs from 'fs';
|
| 2 |
import os from 'os';
|
| 3 |
import path from 'path';
|
|
@@ -72,25 +73,22 @@ export async function downloadDocx(url: string, fileName: string) {
|
|
| 72 |
}
|
| 73 |
|
| 74 |
export async function getImageBuffer(text: string, apiKey: string) {
|
| 75 |
-
const response = await
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
// 如果需要认证
|
| 79 |
-
Authorization: `Bearer ${apiKey}`,
|
| 80 |
-
},
|
| 81 |
-
method: 'POST',
|
| 82 |
-
body: JSON.stringify({
|
| 83 |
inputs: text,
|
| 84 |
options: {
|
| 85 |
use_cache: false,
|
| 86 |
},
|
| 87 |
-
}
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
| 96 |
}
|
|
|
|
| 1 |
+
import axios from 'axios';
|
| 2 |
import fs from 'fs';
|
| 3 |
import os from 'os';
|
| 4 |
import path from 'path';
|
|
|
|
| 73 |
}
|
| 74 |
|
| 75 |
export async function getImageBuffer(text: string, apiKey: string) {
|
| 76 |
+
const response = await axios.post(
|
| 77 |
+
'https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev',
|
| 78 |
+
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
inputs: text,
|
| 80 |
options: {
|
| 81 |
use_cache: false,
|
| 82 |
},
|
| 83 |
+
},
|
| 84 |
+
{
|
| 85 |
+
headers: {
|
| 86 |
+
'Content-Type': 'application/json',
|
| 87 |
+
Authorization: `Bearer ${apiKey}`,
|
| 88 |
+
},
|
| 89 |
+
responseType: 'arraybuffer', // 设置响应类型为 blob
|
| 90 |
+
},
|
| 91 |
+
);
|
| 92 |
+
const arrayBuffer = response.data as ArrayBuffer;
|
| 93 |
+
return Buffer.from(arrayBuffer);
|
| 94 |
}
|
src/utils/hugging-face.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import axios from 'axios';
|
| 2 |
+
|
| 3 |
+
export async function getTranslatedText(text: string, apiKey: string) {
|
| 4 |
+
const translatedData = await axios.post(
|
| 5 |
+
'https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-zh-en',
|
| 6 |
+
{ inputs: text, src_lang: 'ch', target_lang: 'en' },
|
| 7 |
+
{
|
| 8 |
+
headers: {
|
| 9 |
+
'Content-Type': 'application/json',
|
| 10 |
+
// 如果需要认证
|
| 11 |
+
Authorization: `Bearer ${apiKey}`,
|
| 12 |
+
},
|
| 13 |
+
},
|
| 14 |
+
);
|
| 15 |
+
const translatedText = translatedData.data[0].translation_text as string;
|
| 16 |
+
|
| 17 |
+
return translatedText;
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
export async function getSummaryText(text: string, apiKey: string) {
|
| 21 |
+
const summaryData = await axios.post(
|
| 22 |
+
'https://api-inference.huggingface.co/models/csebuetnlp/mT5_multilingual_XLSum',
|
| 23 |
+
{ inputs: text },
|
| 24 |
+
{
|
| 25 |
+
headers: {
|
| 26 |
+
'Content-Type': 'application/json',
|
| 27 |
+
// 如果需要认证
|
| 28 |
+
Authorization: `Bearer ${apiKey}`,
|
| 29 |
+
},
|
| 30 |
+
},
|
| 31 |
+
);
|
| 32 |
+
|
| 33 |
+
const summaryText = summaryData.data[0].summary_text as string;
|
| 34 |
+
|
| 35 |
+
return summaryText;
|
| 36 |
+
}
|
src/utils/index.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
export * from './common';
|
| 2 |
export * from './file-utils';
|
|
|
|
|
|
| 1 |
export * from './common';
|
| 2 |
export * from './file-utils';
|
| 3 |
+
export * from './hugging-face';
|