Spaces:
Sleeping
Sleeping
Update index.mjs
Browse files
index.mjs
CHANGED
@@ -1,60 +1,64 @@
|
|
1 |
import express from "express";
|
2 |
-
import 'dotenv/config.js'
|
3 |
import { createServer } from "http";
|
4 |
import { Server } from "socket.io";
|
5 |
-
import { client
|
6 |
-
import { createRequire } from 'node:module'
|
7 |
-
|
8 |
const require = createRequire(import.meta.url);
|
9 |
global.EventSource = require('eventsource');
|
10 |
|
11 |
const app = express();
|
12 |
const httpServer = createServer(app);
|
13 |
app.use(express.static('public'));
|
14 |
-
const io = new Server(httpServer);
|
15 |
-
|
16 |
-
// 🌐 Server Initialization
|
17 |
-
httpServer.listen(7860, () => {
|
18 |
-
console.log("App running on localhost:7860");
|
19 |
-
});
|
20 |
|
21 |
-
// 🔌 Socket Connection Handler
|
22 |
io.on("connection", (socket) => {
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
socket.on("ask_api", (
|
27 |
-
console.log(
|
28 |
-
console.log("
|
29 |
-
|
30 |
});
|
|
|
31 |
});
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
const
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
}
|
46 |
|
47 |
-
//
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
}
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
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 |
const require = createRequire(import.meta.url);
|
8 |
global.EventSource = require('eventsource');
|
9 |
|
10 |
const app = express();
|
11 |
const httpServer = createServer(app);
|
12 |
app.use(express.static('public'));
|
13 |
+
const io = new Server(httpServer, { /* options */ });
|
|
|
|
|
|
|
|
|
|
|
14 |
|
|
|
15 |
io.on("connection", (socket) => {
|
16 |
+
|
17 |
+
console.log("new socket connection");
|
18 |
+
|
19 |
+
socket.on("ask_api", (client_data) => {
|
20 |
+
console.log(client_data)
|
21 |
+
console.log("trying to reach api");
|
22 |
+
asyncAPICall(client_data, socket)
|
23 |
});
|
24 |
+
|
25 |
});
|
26 |
|
27 |
+
async function test_servers(){
|
28 |
+
try{
|
29 |
+
const grapi_test = await client("https://gradio-hello-world.hf.space");
|
30 |
+
const apitest_result = await grapi_test.predict("/predict", [
|
31 |
+
"John",
|
32 |
+
]);
|
33 |
+
console.log(apitest_result);
|
34 |
+
}
|
35 |
+
catch(e){
|
36 |
+
console.log(e)
|
37 |
+
}
|
38 |
+
|
39 |
}
|
40 |
|
41 |
+
//test_servers()
|
42 |
+
|
43 |
+
async function asyncAPICall(data, socket) {
|
44 |
+
|
45 |
+
const grapi = await client("fffiloni/mndrm-call");
|
46 |
+
try{
|
47 |
+
const api_result = await grapi.predict("/infer", [
|
48 |
+
data[0], // blob in 'image' Image component
|
49 |
+
data[1], // string in 'Question' Textbox component
|
50 |
+
]);
|
51 |
+
console.log(api_result)
|
52 |
+
socket.emit("api_response", (api_result.data))
|
53 |
+
}
|
54 |
+
catch(e){
|
55 |
+
console.log(e)
|
56 |
+
socket.emit("api_error", ("ERROR ON API SIDE, SORRY..."))
|
57 |
+
}
|
58 |
+
|
59 |
}
|
60 |
+
|
61 |
+
|
62 |
+
|
63 |
+
httpServer.listen(7860);
|
64 |
+
console.log("App running on localhost:7860")
|