Spaces:
Sleeping
Sleeping
Update index.mjs
Browse files
index.mjs
CHANGED
@@ -1,73 +1,63 @@
|
|
|
|
1 |
import express from "express";
|
2 |
import 'dotenv/config.js'
|
3 |
import { createServer } from "http";
|
4 |
-
import { Server } from "socket.io";
|
5 |
-
import { client } from "@gradio/client";
|
6 |
import { createRequire } from 'node:module'
|
7 |
-
|
8 |
-
|
9 |
const require = createRequire(import.meta.url);
|
10 |
global.EventSource = require('eventsource');
|
11 |
|
|
|
12 |
const app = express();
|
13 |
const httpServer = createServer(app);
|
14 |
app.use(express.static('public'));
|
15 |
-
const io = new Server(httpServer, { /* options */ });
|
16 |
|
|
|
|
|
|
|
|
|
17 |
io.on("connection", (socket) => {
|
18 |
-
console.log("new socket connection");
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
24 |
});
|
25 |
});
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
52 |
}
|
53 |
|
54 |
-
//
|
55 |
-
function saveImage(imageData, filename) {
|
56 |
-
const buffer = Buffer.from(imageData.split(",")[1], 'base64');
|
57 |
-
fs.writeFileSync(path.join(__dirname, 'public', filename), buffer);
|
58 |
-
}
|
59 |
-
|
60 |
-
// Express route to download the image
|
61 |
-
app.get('/download/:filename', (req, res) => {
|
62 |
-
const filename = req.params.filename;
|
63 |
-
const filepath = path.join(__dirname, 'public', filename);
|
64 |
-
res.download(filepath, filename, (err) => {
|
65 |
-
if (err) {
|
66 |
-
console.error(err);
|
67 |
-
res.status(500).send("Error occurred while downloading the file.");
|
68 |
-
}
|
69 |
-
});
|
70 |
-
});
|
71 |
-
|
72 |
httpServer.listen(7860);
|
73 |
-
console.log("App
|
|
|
1 |
+
// π Importing Necessary Modules and Libraries π
|
2 |
import express from "express";
|
3 |
import 'dotenv/config.js'
|
4 |
import { createServer } from "http";
|
5 |
+
import { Server as SocketIOServer } from "socket.io";
|
6 |
+
import { client as GradioClient } from "@gradio/client";
|
7 |
import { createRequire } from 'node:module'
|
8 |
+
|
9 |
+
// π Global Configurations π
|
10 |
const require = createRequire(import.meta.url);
|
11 |
global.EventSource = require('eventsource');
|
12 |
|
13 |
+
// π± Express App Setup π±
|
14 |
const app = express();
|
15 |
const httpServer = createServer(app);
|
16 |
app.use(express.static('public'));
|
|
|
17 |
|
18 |
+
// π Socket.io Setup π
|
19 |
+
const io = new SocketIOServer(httpServer, { /* options */ });
|
20 |
+
|
21 |
+
// π°οΈ Socket.io Connection Handler π°οΈ
|
22 |
io.on("connection", (socket) => {
|
|
|
23 |
|
24 |
+
console.log("π New Socket Connection Established");
|
25 |
+
|
26 |
+
socket.on("ask_api", (clientData) => {
|
27 |
+
console.log(clientData)
|
28 |
+
console.log("π Trying to Reach API");
|
29 |
+
callGradioAPIAsync(clientData, socket)
|
30 |
});
|
31 |
});
|
32 |
|
33 |
+
// π€ Gradio API Testing Function (Uncomment to test) π€
|
34 |
+
// async function testGradioServers(){
|
35 |
+
// try {
|
36 |
+
// const gradioTestClient = await GradioClient("https://gradio-hello-world.hf.space");
|
37 |
+
// const apiTestResult = await gradioTestClient.predict("/predict", ["John"]);
|
38 |
+
// console.log(apiTestResult);
|
39 |
+
// } catch (error) {
|
40 |
+
// console.log(error);
|
41 |
+
// }
|
42 |
+
// }
|
43 |
+
// testGradioServers();
|
44 |
+
|
45 |
+
// π Async Function to Call Gradio API π
|
46 |
+
async function callGradioAPIAsync(clientData, socket) {
|
47 |
+
const gradioClient = await GradioClient("fffiloni/mndrm-call");
|
48 |
+
try {
|
49 |
+
const apiResult = await gradioClient.predict("/infer", [
|
50 |
+
clientData[0], // blob in 'image' Image component
|
51 |
+
clientData[1], // string in 'Question' Textbox component
|
52 |
+
]);
|
53 |
+
console.log(apiResult)
|
54 |
+
socket.emit("api_response", (apiResult.data))
|
55 |
+
} catch (error) {
|
56 |
+
console.log(error)
|
57 |
+
socket.emit("api_error", ("ERROR ON API SIDE, SORRY..."))
|
58 |
+
}
|
59 |
}
|
60 |
|
61 |
+
// π Server Startup π
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
httpServer.listen(7860);
|
63 |
+
console.log("π App Running on http://localhost:7860");
|