awacke1 commited on
Commit
59f8f57
Β·
verified Β·
1 Parent(s): b3b53dd

Update index.mjs

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