awacke1 commited on
Commit
9a70daf
Β·
verified Β·
1 Parent(s): f3c1998

Update index.mjs

Browse files
Files changed (1) hide show
  1. index.mjs +44 -54
index.mjs CHANGED
@@ -1,73 +1,63 @@
 
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
- import fs from 'fs';
8
- import path from 'path';
9
  const require = createRequire(import.meta.url);
10
  global.EventSource = require('eventsource');
11
 
 
12
  const app = express();
13
  const httpServer = createServer(app);
14
  app.use(express.static('public'));
15
- const io = new Server(httpServer, { /* options */ });
16
 
 
 
 
 
17
  io.on("connection", (socket) => {
18
- console.log("new socket connection");
19
 
20
- socket.on("ask_api", (client_data) => {
21
- console.log(client_data)
22
- console.log("trying to reach api");
23
- asyncAPICall(client_data, socket)
 
 
24
  });
25
  });
26
 
27
- async function asyncAPICall(data, socket) {
28
- const grapi = await client("fffiloni/mndrm-call");
29
- try {
30
- const api_result = await grapi.predict("/infer", [
31
- data[0], // blob in 'image' Image component
32
- data[1], // string in 'Question' Textbox component
33
- ]);
34
- console.log(api_result)
35
-
36
- // Save the image and provide a download link
37
- if(api_result && api_result.data){
38
- const filename = createFileName(api_result.data.text);
39
- saveImage(data[0], filename);
40
- socket.emit("api_response", { ...api_result.data, downloadLink: `/download/${filename}` });
41
- }
42
- } catch(e) {
43
- console.log(e)
44
- socket.emit("api_error", ("ERROR ON API SIDE, SORRY..."))
45
- }
46
- }
47
-
48
- // Function to create a filename from the caption and current datetime
49
- function createFileName(caption) {
50
- const datetime = new Date().toISOString().replace(/:/g, '-');
51
- return `${caption}-${datetime}.png`;
 
52
  }
53
 
54
- // Function to save image data to a file
55
- function saveImage(imageData, filename) {
56
- const buffer = Buffer.from(imageData.split(",")[1], 'base64');
57
- fs.writeFileSync(path.join(__dirname, 'public', filename), buffer);
58
- }
59
-
60
- // Express route to download the image
61
- app.get('/download/:filename', (req, res) => {
62
- const filename = req.params.filename;
63
- const filepath = path.join(__dirname, 'public', filename);
64
- res.download(filepath, filename, (err) => {
65
- if (err) {
66
- console.error(err);
67
- res.status(500).send("Error occurred while downloading the file.");
68
- }
69
- });
70
- });
71
-
72
  httpServer.listen(7860);
73
- console.log("App running on localhost:7860")
 
1
+ // 🌍 Importing Necessary Modules and Libraries 🌍
2
  import express from "express";
3
  import 'dotenv/config.js'
4
  import { createServer } from "http";
5
+ import { Server as SocketIOServer } from "socket.io";
6
+ import { client as GradioClient } from "@gradio/client";
7
  import { createRequire } from 'node:module'
8
+
9
+ // πŸš€ Global Configurations πŸš€
10
  const require = createRequire(import.meta.url);
11
  global.EventSource = require('eventsource');
12
 
13
+ // πŸ“± Express App Setup πŸ“±
14
  const app = express();
15
  const httpServer = createServer(app);
16
  app.use(express.static('public'));
 
17
 
18
+ // πŸ”Œ Socket.io Setup πŸ”Œ
19
+ const io = new SocketIOServer(httpServer, { /* options */ });
20
+
21
+ // πŸ›°οΈ Socket.io Connection Handler πŸ›°οΈ
22
  io.on("connection", (socket) => {
 
23
 
24
+ console.log("🌐 New Socket Connection Established");
25
+
26
+ socket.on("ask_api", (clientData) => {
27
+ console.log(clientData)
28
+ console.log("πŸ” Trying to Reach API");
29
+ callGradioAPIAsync(clientData, socket)
30
  });
31
  });
32
 
33
+ // πŸ€– Gradio API Testing Function (Uncomment to test) πŸ€–
34
+ // async function testGradioServers(){
35
+ // try {
36
+ // const gradioTestClient = await GradioClient("https://gradio-hello-world.hf.space");
37
+ // const apiTestResult = await gradioTestClient.predict("/predict", ["John"]);
38
+ // console.log(apiTestResult);
39
+ // } catch (error) {
40
+ // console.log(error);
41
+ // }
42
+ // }
43
+ // testGradioServers();
44
+
45
+ // 🌟 Async Function to Call Gradio API 🌟
46
+ async function callGradioAPIAsync(clientData, socket) {
47
+ const gradioClient = await GradioClient("fffiloni/mndrm-call");
48
+ try {
49
+ const apiResult = await gradioClient.predict("/infer", [
50
+ clientData[0], // blob in 'image' Image component
51
+ clientData[1], // string in 'Question' Textbox component
52
+ ]);
53
+ console.log(apiResult)
54
+ socket.emit("api_response", (apiResult.data))
55
+ } catch (error) {
56
+ console.log(error)
57
+ socket.emit("api_error", ("ERROR ON API SIDE, SORRY..."))
58
+ }
59
  }
60
 
61
+ // πŸƒ Server Startup πŸƒ
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  httpServer.listen(7860);
63
+ console.log("πŸš€ App Running on http://localhost:7860");