awacke1 commited on
Commit
325f41a
ยท
verified ยท
1 Parent(s): d31d9d1

Update index.mjs

Browse files
Files changed (1) hide show
  1. index.mjs +65 -39
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
- 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")
 
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"));