Khrisna commited on
Commit
182f6b6
·
verified ·
1 Parent(s): 5064ec0

Update lib/enchance.js

Browse files
Files changed (1) hide show
  1. lib/enchance.js +43 -80
lib/enchance.js CHANGED
@@ -1,87 +1,50 @@
1
- import WebSocket from "ws"
2
  import Jimp from "jimp"
3
  import FormData from "form-data"
4
  import axios from "axios"
5
 
6
- async function upscaler(images) {
7
- return new Promise((resolve, reject) => {
8
- const ws = new WebSocket("wss://sczhou-codeformer.hf.space/queue/join");
9
- const session_hash = Math.random().toString(36).substring(2).slice(1);
10
-
11
- ws.on("open", () => {
12
- console.log("new client upscaler")
13
- })
14
-
15
- ws.on("message", (message) => {
16
- try {
17
- const msg = JSON.parse(message)
18
- if(msg.msg == "send_hash") {
19
- ws.send(JSON.stringify({ fn_index: 0, session_hash }));
20
- } else if(msg.msg == "send_data") {
21
- ws.send(JSON.stringify({
22
- data: [
23
- images,
24
- true,
25
- true,
26
- true,
27
- 3,
28
- 0.5
29
- ],
30
- event_data: null,
31
- fn_index: 0,
32
- session_hash
33
- }));
34
- } else if(msg.msg == "estimation") {
35
- console.log("Progress: " + msg.rank);
36
- } else if(msg.msg == "process_completed") {
37
- resolve(msg.output);
38
- };
39
-
40
- } catch(e) {
41
- reject(e.message);
42
- } finally {
43
- ws.closeAllListener();
44
- ws.close();
45
- }
46
- })
47
-
48
- ws.on("error", (err) => {
49
- return err;
50
- });
51
- })
52
-
53
- }
54
-
55
  async function processImageUpscaler({ buffer, method, filenames, mimetype }) {
56
- return new Promise(async (resolve, reject) => {
57
- let Methods = ["enhance"];
58
- Methods.includes(method)? (method = method) : (method = Methods[0]);
59
-
60
- let buffer,
61
- formData = new FormData(),
62
- scheme = "https://" + "inferenceengine" + ".vyro" + ".ai/" + method;
63
-
64
- formData.append("model_version", 1);
65
-
66
- formData.append("image", Buffer.from(buffer), {
67
- filename: filenames,
68
- contentType: mimetype,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  });
 
70
 
71
- const headers = {
72
- "User-Agent": "okhttp/4.9.3",
73
- Connection: "Keep-Alive",
74
- "Accept-Encoding": "gzip",
75
- };
76
-
77
- axios.post(scheme, formData, { headers })
78
- .then(response => {
79
- resolve(response.data);
80
- })
81
- .catch(error => {
82
- reject(error);
83
- });
84
- });
85
- }
86
-
87
- export { upscaler, processImageUpscaler }
 
 
1
  import Jimp from "jimp"
2
  import FormData from "form-data"
3
  import axios from "axios"
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  async function processImageUpscaler({ buffer, method, filenames, mimetype }) {
6
+ return new Promise(async (resolve, reject) => {
7
+ let Methods = ["enhance"];
8
+ Methods.includes(method) ? (method = method) : (method = Methods[0]);
9
+
10
+ let buffer,
11
+ Form = new FormData(),
12
+ scheme = "https" + "://" + "inferenceengine" + ".vyro" + ".ai/" + method;
13
+
14
+ Form.append("model_version", 1);
15
+
16
+ Form.append("image", buffer, {
17
+ filename: filenames,
18
+ contentType: mimetype,
19
+ });
20
+
21
+ Form.submit({
22
+ url: scheme,
23
+ host: "inferenceengine" + ".vyro" + ".ai",
24
+ path: "/" + method,
25
+ protocol: "https:",
26
+ headers: {
27
+ "User-Agent": "okhttp/4.9.3",
28
+ Connection: "Keep-Alive",
29
+ "Accept-Encoding": "gzip",
30
+ },
31
+ },
32
+ function(err, res) {
33
+ if (err) reject(err);
34
+ let data = [];
35
+ res
36
+ .on("data", function(chunk) {
37
+ data.push(chunk);
38
+ })
39
+ .on("end", () => {
40
+ resolve(Buffer.concat(data));
41
+ });
42
+ res.on("error", (e) => {
43
+ reject(e);
44
+ });
45
+ }
46
+ );
47
  });
48
+ };
49
 
50
+ export { processImageUpscaler }