-
Notifications
You must be signed in to change notification settings - Fork 722
Expand file tree
/
Copy pathbridge.js
More file actions
134 lines (91 loc) · 3.15 KB
/
bridge.js
File metadata and controls
134 lines (91 loc) · 3.15 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { ipcMain, dialog, app } from 'electron'
var win;
var python;
var py_buffer = "";
var is_app_closing = false;
function start_bridge() {
console.log("starting briddddd")
const fs = require('fs')
// PY_SCRIPT_DEV can be set to "./src/fake_backend.py" when developing the electron app
// to simulate the model inference, without actually executing the code
let script_path = process.env.PY_SCRIPT_DEV || "../backends/stable_diffusion_torch/txt2img.py";
if (fs.existsSync(script_path)) {
python = require('child_process').spawn('python3', [script_path]);
}
else{
const path = require('path');
let backend_path = path.join(path.dirname(__dirname), 'core' , 'diffusionbee_backend' );
python = require('child_process').spawn( backend_path );
}
python.stdin.setEncoding('utf-8');
python.stdout.on('data', function(data) {
console.log("Python response: ", data.toString('utf8'));
if(! data.toString().includes("sdbk ")){
if(win && !is_app_closing )
win.webContents.send('to_renderer', 'adlg ' + data.toString('utf8'));
}
if (win) {
py_buffer += data.toString('utf8');
let splitted = py_buffer.split("\n")
if( splitted.length > 1 ){
for (var i = 0; i < splitted.length -1 ; i++) {
if (splitted[i].length > 0)
if(win && !is_app_closing )
win.webContents.send('to_renderer', 'py2b ' + splitted[i]);
}
}
py_buffer = splitted[ splitted.length - 1 ];
} else {
console.log("window not binded yet, got from py : " + data.toString('utf8'))
}
});
python.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
if(win && !is_app_closing )
win.webContents.send('to_renderer', 'adlg ' + data.toString('utf8') );
});
python.on('close', (code) => {
// if( code != 0 )
// {
// dialog.showMessageBox("Backend quit unexpectedly")
// }
if(is_app_closing){
if (win){
app.exit(1);
}
return;
}
dialog.showMessageBox({ message: "Backend quit unexpectedly" });
if (win)
{
is_app_closing = true;
app.exit(1);
}
});
}
ipcMain.on('to_python_sync', (event, arg) => {
if (python) {
event.returnValue = "ok";
// console("sending to py from main " + arg )
python.stdin.write("b2py " + arg.toString() + "\n")
} else {
console.log("Python not binded yet!");
event.returnValue = "not_ok";
}
})
ipcMain.on('to_python_async', (event, arg) => {
if (python) {
python.stdin.write("b2py " + arg.toString() + "\n")
}
})
app.on('window-all-closed', () => {
if(python){
is_app_closing = true;
python.kill();
}
})
function bind_window_bridge(w) {
console.log("browser object binded")
win = w;
}
export { start_bridge, bind_window_bridge }