Spaces:
Sleeping
Sleeping
Update index.mjs
Browse files
index.mjs
CHANGED
@@ -1,90 +1,40 @@
|
|
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("
|
33 |
|
34 |
-
// Listen for 'ask_api' events from the client
|
35 |
socket.on("ask_api", (client_data) => {
|
36 |
-
console.log("
|
|
|
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 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
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 |
-
|
90 |
-
httpServer.listen(7860, () => console.log("π App running on localhost:7860"));
|
|
|
|
|
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 |
+
console.log("new socket connection π");
|
17 |
|
|
|
18 |
socket.on("ask_api", (client_data) => {
|
19 |
+
console.log("Received data from client πΈ", client_data);
|
20 |
+
console.log("Attempting to reach API... π");
|
21 |
asyncAPICall(client_data, socket);
|
22 |
});
|
23 |
});
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
async function asyncAPICall(data, socket) {
|
26 |
+
const grapi = await client("fffiloni/mndrm-call");
|
27 |
+
try {
|
28 |
+
const api_result = await grapi.predict("/infer", [
|
29 |
+
data[0], // blob in 'image' Image component
|
30 |
+
data[1], // string in 'Question' Textbox component
|
31 |
+
]);
|
32 |
+
console.log("API response received β
", api_result);
|
33 |
+
socket.emit("api_response", (api_result.data));
|
34 |
+
} catch (e) {
|
35 |
+
console.error("API call failed β", e);
|
36 |
+
socket.emit("api_error", ("ERROR ON API SIDE, SORRY..."));
|
37 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
}
|
39 |
|
40 |
+
httpServer.listen(7860, () => console.log("App running on localhost:7860 π"));
|
|