awacke1 commited on
Commit
06a2b71
·
verified ·
1 Parent(s): 2f4d105

Update index.mjs

Browse files
Files changed (1) hide show
  1. index.mjs +38 -75
index.mjs CHANGED
@@ -1,19 +1,9 @@
1
- // At the top of your index.mjs or wherever you're planning to use fetch
2
- import fetch from 'node-fetch';
3
- if (!globalThis.fetch) {
4
- globalThis.fetch = fetch;
5
- }
6
  import express from "express";
7
- import 'dotenv/config.js';
8
  import { createServer } from "http";
9
  import { Server } from "socket.io";
10
  import { client } from "@gradio/client";
11
- import { createRequire } from 'node:module';
12
- import { writeFile, readFileSync, appendFileSync } from 'node:fs';
13
- import { join } from 'node:path';
14
- import { randomBytes } from 'node:crypto';
15
- import base64url from 'base64-url';
16
-
17
  const require = createRequire(import.meta.url);
18
  global.EventSource = require('eventsource');
19
 
@@ -22,80 +12,53 @@ const httpServer = createServer(app);
22
  app.use(express.static('public'));
23
  const io = new Server(httpServer, { /* options */ });
24
 
25
- const textHistoryPath = join(__dirname, 'public', 'history.txt');
26
-
27
  io.on("connection", (socket) => {
28
- console.log("new socket connection 🎉");
 
29
 
30
  socket.on("ask_api", (client_data) => {
31
- console.log("Received data from client 📸", client_data);
32
- console.log("Attempting to reach API... 🚀");
33
- asyncAPICall(client_data, socket);
34
  });
 
35
  });
36
 
37
- async function asyncAPICall(data, socket) {
38
- const grapi = await client("fffiloni/mndrm-call");
39
- try {
40
- const api_result = await grapi.predict("/infer", [
41
- data[0], // blob in 'image' Image component
42
- data[1], // string in 'Question' Textbox component
43
- ]);
44
- console.log("API response received ✅", api_result);
45
-
46
- // Save image and text
47
- const imageName = saveImage(api_result.data.image); // Assuming api_result.data.image is the image blob or base64 string
48
- const textName = saveText(api_result.data.text, imageName); // Assuming api_result.data.text is the text to be saved
49
-
50
- // Emit the response with download links
51
- socket.emit("api_response", {
52
- ...api_result.data,
53
- imageDownloadLink: `/download/${imageName}`,
54
- textDownloadLink: `/download/${textName}`
55
- });
56
-
57
- updateHistory(api_result.data.text, imageName);
58
-
59
- } catch (e) {
60
- console.error("API call failed ❌", e);
61
- socket.emit("api_error", ("ERROR ON API SIDE, SORRY..."));
62
- }
63
  }
64
 
65
- function saveImage(imageData) {
66
- const imageName = `image_${randomBytes(8).toString('hex')}.png`;
67
- writeFile(join(__dirname, 'public', imageName), imageData, 'base64', (err) => {
68
- if (err) throw err;
69
- console.log(`Image saved as ${imageName}`);
70
- });
71
- return imageName;
72
- }
73
 
74
- function saveText(text, imageName) {
75
- const textName = `text_${randomBytes(8).toString('hex')}.txt`;
76
- writeFile(join(__dirname, 'public', textName), `${text}\nImage File: ${imageName}\n`, (err) => {
77
- if (err) throw err;
78
- console.log(`Text saved as ${textName}`);
79
- });
80
- return textName;
81
- }
82
 
83
- function updateHistory(text, imageName) {
84
- appendFileSync(textHistoryPath, `${text}\nImage File: ${imageName}\n`);
 
 
 
 
 
 
 
 
 
 
 
 
85
  }
86
 
87
- // Serve files for download
88
- app.get('/download/:name', (req, res) => {
89
- const fileName = req.params.name;
90
- const filePath = join(__dirname, 'public', fileName);
91
- res.download(filePath);
92
- });
93
 
94
- // Serve history.txt encoded in Base64
95
- app.get('/history', (req, res) => {
96
- const history = readFileSync(textHistoryPath, 'utf8');
97
- const encodedHistory = base64url.encode(history);
98
- res.send(`<a href="data:text/plain;base64,${encodedHistory}" download="history.txt">Download History</a>`);
99
- });
100
 
101
- 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
 
 
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")