yuanjiajun commited on
Commit ·
04caba1
1
Parent(s): 76ca19f
feat: 使用qw
Browse files- src/service/article-service.ts +20 -13
- src/utils/common.ts +18 -0
src/service/article-service.ts
CHANGED
|
@@ -2,26 +2,33 @@ import fs from 'fs';
|
|
| 2 |
import htmlDocx from 'html-docx-js';
|
| 3 |
import path from 'path';
|
| 4 |
|
| 5 |
-
import { delay, getImageBuffer,
|
| 6 |
|
| 7 |
const uploadDir = path.join(__dirname, '../../uploads');
|
| 8 |
|
| 9 |
async function getImageBufferByText(text: string, apiKey: string) {
|
| 10 |
console.log(`------------ 开始获取图片,原文案:${text} ---------------`);
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
console.log('------------- 图片buffer获取成功 ---------------');
|
| 17 |
return buffer;
|
| 18 |
}
|
| 19 |
|
| 20 |
-
async function preheat(apiKey: string) {
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
}
|
| 25 |
|
| 26 |
export const processArticleServe = async (data: { title: string; content: string; config: { output: string; hfApiKey: string } }) => {
|
| 27 |
const { title, content, config } = data;
|
|
@@ -31,11 +38,11 @@ export const processArticleServe = async (data: { title: string; content: string
|
|
| 31 |
|
| 32 |
let htmlWithImages = '';
|
| 33 |
|
| 34 |
-
await preheat(config.hfApiKey);
|
| 35 |
|
| 36 |
// 遍历段落,根据段落内容调用接口生成图片并插入到HTML中
|
| 37 |
for (let i = 0; i < paragraphs.length; i++) {
|
| 38 |
-
|
| 39 |
|
| 40 |
// 确定用于生成图片的描述内容,除了第一段,其余为上一段的内容
|
| 41 |
const description = i === 0 ? paragraphs[i] : paragraphs[i - 1];
|
|
|
|
| 2 |
import htmlDocx from 'html-docx-js';
|
| 3 |
import path from 'path';
|
| 4 |
|
| 5 |
+
import { delay, getImageBuffer, requestQw } from '@/utils';
|
| 6 |
|
| 7 |
const uploadDir = path.join(__dirname, '../../uploads');
|
| 8 |
|
| 9 |
async function getImageBufferByText(text: string, apiKey: string) {
|
| 10 |
console.log(`------------ 开始获取图片,原文案:${text} ---------------`);
|
| 11 |
+
|
| 12 |
+
const summaryTranslatedText = await requestQw({
|
| 13 |
+
"messages": [
|
| 14 |
+
{
|
| 15 |
+
"role": "user",
|
| 16 |
+
"content": `将以下内容简化内容提炼核心并翻译为英语,控制在八个单词内:${text}`
|
| 17 |
+
}
|
| 18 |
+
],
|
| 19 |
+
"model": "qwen-plus"
|
| 20 |
+
})
|
| 21 |
+
console.log(`summaryTranslatedText:${summaryTranslatedText}`);
|
| 22 |
+
const buffer = await getImageBuffer(summaryTranslatedText, apiKey)
|
| 23 |
console.log('------------- 图片buffer获取成功 ---------------');
|
| 24 |
return buffer;
|
| 25 |
}
|
| 26 |
|
| 27 |
+
// async function preheat(apiKey: string) {
|
| 28 |
+
// try {
|
| 29 |
+
// await getImageBufferByText('一个苹果', apiKey);
|
| 30 |
+
// } catch (error) {}
|
| 31 |
+
// }
|
| 32 |
|
| 33 |
export const processArticleServe = async (data: { title: string; content: string; config: { output: string; hfApiKey: string } }) => {
|
| 34 |
const { title, content, config } = data;
|
|
|
|
| 38 |
|
| 39 |
let htmlWithImages = '';
|
| 40 |
|
| 41 |
+
// await preheat(config.hfApiKey);
|
| 42 |
|
| 43 |
// 遍历段落,根据段落内容调用接口生成图片并插入到HTML中
|
| 44 |
for (let i = 0; i < paragraphs.length; i++) {
|
| 45 |
+
await delay(10 * 1000);
|
| 46 |
|
| 47 |
// 确定用于生成图片的描述内容,除了第一段,其余为上一段的内容
|
| 48 |
const description = i === 0 ? paragraphs[i] : paragraphs[i - 1];
|
src/utils/common.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import util from 'util';
|
| 2 |
const setTimeoutPromise = util.promisify(setTimeout);
|
| 3 |
|
|
@@ -19,3 +20,20 @@ export async function retryAsync(func: () => Promise<any>, x = 3, timeout = 10 *
|
|
| 19 |
}
|
| 20 |
throw new Error(`Async function failed after ${x} attempts.`);
|
| 21 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import axios from 'axios';
|
| 2 |
import util from 'util';
|
| 3 |
const setTimeoutPromise = util.promisify(setTimeout);
|
| 4 |
|
|
|
|
| 20 |
}
|
| 21 |
throw new Error(`Async function failed after ${x} attempts.`);
|
| 22 |
}
|
| 23 |
+
|
| 24 |
+
export async function requestQw(data:any) {
|
| 25 |
+
const qwToken = 'sBy1ogROHqapzX0CcdoyjCl$7wAX1NzRpOPRUMrQGgN8J7jSxmMQWreOgkeheTFbylzpf8Gz1g_n0'
|
| 26 |
+
|
| 27 |
+
const response = await axios.post(
|
| 28 |
+
'https://Joey7938-joe-qw-api.hf.space/api/chat/completions',
|
| 29 |
+
data,
|
| 30 |
+
{
|
| 31 |
+
headers: {
|
| 32 |
+
'Content-Type': 'application/json',
|
| 33 |
+
Authorization: `Bearer ${qwToken}`,
|
| 34 |
+
},
|
| 35 |
+
},
|
| 36 |
+
);
|
| 37 |
+
|
| 38 |
+
return response.data.choices[0].message.content;
|
| 39 |
+
}
|