-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
290 lines (272 loc) · 9.33 KB
/
script.js
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// import { render, html } from "https://cdn.jsdelivr.net/npm/lit-html@3/+esm";
import * as d3 from "https://cdn.jsdelivr.net/npm/d3@7/+esm";
const $demos = document.getElementById("demos");
const $app = document.getElementById("app");
const loading = `<div class="text-center my-5"><div class="spinner-border" role="status"></div></div>`;
// Load configurations and render the demos
$demos.innerHTML = loading;
const { demos, emotions } = await fetch("config.json").then((r) => r.json());
$demos.innerHTML = demos
.map(
({ title, body, audio, prosody }) => /* html */ `
<div class="col py-3">
<a class="demo card h-100 text-decoration-none" href="${audio}" data-prosody="${prosody}">
<div class="card-body">
<h5 class="card-title">${title}</h5>
<p class="card-text">${body}</p>
</div>
</a>
</div>
`
)
.join("");
// Ensure that the user is logged in
const { token } = await fetch("https://llmfoundry.straive.com/token", {
credentials: "include",
}).then((res) => res.json());
if (!token) {
const url = "https://llmfoundry.straive.com/login?" + new URLSearchParams({ next: location.href });
$app.innerHTML = /* html */ `<div class="text-center my-5"><a class="btn btn-lg btn-primary" href="${url}">Log in to analyze</a></div>`;
throw new Error("User is not logged in");
}
// When the user clicks on a demo, analyze it
$demos.addEventListener("click", async (e) => {
e.preventDefault();
const $demo = e.target.closest(".demo");
if (!$demo) return;
$app.innerHTML = loading;
const prosody = await d3.csv($demo.dataset.prosody, d3.autoType);
const response = await fetch("https://llmfoundry.straive.com/openai/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}:patient-pulse`,
},
body: JSON.stringify({
model: "gpt-4o-mini",
messages: [
{
role: "system",
content: `You are a clinical trial expert. Read this call transcript. Identify all drugs, diseases, and symptoms mentioned. Return a JSON that mentions each along with the line in the call transcript they occur in. Example:
\`\`\`json
{
"symptoms": [
{"name": "...", "lines": [1, 4]}, // first symptom is mentioned in lines 1, 4
{"name": "...", "lines": [8]} // second symptom is mentioned in line 8
],
"drugs": [
{"name": "...", "lines": [6]} // first drug is mentioned in line 6
],
"emotions": [
{"name": "...", "lines": [9]} // first emotion is mentioned in line 9
]
}
\`\`\`
`,
},
{
role: "user",
content: prosody.map((line, i) => `${i + 1}. ${line.Text}`).join("\n"),
},
],
response_format: {
type: "json_schema",
json_schema: {
name: "extraction",
strict: true,
schema: extractionSchema,
},
},
}),
});
if (!response.ok) {
$app.innerHTML = /* html */ `<div class="text-center my-5"><pre class="alert alert-danger">${await response.text()}</pre></div>`;
return;
}
const result = await response.json();
let extracts;
try {
extracts = JSON.parse(result.choices[0].message.content);
} catch (e) {
$app.innerHTML = /* html */ `<div class="my-5">
<div class="alert alert-danger">
<h3 class="h5 alert-heading">${e.message}</h3>
<pre class="text-start" style="white-space: pre-wrap;">${JSON.stringify(result, null, 2)}</pre>
</div>
</div>`;
return;
}
// Render the UI
const transcript = prosody.map(({ Text }) => Text).join(" ");
const adverseEventsURL = "https://adverseevents.straive.app/?" + new URLSearchParams({ q: transcript });
$app.innerHTML = /* html */ `
<div class="d-flex align-items-center">
<audio src="${$demo.href}" controls class="w-100" autoplay></audio>
<a class="btn btn-primary text-nowrap ms-3" target="_blank" href="${adverseEventsURL}">
Check <span class="d-none d-md-inline">adverse events</span>
</a>
</div>
<div class="row my-3">
<div class="col-md extracts">
<div class="list-group drugs mb-3"></div>
<div class="list-group symptoms mb-3"></div>
<div class="list-group diseases mb-3"></div>
</div>
<div class="col-md transcript"></div>
<div class="col-md wheel position-relative">
<svg class="position-absolute top-0 start-0 img-fluid" viewBox="0 0 1080 1080" fill="currentColor">
<g class="slices" transform="translate(540, 540)"></g>
<g class="labels" transform="translate(540, 540)"></g>
</svg>
</div>
</div>
`;
const wheelRadius = 500;
const arc = d3.arc().innerRadius(0).outerRadius(wheelRadius).cornerRadius(2);
const pie = d3
.pie()
.value(() => 1)
.startAngle(-Math.PI / 2)
.endAngle((Math.PI * 3) / 2)
.padAngle(0.01);
const slices = d3
.select(".wheel svg .slices")
.selectAll("path")
.data(pie(emotions))
.join("path")
.attr("d", arc)
.attr("fill", (d, i) => d3.hsl((360 * i) / emotions.length, 0.7, 0.6))
.attr("stroke", "#fff")
.attr("stroke-width", 2);
// Add emotion labels
const labels = d3
.select(".wheel svg .labels")
.selectAll("text")
.data(pie(emotions))
.join("text")
.attr("transform", (d) => {
const angle = (d.startAngle + d.endAngle) / 2;
const radius = wheelRadius - 10;
const x = Math.cos(angle - Math.PI / 2) * radius;
const y = Math.sin(angle - Math.PI / 2) * radius;
const degrees = ((angle - Math.PI / 2) * 180) / Math.PI;
const flip = angle <= 0 || angle > Math.PI;
return `translate(${x}, ${y}) rotate(${degrees + (flip ? 180 : 0)})`;
})
.attr("text-anchor", (d) => {
const angle = (d.startAngle + d.endAngle) / 2;
const flip = angle <= 0 || angle > Math.PI;
return flip ? "start" : "end";
})
.attr("dominant-baseline", "middle")
.attr("font-size", "36px")
.text((d) => d.data);
// As the audio plays, update the UI
const audio = $app.querySelector("audio");
audio.addEventListener("timeupdate", () => {
const time = audio.currentTime;
let index = prosody.findLastIndex((line) => line.BeginTime <= time);
index = Math.max(0, index);
const line = prosody[index];
// Render transcript up to the current line, highlighting the current line
$app.querySelector(".transcript").innerHTML =
prosody
.slice(0, index)
.map((line, i) => `<span data-line="${i + 1}">${line.Text}</span> `)
.join("") + `<span data-line="${index + 1}" class="text-primary">${line.Text}</span> `;
// Update extracts up to the current line
for (const [key, items] of Object.entries(extracts)) {
const terms = items.filter(({ lines }) => lines.some((line) => line <= index));
$app.querySelector(`.extracts .${key}`).innerHTML = [
/* html */ `<a href="#" class="list-group-item active">${key.toUpperCase()}</a>`,
...terms.map(
({ name, lines }) =>
/* html */ `<a href="#" class="list-group-item" data-lines="${lines.join(",")}">${name}</a>`
),
].join("");
}
// Update slice radii based on emotion values
slices
.transition()
.duration(500)
.attr("d", (d) => arc.outerRadius(wheelRadius * Math.min(1, 2 * Math.max(0.1, prosody[index][d.data] ?? 0)))(d));
// Update labels opacity based on emotion values
labels.style("opacity", (d) => Math.min(1, 2 * (prosody[index][d.data] ?? 0)));
});
// Clicking on the list-group-item highlight the corresponding lines in the transcript
$app.querySelector(".extracts").addEventListener("click", (e) => {
const $item = e.target.closest("[data-lines]");
if (!$item) return;
e.preventDefault();
const lines = new Set($item.dataset.lines.split(","));
$app.querySelectorAll("[data-lines].text-bg-danger").forEach((el) => el.classList.remove("text-bg-danger"));
$item.classList.toggle("text-bg-danger");
$app
.querySelectorAll(".transcript [data-line]")
.forEach((el) => el.classList.toggle("text-bg-danger", lines.has(el.dataset.line)));
});
});
const extractionSchema = {
type: "object",
properties: {
symptoms: {
type: "array",
items: {
type: "object",
properties: {
name: {
type: "string",
},
lines: {
type: "array",
items: {
type: "integer",
},
},
},
required: ["name", "lines"],
additionalProperties: false,
},
},
drugs: {
type: "array",
items: {
type: "object",
properties: {
name: {
type: "string",
},
lines: {
type: "array",
items: {
type: "integer",
},
},
},
required: ["name", "lines"],
additionalProperties: false,
},
},
diseases: {
type: "array",
items: {
type: "object",
properties: {
name: {
type: "string",
},
lines: {
type: "array",
items: {
type: "integer",
},
},
},
required: ["name", "lines"],
additionalProperties: false,
},
},
},
required: ["symptoms", "drugs", "diseases"],
additionalProperties: false,
};