Spaces:
Sleeping
Sleeping
Update index.js
Browse files
index.js
CHANGED
@@ -2,6 +2,9 @@ import express from "express";
|
|
2 |
import os from "os";
|
3 |
import morgan from "morgan";
|
4 |
import bytes from "bytes";
|
|
|
|
|
|
|
5 |
|
6 |
|
7 |
const app = express();
|
@@ -13,6 +16,8 @@ app.use((req, res, next) => {
|
|
13 |
next()
|
14 |
})
|
15 |
|
|
|
|
|
16 |
app.all('/', (req, res) => {
|
17 |
const status = {}
|
18 |
const used = process.memoryUsage()
|
@@ -32,6 +37,42 @@ app.all('/', (req, res) => {
|
|
32 |
})
|
33 |
})
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
const PORT = process.env.PORT || 7860
|
36 |
app.listen(PORT, () => {
|
37 |
console.log('App running on port', PORT)
|
@@ -46,4 +87,28 @@ function isBase64(str) {
|
|
46 |
} catch {
|
47 |
return false
|
48 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
}
|
|
|
2 |
import os from "os";
|
3 |
import morgan from "morgan";
|
4 |
import bytes from "bytes";
|
5 |
+
import FormData from "formdata-node";
|
6 |
+
import Blob from "formdata-node/blob";
|
7 |
+
import fileType from "file-type";
|
8 |
|
9 |
|
10 |
const app = express();
|
|
|
16 |
next()
|
17 |
})
|
18 |
|
19 |
+
const apikey = "@SadTeam77"
|
20 |
+
|
21 |
app.all('/', (req, res) => {
|
22 |
const status = {}
|
23 |
const used = process.memoryUsage()
|
|
|
37 |
})
|
38 |
})
|
39 |
|
40 |
+
app.post('/api/img2img', async (req, res) => {
|
41 |
+
try {
|
42 |
+
console.log(req.body)
|
43 |
+
const { images, prompt, status } = req.body
|
44 |
+
if (!images) return res.json({ success: false, message: 'Required an images!' })
|
45 |
+
if (!promot) return res.json({ succese: false, message: 'Require an Promot text Image!'})
|
46 |
+
if (!status) return res.json({ success: false, message: 'Required an status text!' })
|
47 |
+
|
48 |
+
if(status !== apikey) return res.json({ success: false, message: 'Invalid status!' })
|
49 |
+
if (/^(https?|http):\/\//i.test(images)) {
|
50 |
+
const data_img = await axios.request({
|
51 |
+
method: "GET",
|
52 |
+
url: images,
|
53 |
+
responseType: "arraybuffer"
|
54 |
+
})
|
55 |
+
const response = await processImage2Img(data_img.data, prompt)
|
56 |
+
const type_img = await fileType.fromBuffer(response)
|
57 |
+
res.setHeader('Content-Type', type_img.mime)
|
58 |
+
res.send(response)
|
59 |
+
} else if (images && typeof images == 'string' && isBase64(images)) {
|
60 |
+
const response = await processImage2Img(Buffer.from(images, "base64"), prompt)
|
61 |
+
const type_img = await fileType.fromBuffer(response)
|
62 |
+
res.setHeader('Content-Type', type_img.mime)
|
63 |
+
res.send(response)
|
64 |
+
} else {
|
65 |
+
res.json({
|
66 |
+
success: false, message: 'No url or base64 detected!!'
|
67 |
+
})
|
68 |
+
}
|
69 |
+
} catch (e) {
|
70 |
+
console.log(e)
|
71 |
+
e = String(e)
|
72 |
+
res.json({ error: true, message: e === '[object Object]' ? 'Internal Server Error' : e })
|
73 |
+
}
|
74 |
+
})
|
75 |
+
|
76 |
const PORT = process.env.PORT || 7860
|
77 |
app.listen(PORT, () => {
|
78 |
console.log('App running on port', PORT)
|
|
|
87 |
} catch {
|
88 |
return false
|
89 |
}
|
90 |
+
}
|
91 |
+
|
92 |
+
async function processImage2Img(imgBuffer, prompt) {
|
93 |
+
return new Promise(async (resolve, reject) => {
|
94 |
+
try {
|
95 |
+
const type = fileType(imgBuffer);
|
96 |
+
const convertingBlob = new Blob([imgBuffer], { type: type.mime });
|
97 |
+
|
98 |
+
const form = new FormData();
|
99 |
+
form.append('image', convertingBlob, "image" + type.ext);
|
100 |
+
|
101 |
+
const app = await Client.connect("Manjushri/SDXL-Turbo-Img2Img-CPU");
|
102 |
+
const result = await app.predict("/predict", [
|
103 |
+
form, // blob in 'Raw Image.' Image component
|
104 |
+
prompt, // string in 'Prompt Input Text. 77 Token (Keyword or Symbol) Maximum' Textbox component
|
105 |
+
1, // number (numeric value between 1 and 5) in 'Number of Iterations' Slider component
|
106 |
+
987654321987654321, // number (numeric value between 0 and 987654321987654321) in 'Seed' Slider component
|
107 |
+
0.1, // number (numeric value between 0.1 and 1) in 'Strength' Slider component
|
108 |
+
]);
|
109 |
+
resolve(result.data);
|
110 |
+
} catch(e) {
|
111 |
+
reject(e.message);
|
112 |
+
}
|
113 |
+
});
|
114 |
}
|