Spaces:
Running
Running
Update index.js
Browse files
index.js
CHANGED
@@ -2,16 +2,6 @@ import express from "express";
|
|
2 |
import os from "os";
|
3 |
import morgan from "morgan";
|
4 |
import bytes from "bytes";
|
5 |
-
import axios from "axios";
|
6 |
-
import { FormData, Blob } from "formdata-node";
|
7 |
-
import { fileTypeFromBuffer } from "file-type";
|
8 |
-
import { Client } from "@gradio/client";
|
9 |
-
import { stablediff } from "./lib/diffusion.js";
|
10 |
-
import { askOpenGPT4o } from "./lib/chatgpt.js";
|
11 |
-
import { processImageUpscaler } from "./lib/enchance.js";
|
12 |
-
import { toAnime } from "./lib/toanime.js";
|
13 |
-
import { convertWebpToPng } from "./lib/converter.js";
|
14 |
-
import { Uploader } from "./lib/uploader.js";
|
15 |
|
16 |
import APIRouter from "./router/api.js";
|
17 |
|
@@ -26,8 +16,6 @@ app.use((req, res, next) => {
|
|
26 |
|
27 |
app.use('/api', APIRouter);
|
28 |
|
29 |
-
const apikey = "@SadTeam77";
|
30 |
-
|
31 |
|
32 |
app.all('/', (req, res) => {
|
33 |
const status = {}
|
@@ -48,192 +36,6 @@ app.all('/', (req, res) => {
|
|
48 |
})
|
49 |
})
|
50 |
|
51 |
-
app.post('/api/img2img', async (req, res) => {
|
52 |
-
try {
|
53 |
-
console.log(req.body)
|
54 |
-
const { images, prompt, status } = req.body
|
55 |
-
if (!images) return res.json({ success: false, message: 'Required an images!' })
|
56 |
-
if (!prompt) return res.json({ succese: false, message: 'Require an Promot text Image!'})
|
57 |
-
if (!status) return res.json({ success: false, message: 'Required an status text!' })
|
58 |
-
|
59 |
-
if(status !== apikey) return res.json({ success: false, message: 'Invalid status!' })
|
60 |
-
if (/^(https?|http):\/\//i.test(images)) {
|
61 |
-
const data_img = await axios.request({
|
62 |
-
method: "GET",
|
63 |
-
url: images,
|
64 |
-
responseType: "arraybuffer"
|
65 |
-
})
|
66 |
-
const response = await processImage2Img(data_img.data, prompt)
|
67 |
-
//const type_img = await fileTypeFromBuffer(response)
|
68 |
-
//res.setHeader('Content-Type', type_img.mime)
|
69 |
-
res.json(response)
|
70 |
-
} else if (images && typeof images == 'string' && isBase64(images)) {
|
71 |
-
const response = await processImage2Img(Buffer.from(images, "base64"), prompt)
|
72 |
-
//const type_img = await fileTypeFromBuffer(response)
|
73 |
-
//res.setHeader('Content-Type', type_img.mime)
|
74 |
-
res.json(response)
|
75 |
-
} else {
|
76 |
-
res.json({
|
77 |
-
success: false, message: 'No url or base64 detected!!'
|
78 |
-
})
|
79 |
-
}
|
80 |
-
} catch (e) {
|
81 |
-
console.log(e)
|
82 |
-
e = String(e)
|
83 |
-
res.json({ error: true, message: e === '[object Object]' ? 'Internal Server Error' : e })
|
84 |
-
}
|
85 |
-
})
|
86 |
-
|
87 |
-
app.post('/api/upscaler', async (req, res) => {
|
88 |
-
try {
|
89 |
-
console.log(req.body)
|
90 |
-
const { images, filenames, mimetype, status } = req.body
|
91 |
-
if (!images) return res.json({ success: false, message: 'Required an images!' })
|
92 |
-
if (!filenames) return res.json({ success: false, message: 'Required an filenames!' })
|
93 |
-
if (!mimetype) return res.json({ success: false, message: 'Required an mimetype!' })
|
94 |
-
if (!status) return res.json({ success: false, message: 'Required an status text!' })
|
95 |
-
|
96 |
-
if(status !== apikey) return res.json({ success: false, message: 'Invalid status!' })
|
97 |
-
if (/^(https?|http):\/\//i.test(images)) {
|
98 |
-
const data_img = await axios.request({
|
99 |
-
method: "GET",
|
100 |
-
url: images,
|
101 |
-
responseType: "arraybuffer"
|
102 |
-
})
|
103 |
-
const response = await processImageUpscaler({buffer: data_img.data, method: "enchance", filenames, mimetype})
|
104 |
-
//const type_img = await fileTypeFromBuffer(response)
|
105 |
-
//res.setHeader('Content-Type', type_img.mime)
|
106 |
-
res.json(response)
|
107 |
-
} else if (images && typeof images == 'string' && isBase64(images)) {
|
108 |
-
const response = await processImageUpscaler({buffer: Buffer.from(images, "base64"), method: "enchance", filenames, mimetype})
|
109 |
-
//const type_img = await fileTypeFromBuffer(response)
|
110 |
-
//res.setHeader('Content-Type', type_img.mime)
|
111 |
-
res.json(response)
|
112 |
-
} else {
|
113 |
-
res.json({
|
114 |
-
success: false, message: 'No url or base64 detected!!'
|
115 |
-
})
|
116 |
-
}
|
117 |
-
} catch (e) {
|
118 |
-
console.log(e)
|
119 |
-
e = String(e)
|
120 |
-
res.json({ error: true, message: e === '[object Object]' ? 'Internal Server Error' : e })
|
121 |
-
}
|
122 |
-
})
|
123 |
-
|
124 |
-
app.post('/api/toanime', async (req, res) => {
|
125 |
-
try {
|
126 |
-
console.log(req.body)
|
127 |
-
const { images, filenames, mimetype, status } = req.body
|
128 |
-
if (!images) return res.json({ success: false, message: 'Required an images!' })
|
129 |
-
if (!status) return res.json({ success: false, message: 'Required an status text!' })
|
130 |
-
|
131 |
-
if(status !== apikey) return res.json({ success: false, message: 'Invalid status!' })
|
132 |
-
if (/^(https?|http):\/\//i.test(images)) {
|
133 |
-
const data_img = await axios.request({
|
134 |
-
method: "GET",
|
135 |
-
url: images,
|
136 |
-
responseType: "arraybuffer"
|
137 |
-
})
|
138 |
-
const response = await toAnime({ imgBuffer: data_img.data });
|
139 |
-
const converted = await convertWebpToPng(response)
|
140 |
-
//const type_img = await fileTypeFromBuffer(converting)
|
141 |
-
//res.setHeader('Content-Type', type_img.mime)
|
142 |
-
res.json({
|
143 |
-
status: true,
|
144 |
-
result: converteed
|
145 |
-
})
|
146 |
-
} else if (images && typeof images == 'string' && isBase64(images)) {
|
147 |
-
const response = await await toAnime({ imgBuffer: Buffer.from(images, "base64") });
|
148 |
-
const converted = await convertWebpToPng(response);
|
149 |
-
//const type_img = await fileTypeFromBuffer(converting)
|
150 |
-
//res.setHeader('Content-Type', type_img.mime)
|
151 |
-
res.json({
|
152 |
-
status: true,
|
153 |
-
result: converted
|
154 |
-
})
|
155 |
-
} else {
|
156 |
-
res.json({
|
157 |
-
success: false, message: 'No url or base64 detected!!'
|
158 |
-
})
|
159 |
-
}
|
160 |
-
} catch (e) {
|
161 |
-
console.log(e)
|
162 |
-
e = String(e)
|
163 |
-
res.json({ error: true, message: e === '[object Object]' ? 'Internal Server Error' : e })
|
164 |
-
}
|
165 |
-
});
|
166 |
-
|
167 |
-
app.post('/api/openai/gpt4', async (req, res) => {
|
168 |
-
try {
|
169 |
-
console.log(req.body)
|
170 |
-
const { prompt, images, status } = req.body
|
171 |
-
if (!prompt) return res.json({ succese: false, message: 'Require an Promot text!'})
|
172 |
-
if (!status) return res.json({ success: false, message: 'Required an status text!' })
|
173 |
-
|
174 |
-
if(status !== apikey) return res.json({ success: false, message: 'Invalid status!' })
|
175 |
-
if(images) {
|
176 |
-
if (/^(https?|http):\/\//i.test(images)) {
|
177 |
-
const data_img = await axios.request({
|
178 |
-
method: "GET",
|
179 |
-
url: images,
|
180 |
-
responseType: "arraybuffer"
|
181 |
-
})
|
182 |
-
const response = await askOpenGPT4o(prompt, data_img.data)
|
183 |
-
//const type_img = await fileTypeFromBuffer(response)
|
184 |
-
//res.setHeader('Content-Type', type_img.mime)
|
185 |
-
res.json({
|
186 |
-
status: true,
|
187 |
-
result: response
|
188 |
-
})
|
189 |
-
} else if (images && typeof images == 'string' && isBase64(images)) {
|
190 |
-
const response = await askOpenGPT4o(prompt, Buffer.from(images, "base64"))
|
191 |
-
//const type_img = await fileTypeFromBuffer(response)
|
192 |
-
//res.setHeader('Content-Type', type_img.mime)
|
193 |
-
res.json({
|
194 |
-
status: true,
|
195 |
-
result: response
|
196 |
-
})
|
197 |
-
} else {
|
198 |
-
res.json({
|
199 |
-
success: false, message: 'No url or base64 detected!!'
|
200 |
-
})
|
201 |
-
}
|
202 |
-
} else if(!images) {
|
203 |
-
const response = await askOpenGPT4o(prompt)
|
204 |
-
res.json({
|
205 |
-
status: true,
|
206 |
-
result: response
|
207 |
-
});
|
208 |
-
}
|
209 |
-
} catch (e) {
|
210 |
-
console.log(e)
|
211 |
-
e = String(e)
|
212 |
-
res.json({ error: true, message: e === '[object Object]' ? 'Internal Server Error' : e })
|
213 |
-
}
|
214 |
-
})
|
215 |
-
|
216 |
-
app.post('/api/stabeldiff', async (req, res) => {
|
217 |
-
try {
|
218 |
-
console.log(req.body)
|
219 |
-
const { prompt, status } = req.body
|
220 |
-
if (!prompt) return res.json({ succese: false, message: 'Require an Promot text!'})
|
221 |
-
if (!status) return res.json({ success: false, message: 'Required an status text!' })
|
222 |
-
|
223 |
-
if(status !== apikey) return res.json({ success: false, message: 'Invalid status!' })
|
224 |
-
const response = await stablediff(prompt);
|
225 |
-
|
226 |
-
res.json({
|
227 |
-
status: true,
|
228 |
-
result: response
|
229 |
-
});
|
230 |
-
} catch (e) {
|
231 |
-
console.log(e)
|
232 |
-
e = String(e)
|
233 |
-
res.json({ error: true, message: e === '[object Object]' ? 'Internal Server Error' : e })
|
234 |
-
}
|
235 |
-
})
|
236 |
-
|
237 |
const PORT = process.env.PORT || 7860
|
238 |
app.listen(PORT, () => {
|
239 |
console.log('App running on port', PORT)
|
@@ -248,26 +50,4 @@ function isBase64(str) {
|
|
248 |
} catch {
|
249 |
return false
|
250 |
}
|
251 |
-
}
|
252 |
-
|
253 |
-
async function processImage2Img(imgBuffer, prompt) {
|
254 |
-
return new Promise(async (resolve, reject) => {
|
255 |
-
try {
|
256 |
-
const imageArray = Buffer.from(imgBuffer);
|
257 |
-
|
258 |
-
process.env.GRADIO_CLIENT_DEBUG = 'true';
|
259 |
-
|
260 |
-
const app = await Client.connect("Manjushri/SDXL-Turbo-Img2Img-CPU");
|
261 |
-
const result = await app.predict("/predict", [
|
262 |
-
imageArray, // binary input for the image
|
263 |
-
prompt, // string input for the prompt
|
264 |
-
1, // number input for the number of iterations
|
265 |
-
540388010706833800, // number input for the seed
|
266 |
-
0.3, // number input for the strength
|
267 |
-
]);
|
268 |
-
resolve(result.data);
|
269 |
-
} catch (e) {
|
270 |
-
reject(e.message);
|
271 |
-
}
|
272 |
-
});
|
273 |
}
|
|
|
2 |
import os from "os";
|
3 |
import morgan from "morgan";
|
4 |
import bytes from "bytes";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
import APIRouter from "./router/api.js";
|
7 |
|
|
|
16 |
|
17 |
app.use('/api', APIRouter);
|
18 |
|
|
|
|
|
19 |
|
20 |
app.all('/', (req, res) => {
|
21 |
const status = {}
|
|
|
36 |
})
|
37 |
})
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
const PORT = process.env.PORT || 7860
|
40 |
app.listen(PORT, () => {
|
41 |
console.log('App running on port', PORT)
|
|
|
50 |
} catch {
|
51 |
return false
|
52 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
}
|