Spaces:
Sleeping
Sleeping
Update index.mjs
Browse files
index.mjs
CHANGED
@@ -1,64 +1,90 @@
|
|
|
|
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 |
-
|
18 |
-
|
19 |
socket.on("ask_api", (client_data) => {
|
20 |
-
console.log(
|
21 |
-
|
22 |
-
asyncAPICall(client_data, socket)
|
23 |
});
|
24 |
-
|
25 |
});
|
26 |
|
|
|
27 |
async function test_servers(){
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
console.log(e)
|
37 |
-
}
|
38 |
-
|
39 |
}
|
40 |
|
41 |
-
//
|
42 |
-
|
43 |
async function asyncAPICall(data, socket) {
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
59 |
}
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
-
|
63 |
-
httpServer.listen(7860);
|
64 |
-
console.log("App running on localhost:7860")
|
|
|
1 |
+
// Import necessary libraries
|
2 |
import express from "express";
|
3 |
import 'dotenv/config.js'
|
4 |
import { createServer } from "http";
|
5 |
import { Server } from "socket.io";
|
6 |
import { client } from "@gradio/client";
|
7 |
+
import { createRequire } from 'node:module';
|
8 |
+
import fs from 'fs';
|
9 |
+
import path from 'path';
|
10 |
+
|
11 |
const require = createRequire(import.meta.url);
|
12 |
global.EventSource = require('eventsource');
|
13 |
|
14 |
+
// Initialize the Express app and HTTP server
|
15 |
const app = express();
|
16 |
const httpServer = createServer(app);
|
17 |
+
|
18 |
+
// Serve static files from 'public' directory
|
19 |
app.use(express.static('public'));
|
|
|
20 |
|
21 |
+
// Initialize Socket.IO for real-time web socket communication
|
22 |
+
const io = new Server(httpServer, {});
|
23 |
+
|
24 |
+
// Directory for saving inference files
|
25 |
+
const outputDir = 'inference_outputs';
|
26 |
+
if (!fs.existsSync(outputDir)){
|
27 |
+
fs.mkdirSync(outputDir);
|
28 |
+
}
|
29 |
+
|
30 |
+
// Handle new socket connection
|
31 |
io.on("connection", (socket) => {
|
32 |
+
console.log("๐ New socket connection");
|
33 |
|
34 |
+
// Listen for 'ask_api' events from the client
|
|
|
35 |
socket.on("ask_api", (client_data) => {
|
36 |
+
console.log("๐ธ Received data from client");
|
37 |
+
asyncAPICall(client_data, socket);
|
|
|
38 |
});
|
|
|
39 |
});
|
40 |
|
41 |
+
// Example function to test server communication with a Gradio API
|
42 |
async function test_servers(){
|
43 |
+
try{
|
44 |
+
const grapi_test = await client("https://gradio-hello-world.hf.space");
|
45 |
+
const apitest_result = await grapi_test.predict("/predict", ["John"]);
|
46 |
+
console.log(apitest_result);
|
47 |
+
}
|
48 |
+
catch(e){
|
49 |
+
console.log("โ Error testing servers:", e);
|
50 |
+
}
|
|
|
|
|
|
|
51 |
}
|
52 |
|
53 |
+
// Function to call the Gradio API asynchronously
|
|
|
54 |
async function asyncAPICall(data, socket) {
|
55 |
+
const grapi = await client("fffiloni/mndrm-call");
|
56 |
+
try{
|
57 |
+
// Perform the API call with the provided image blob and question
|
58 |
+
const api_result = await grapi.predict("/infer", [data[0], data[1]]);
|
59 |
+
console.log("โ
API Result:", api_result);
|
60 |
+
|
61 |
+
// Emit the API response back to the client
|
62 |
+
socket.emit("api_response", api_result.data);
|
63 |
+
|
64 |
+
// Save the inference input and output
|
65 |
+
saveInference(data, api_result.data);
|
66 |
+
}
|
67 |
+
catch(e){
|
68 |
+
console.log("โ API Call Error:", e);
|
69 |
+
socket.emit("api_error", "ERROR ON API SIDE, SORRY...");
|
70 |
+
}
|
71 |
}
|
72 |
|
73 |
+
// Save the inference input (image and question) and output (API response) to files
|
74 |
+
function saveInference(data, apiResponse) {
|
75 |
+
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
76 |
+
const basePath = path.join(outputDir, timestamp);
|
77 |
+
|
78 |
+
// Save image
|
79 |
+
const imageData = data[0].split(',')[1]; // Assuming data[0] is a base64 image string
|
80 |
+
fs.writeFileSync(`${basePath}_image.png`, imageData, 'base64');
|
81 |
+
|
82 |
+
// Save question and API response in a text file
|
83 |
+
const textContent = `Question: ${data[1]}\nAPI Response: ${apiResponse}`;
|
84 |
+
fs.writeFileSync(`${basePath}_info.txt`, textContent);
|
85 |
+
|
86 |
+
console.log("๐ Inference data saved:", basePath);
|
87 |
+
}
|
88 |
|
89 |
+
// Start the HTTP server
|
90 |
+
httpServer.listen(7860, () => console.log("๐ App running on localhost:7860"));
|
|