awacke1 commited on
Commit
f3c1998
·
verified ·
1 Parent(s): efe93a0

Update index.mjs

Browse files
Files changed (1) hide show
  1. index.mjs +41 -37
index.mjs CHANGED
@@ -4,6 +4,8 @@ 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
 
@@ -13,7 +15,6 @@ 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) => {
@@ -21,49 +22,52 @@ io.on("connection", (socket) => {
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")
65
-
66
-
67
-
68
-
69
-
 
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
 
 
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) => {
 
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")