File size: 1,397 Bytes
f9f0fec
eb5e811
f9f0fec
2767dda
a227ed8
325f41a
f9f0fec
 
 
 
 
 
eb5e811
9a70daf
f9f0fec
eb5e811
a227ed8
 
eb5e811
 
325f41a
f9f0fec
 
 
a227ed8
eb5e811
 
 
 
 
 
 
 
 
 
 
 
325f41a
a227ed8
eb5e811
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import express from "express";
import 'dotenv/config.js';
import { createServer } from "http";
import { Server } from "socket.io";
import { client } from "@gradio/client";
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
global.EventSource = require('eventsource');

const app = express();
const httpServer = createServer(app);
app.use(express.static('public'));
const io = new Server(httpServer, { /* options */ });

io.on("connection", (socket) => {
    console.log("new socket connection ๐ŸŽ‰");
    
    socket.on("ask_api", (client_data) => {
        console.log("Received data from client ๐Ÿ“ธ", client_data);
        console.log("Attempting to reach API... ๐Ÿš€");   
        asyncAPICall(client_data, socket);
    });
});

async function asyncAPICall(data, socket) {
  const grapi = await client("fffiloni/mndrm-call");
  try {
      const api_result = await grapi.predict("/infer", [
          data[0], // blob in 'image' Image component
          data[1], // string in 'Question' Textbox component
      ]);
      console.log("API response received โœ…", api_result);
      socket.emit("api_response", (api_result.data));
  } catch (e) {
      console.error("API call failed โŒ", e);
      socket.emit("api_error", ("ERROR ON API SIDE, SORRY..."));
  }    
}

httpServer.listen(7860, () => console.log("App running on localhost:7860 ๐ŸŒ"));