yuanjiajun commited on
Commit
b118dd1
·
1 Parent(s): dc7ec9b
src/service/article-service.ts CHANGED
@@ -12,7 +12,6 @@ async function getImageBufferByText(text: string, apiKey: string) {
12
  console.log(`translatedText:${translatedText}`);
13
  const summaryText = await retryAsync(() => getSummaryText(translatedText, apiKey));
14
  console.log(`summaryText:${summaryText}`);
15
- // 使用axios以async/await形式调用API接口生成图片
16
  const buffer = await retryAsync(() => getImageBuffer(summaryText, apiKey));
17
  console.log('------------- 图片buffer获取成功 ---------------');
18
  return buffer;
@@ -20,7 +19,7 @@ async function getImageBufferByText(text: string, apiKey: string) {
20
 
21
  async function preheat(apiKey: string) {
22
  try {
23
- await retryAsync(() => getImageBufferByText('预热', apiKey));
24
  } catch (error) {}
25
  }
26
 
 
12
  console.log(`translatedText:${translatedText}`);
13
  const summaryText = await retryAsync(() => getSummaryText(translatedText, apiKey));
14
  console.log(`summaryText:${summaryText}`);
 
15
  const buffer = await retryAsync(() => getImageBuffer(summaryText, apiKey));
16
  console.log('------------- 图片buffer获取成功 ---------------');
17
  return buffer;
 
19
 
20
  async function preheat(apiKey: string) {
21
  try {
22
+ await getImageBufferByText('一个苹果', apiKey);
23
  } catch (error) {}
24
  }
25
 
src/utils/common.ts CHANGED
@@ -6,14 +6,15 @@ export async function delay(time: number) {
6
  console.log(`延迟${time}毫秒后执行`);
7
  }
8
 
9
- export async function retryAsync(func: () => Promise<any>, x = 3, timeout = 10 * 1000) {
10
  let attempts = 0;
11
  while (attempts < x) {
12
  try {
13
  return await func();
14
  } catch (error) {
 
15
  attempts++;
16
- delay(timeout);
17
  }
18
  }
19
  throw new Error(`Async function failed after ${x} attempts.`);
 
6
  console.log(`延迟${time}毫秒后执行`);
7
  }
8
 
9
+ export async function retryAsync(func: () => Promise<any>, x = 3, timeout = 5 * 1000) {
10
  let attempts = 0;
11
  while (attempts < x) {
12
  try {
13
  return await func();
14
  } catch (error) {
15
+ console.log(`第${attempts + 1}次尝试失败:${error}`);
16
  attempts++;
17
+ await delay(timeout);
18
  }
19
  }
20
  throw new Error(`Async function failed after ${x} attempts.`);
src/utils/file-utils.ts CHANGED
@@ -69,22 +69,26 @@ export async function downloadDocx(url: string, fileName: string) {
69
  }
70
 
71
  export async function getImageBuffer(text: string, apiKey: string) {
72
- const response = await axios.post(
73
- 'https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev',
74
- {
75
- inputs: text,
76
- options: {
77
- use_cache: false,
 
 
78
  },
79
- },
80
- {
81
- headers: {
82
- 'Content-Type': 'application/json',
83
- Authorization: `Bearer ${apiKey}`,
 
84
  },
85
- responseType: 'arraybuffer', // 设置响应类型为 blob
86
- },
87
- );
88
- const arrayBuffer = response.data as ArrayBuffer;
89
- return Buffer.from(arrayBuffer);
 
90
  }
 
69
  }
70
 
71
  export async function getImageBuffer(text: string, apiKey: string) {
72
+ try {
73
+ const response = await axios.post(
74
+ 'https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev',
75
+ {
76
+ inputs: text,
77
+ options: {
78
+ use_cache: true,
79
+ },
80
  },
81
+ {
82
+ headers: {
83
+ 'Content-Type': 'application/json',
84
+ Authorization: `Bearer ${apiKey}`,
85
+ },
86
+ responseType: 'arraybuffer', // 设置响应类型为 blob
87
  },
88
+ );
89
+ const arrayBuffer = response.data as ArrayBuffer;
90
+ return Buffer.from(arrayBuffer);
91
+ } catch (error) {
92
+ throw new Error(`生成图片失败,错误信息:${error}`);
93
+ }
94
  }
src/utils/hugging-face.ts CHANGED
@@ -1,36 +1,44 @@
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
  }
 
1
  import axios from 'axios';
2
 
3
  export async function getTranslatedText(text: string, apiKey: string) {
4
+ try {
5
+ const translatedData = await axios.post(
6
+ 'https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-zh-en',
7
+ { inputs: text, src_lang: 'ch', target_lang: 'en' },
8
+ {
9
+ headers: {
10
+ 'Content-Type': 'application/json',
11
+ // 如果需要认证
12
+ Authorization: `Bearer ${apiKey}`,
13
+ },
14
  },
15
+ );
16
+ const translatedText = translatedData.data[0].translation_text as string;
17
+
18
+ return translatedText;
19
+ } catch (error) {
20
+ throw new Error(`翻译失败,错误信息:${error}`);
21
+ }
22
  }
23
 
24
  export async function getSummaryText(text: string, apiKey: string) {
25
+ try {
26
+ const summaryData = await axios.post(
27
+ 'https://api-inference.huggingface.co/models/csebuetnlp/mT5_multilingual_XLSum',
28
+ { inputs: text },
29
+ {
30
+ headers: {
31
+ 'Content-Type': 'application/json',
32
+ // 如果需要认证
33
+ Authorization: `Bearer ${apiKey}`,
34
+ },
35
  },
36
+ );
37
+
38
+ const summaryText = summaryData.data[0].summary_text as string;
39
+
40
+ return summaryText;
41
+ } catch (error) {
42
+ throw new Error(`总结失败,错误信息:${error}`);
43
+ }
44
  }