Khrisna commited on
Commit
fa70cf8
·
verified ·
1 Parent(s): 886a675

Update lib/diffusion.js

Browse files
Files changed (1) hide show
  1. lib/diffusion.js +45 -34
lib/diffusion.js CHANGED
@@ -1,43 +1,54 @@
1
  import axios from "axios";
2
 
3
  async function stablediff(prompt) {
4
- const BASE_URL = "https://stabilityai-stable-diffusion-3-medium.hf.space/";
5
- const session_hash = Math.random().toString(36).substring(2).slice(1);
6
-
7
- const resp = await axios({
8
- method: "POST",
9
- url: BASE_URL + "queue/join?__theme=light",
10
- data: {
11
- data: [
12
- prompt,
13
- "",
14
- 0,
15
- true,
16
- 1024,
17
- 1024,
18
- 5,
19
- 28
20
- ],
21
- event_data: null,
22
- fn_index: 1,
23
- session_hash,
24
- trigger_id: 4
25
- }
26
- });
27
-
28
- if(resp.data.event_id) {
29
- const response = await axios({
30
- method: "GET",
31
- url: BASE_URL + "queue/data?session_hash=" + session_hash
32
  });
33
- const splited = response.data.split("\n");
34
- const processStartsLine = splited.find(line => line.includes('process_completed'));
35
- const processStartsData = JSON.parse(processStartsLine.replace('data: ', ''));
36
-
37
- if(processStartsData?.success) {
38
- return processStartsData.output.data;
 
 
 
 
 
 
 
 
 
 
 
 
39
  }
 
 
 
40
  }
41
  }
42
 
 
43
  export { stablediff }
 
1
  import axios from "axios";
2
 
3
  async function stablediff(prompt) {
4
+ try {
5
+ const BASE_URL = "https://stabilityai-stable-diffusion-3-medium.hf.space/";
6
+ const session_hash = Math.random().toString(36).substring(2).slice(1);
7
+
8
+ const resp = await axios({
9
+ method: "POST",
10
+ url: BASE_URL + "queue/join?__theme=light",
11
+ data: {
12
+ data: [
13
+ prompt,
14
+ "",
15
+ 0,
16
+ true,
17
+ 1024,
18
+ 1024,
19
+ 5,
20
+ 28
21
+ ],
22
+ event_data: null,
23
+ fn_index: 1,
24
+ session_hash,
25
+ trigger_id: 4
26
+ }
 
 
 
 
 
27
  });
28
+
29
+ if(resp.data.event_id) {
30
+ const response = await axios({
31
+ method: "GET",
32
+ url: BASE_URL + "queue/data?session_hash=" + session_hash
33
+ });
34
+ const splited = response.data.split("\n");
35
+ const processStartsLine = splited.find(line => line.includes('process_completed'));
36
+ const processStartsData = JSON.parse(processStartsLine.replace('data: ', ''));
37
+
38
+ const processStartsLine2 = splited.find(line => line.includes('progress'));
39
+ const processStartsData2 = JSON.parse(processStartsLine.replace('data: ', ''));
40
+
41
+ if(processStartsData?.success) {
42
+ return processStartsData.output.data;
43
+ } else if(processStartsData?.success) {
44
+ return processStartsData2.output.data;
45
+ }
46
  }
47
+ } catch(e) {
48
+ console.log("error:" + e.message);
49
+ return e.message;
50
  }
51
  }
52
 
53
+
54
  export { stablediff }