-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
174 lines (145 loc) · 6.04 KB
/
index.js
File metadata and controls
174 lines (145 loc) · 6.04 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const express = require('express'),
// For joining the paths properly on different OSes
path = require("path"),
// For reading the user_info file
fs = require("fs"),
app = express(),
port = 8080;
const multer = require('multer')
const upload = multer()
// Get user info by reading file
const user_info = JSON.parse(fs.readFileSync(path.join(__dirname, "user_info.json")));
let { name, email, provider, img_url, eval_state, eval_percent, eval_todo, eval_completed, eval_due } = user_info;
let existing_data = JSON.parse(fs.readFileSync(path.join(__dirname, "test_data.json")));
const change_user_info = (stuff) => {
fs.writeFileSync(
path.join(__dirname, "user_info.json"),
JSON.stringify({ name, email, provider, img_url, eval_state, eval_percent, eval_todo, eval_completed, eval_due, ...stuff }, null, 4)
);
}
// This lets us put in user info super easily
app.set("view engine", "ejs");
app.use(express.json());
const send_dashboard = (res) => {
res.render(
path.join(__dirname, "public", "dashboard", "dashboard"),
{ name, email, provider, img_url, eval_completed }
);
}
// Main page routes to dashboard
app.get('/', (_req, res) => {
send_dashboard(res);
});
// Return a random BBC article
app.get("/random_text", (_req, res) => {
// Read CSV file
fs.readFile(path.join(__dirname, "monologues.txt"), (err, data) => {
if(err) res.sendStatus(404);
else {
// Parse the text + get a random story
let lines = data.toString().split("\r\n\r\n\r\n");
res.send(lines[Math.floor(Math.random() * lines.length)]);
}
})
})
// Also /dashboard routes to dashboard too
app.get("/dashboard", (_req, res) => {
// Parse information from user info JSON
send_dashboard(res);
});
// /learn routes to the learning page!
app.get("/learn", (_req, res) => {
res.sendFile(path.join(__dirname, "public", "learn", "learn.html"));
});
// /eval routes to the eval page
app.get("/eval", (_req, res) => {
res.render(
path.join(__dirname, "public", "eval", "eval"),
{ eval_state, eval_percent, eval_todo, eval_completed, eval_due }
);
});
app.get("/eval/dry_eyes", (_req, res) => {
// Parse information from user info JSON
res.sendFile(path.join(__dirname, "public", "eval", "dry_eyes", "dry_eyes.html"));
});
app.get("/eval/snellan_chart", (_req, res) => {
// Parse information from user info JSON
res.sendFile(path.join(__dirname, "public", "eval", "snellan_chart", "snellan_chart.html"));
});
app.get('/save/snellan_chart', (req, res) => {
let { vision } = req.query;
// Send this to the backend + info
existing_data = { ...existing_data, vision };
fs.writeFileSync(path.join(__dirname, "test_data.json"), JSON.stringify(existing_data, null, 4));
console.log("Saving Snellan data...");
eval_todo = eval_todo.filter(e => e.id != "snellan_chart");
var options = { year: 'numeric', month: 'numeric', day: 'numeric', hour: "2-digit", minute: "2-digit"};
var today = new Date();
eval_completed.push({id: "snellan_chart", completed_on: today.toLocaleDateString("en-US", options), name: "Snellan Chart Test", vision });
// Now update the percent accordingly
eval_percent = Math.round(eval_completed.length / (eval_completed.length + eval_todo.length) * 100);
if(eval_percent == 100) eval_state = "done";
else eval_state = "todo";
change_user_info({ eval_state, eval_percent, eval_todo, eval_completed });
console.log("Vision Level: ", vision);
res.send("Finished request");
});
// Listen for saving info from the backend + place into test_data.json
app.get('/save/dry_eyes', (req, res) => {
// Parse info from JSON
let { num_blinks, bpm, mins, score } = req.query;
num_blinks = parseFloat(num_blinks);
bpm = parseFloat(bpm);
score = parseFloat(score);
mins = parseFloat(mins);
console.log(`Estimated score:\n\tBPM: ${bpm}\n\tScore: ${score}`);
// Now take test_data.json.
existing_data = { ...existing_data, num_blinks, bpm, mins };
fs.writeFileSync(path.join(__dirname, "test_data.json"), JSON.stringify(existing_data, null, 4));
// This test has been completed, so now let's alter user_info to update that
// eval_state, eval_percent, eval_todo, eval_completed
// eval_todo - take out this test + complete
eval_todo = eval_todo.filter(e => e.id != "dry_eyes");
var options = { year: 'numeric', month: 'numeric', day: 'numeric', hour: "2-digit", minute: "2-digit"};
var today = new Date();
eval_completed.push({id: "dry_eyes", completed_on: today.toLocaleDateString("en-US", options), name: "Dry Eyes Test", score });
// Now update the percent accordingly
eval_percent = Math.round(eval_completed.length / (eval_completed.length + eval_todo.length) * 100);
if(eval_percent == 100) eval_state = "done";
else eval_state = "todo";
change_user_info({ eval_state, eval_percent, eval_todo, eval_completed });
res.send("Completed request.");
})
//open-ai eval routes
const { OpenAIApi } = require("openai");
const model = "whisper-1";
//when we get a request for audio, we add a transcription
app.get('/audio',(req,res) =>{
const openAi = new OpenAIApi(new Configuration({ apiKey:"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" }));
this.openai
.audio
.speech.create({
file: './public/eval/snellan_chart/recordings/raw/recording.ogg',
model: model,
response_format: "json",
})
.then((response) => console.log(response.data)); //need to modify so it is saved in /recordings/transcript/transcript.json
res.send(true);
});
app.post('/upload', upload.single('file'), (req,res) => {
console.log('upload request');
const { buffer:recording } = req.file;
fs.open('./public/eval/snellan_chart/recordings/raw/recording.ogg','w+', (err,fd) => {
fs.writeFile(fd,recording,(err)=> {
console.log("Wrote file!");
fs.close(fd, (err) => {
res.status(200).send('recording.ogg');
});
});
});
});
app.use('/', express.static(path.join(__dirname, 'public')));
app.use('/public', express.static(path.join(__dirname, 'public')));
app.listen(port, () => {
console.log(`EyeProject has been initialized! Listening on port ${port}`);
});