Spaces:
Running
Running
main dot py file added both separate pages evaluation and chat
Browse files- app/main.py +317 -264
app/main.py
CHANGED
|
@@ -221,11 +221,12 @@ def health():
|
|
| 221 |
|
| 222 |
@app.get("/", response_class=HTMLResponse)
|
| 223 |
def chat_ui():
|
| 224 |
-
html = """
|
|
|
|
| 225 |
<html lang="en">
|
| 226 |
<head>
|
| 227 |
<meta charset="UTF-8" />
|
| 228 |
-
<title>RAGBench Chat
|
| 229 |
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
| 230 |
<style>
|
| 231 |
* { box-sizing: border-box; }
|
|
@@ -237,121 +238,41 @@ def chat_ui():
|
|
| 237 |
color: #111827;
|
| 238 |
}
|
| 239 |
.app {
|
| 240 |
-
max-width:
|
| 241 |
margin: 0 auto;
|
| 242 |
height: 100vh;
|
| 243 |
display: flex;
|
| 244 |
flex-direction: column;
|
| 245 |
padding: 0.75rem;
|
| 246 |
-
gap: 0.75rem;
|
| 247 |
}
|
| 248 |
header {
|
| 249 |
display: flex;
|
| 250 |
align-items: center;
|
| 251 |
justify-content: space-between;
|
| 252 |
-
|
| 253 |
}
|
| 254 |
header h1 {
|
|
|
|
| 255 |
margin: 0;
|
| 256 |
-
font-size: 1.3rem;
|
| 257 |
}
|
| 258 |
header small {
|
| 259 |
color: #6b7280;
|
|
|
|
| 260 |
}
|
| 261 |
-
select
|
| 262 |
-
|
| 263 |
-
}
|
| 264 |
-
select, input {
|
| 265 |
border-radius: 999px;
|
| 266 |
border: 1px solid #d1d5db;
|
| 267 |
-
padding: 0.25rem 0.6rem;
|
| 268 |
-
font-size: 0.85rem;
|
| 269 |
background: white;
|
|
|
|
| 270 |
}
|
| 271 |
-
.chat-
|
| 272 |
-
display: flex;
|
| 273 |
-
flex-direction: column;
|
| 274 |
flex: 1;
|
| 275 |
-
min-height: 0;
|
| 276 |
-
gap: 0.75rem;
|
| 277 |
-
}
|
| 278 |
-
.eval-card {
|
| 279 |
-
background: white;
|
| 280 |
-
border-radius: 0.75rem;
|
| 281 |
-
padding: 0.75rem 1rem;
|
| 282 |
-
box-shadow: 0 1px 3px rgba(15, 23, 42, 0.08);
|
| 283 |
-
border: 1px solid #e5e7eb;
|
| 284 |
-
}
|
| 285 |
-
.eval-header {
|
| 286 |
-
display: flex;
|
| 287 |
-
justify-content: space-between;
|
| 288 |
-
align-items: baseline;
|
| 289 |
-
gap: 1rem;
|
| 290 |
-
margin-bottom: 0.5rem;
|
| 291 |
-
}
|
| 292 |
-
.eval-header h2 {
|
| 293 |
-
margin: 0;
|
| 294 |
-
font-size: 1rem;
|
| 295 |
-
}
|
| 296 |
-
.eval-header p {
|
| 297 |
-
margin: 0;
|
| 298 |
-
font-size: 0.8rem;
|
| 299 |
-
color: #6b7280;
|
| 300 |
-
}
|
| 301 |
-
.eval-grid {
|
| 302 |
-
display: grid;
|
| 303 |
-
grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
|
| 304 |
-
gap: 0.5rem 0.75rem;
|
| 305 |
-
align-items: end;
|
| 306 |
-
margin-bottom: 0.5rem;
|
| 307 |
-
}
|
| 308 |
-
.field {
|
| 309 |
-
display: flex;
|
| 310 |
-
flex-direction: column;
|
| 311 |
-
gap: 0.15rem;
|
| 312 |
-
font-size: 0.8rem;
|
| 313 |
-
}
|
| 314 |
-
.field label {
|
| 315 |
-
color: #4b5563;
|
| 316 |
-
}
|
| 317 |
-
.eval-actions {
|
| 318 |
-
display: flex;
|
| 319 |
-
align-items: center;
|
| 320 |
-
gap: 0.75rem;
|
| 321 |
-
flex-wrap: wrap;
|
| 322 |
-
font-size: 0.8rem;
|
| 323 |
-
}
|
| 324 |
-
.eval-status {
|
| 325 |
-
color: #6b7280;
|
| 326 |
-
min-height: 1.1rem;
|
| 327 |
-
}
|
| 328 |
-
.eval-output {
|
| 329 |
-
margin-top: 0.35rem;
|
| 330 |
-
max-height: 180px;
|
| 331 |
-
overflow: auto;
|
| 332 |
-
background: #f9fafb;
|
| 333 |
-
border-radius: 0.5rem;
|
| 334 |
-
padding: 0.5rem 0.75rem;
|
| 335 |
-
font-size: 0.75rem;
|
| 336 |
-
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
|
| 337 |
-
border: 1px solid #e5e7eb;
|
| 338 |
-
}
|
| 339 |
-
.chat-card {
|
| 340 |
background: white;
|
| 341 |
border-radius: 0.75rem;
|
| 342 |
-
padding: 0.75rem
|
| 343 |
-
box-shadow: 0 1px 3px rgba(15, 23, 42, 0.08);
|
| 344 |
-
border: 1px solid #e5e7eb;
|
| 345 |
-
display: flex;
|
| 346 |
-
flex-direction: column;
|
| 347 |
-
flex: 1;
|
| 348 |
-
min-height: 0;
|
| 349 |
-
}
|
| 350 |
-
.chat-window {
|
| 351 |
-
flex: 1;
|
| 352 |
-
min-height: 0;
|
| 353 |
overflow-y: auto;
|
| 354 |
-
|
| 355 |
}
|
| 356 |
.message-row {
|
| 357 |
display: flex;
|
|
@@ -360,26 +281,24 @@ def chat_ui():
|
|
| 360 |
.message-row.user {
|
| 361 |
justify-content: flex-end;
|
| 362 |
}
|
| 363 |
-
.message-row.assistant {
|
| 364 |
-
justify-content: flex-start;
|
| 365 |
-
}
|
| 366 |
.bubble {
|
| 367 |
-
max-width:
|
| 368 |
-
padding: 0.
|
| 369 |
border-radius: 0.75rem;
|
| 370 |
-
font-size: 0.
|
| 371 |
-
white-space: pre-wrap;
|
| 372 |
line-height: 1.35;
|
|
|
|
|
|
|
| 373 |
}
|
| 374 |
.bubble.user {
|
| 375 |
background: #2563eb;
|
| 376 |
color: white;
|
| 377 |
-
border-bottom-right-radius: 0.
|
| 378 |
}
|
| 379 |
.bubble.assistant {
|
| 380 |
background: #e5e7eb;
|
| 381 |
color: #111827;
|
| 382 |
-
border-bottom-left-radius: 0.
|
| 383 |
}
|
| 384 |
.status {
|
| 385 |
margin-top: 0.25rem;
|
|
@@ -387,7 +306,7 @@ def chat_ui():
|
|
| 387 |
color: #6b7280;
|
| 388 |
min-height: 1.1rem;
|
| 389 |
}
|
| 390 |
-
form
|
| 391 |
margin-top: 0.5rem;
|
| 392 |
display: flex;
|
| 393 |
gap: 0.5rem;
|
|
@@ -411,19 +330,15 @@ def chat_ui():
|
|
| 411 |
font-size: 0.9rem;
|
| 412 |
font-weight: 500;
|
| 413 |
cursor: pointer;
|
| 414 |
-
white-space: nowrap;
|
| 415 |
}
|
| 416 |
button:disabled {
|
| 417 |
opacity: 0.6;
|
| 418 |
cursor: default;
|
| 419 |
}
|
| 420 |
@media (max-width: 640px) {
|
| 421 |
-
.app {
|
| 422 |
-
|
| 423 |
-
}
|
| 424 |
-
.bubble {
|
| 425 |
-
max-width: 100%;
|
| 426 |
-
}
|
| 427 |
}
|
| 428 |
</style>
|
| 429 |
</head>
|
|
@@ -435,7 +350,6 @@ def chat_ui():
|
|
| 435 |
<small>Select a domain, then start chatting.</small>
|
| 436 |
</div>
|
| 437 |
<div>
|
| 438 |
-
<label for="domain" style="font-size:0.8rem;color:#4b5563;margin-right:0.25rem;">Domain</label>
|
| 439 |
<select id="domain">
|
| 440 |
<option value="biomedical">Biomedical</option>
|
| 441 |
<option value="general_knowledge">General knowledge</option>
|
|
@@ -446,166 +360,305 @@ def chat_ui():
|
|
| 446 |
</div>
|
| 447 |
</header>
|
| 448 |
|
| 449 |
-
<div class="chat-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 457 |
</div>
|
| 458 |
-
<div
|
| 459 |
-
<
|
| 460 |
-
|
| 461 |
-
<select id="eval-domain">
|
| 462 |
-
<option value="biomedical">Biomedical</option>
|
| 463 |
-
<option value="general_knowledge">General knowledge</option>
|
| 464 |
-
<option value="customer_support">Customer support</option>
|
| 465 |
-
<option value="finance">Finance</option>
|
| 466 |
-
<option value="legal">Legal</option>
|
| 467 |
-
</select>
|
| 468 |
-
</div>
|
| 469 |
-
<div class="field">
|
| 470 |
-
<label for="eval-k">Top-k documents</label>
|
| 471 |
-
<input id="eval-k" type="number" min="1" max="10" value="3" />
|
| 472 |
-
</div>
|
| 473 |
-
<div class="field">
|
| 474 |
-
<label for="eval-max-examples">Max examples</label>
|
| 475 |
-
<input id="eval-max-examples" type="number" min="1" value="5" />
|
| 476 |
-
</div>
|
| 477 |
-
<div class="field">
|
| 478 |
-
<label for="eval-split">Dataset split</label>
|
| 479 |
-
<input id="eval-split" type="text" value="validation" />
|
| 480 |
-
</div>
|
| 481 |
</div>
|
| 482 |
-
<div
|
| 483 |
-
<
|
| 484 |
-
<
|
|
|
|
|
|
|
|
|
|
| 485 |
</div>
|
| 486 |
-
|
| 487 |
-
</
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
<textarea id="question" placeholder="Ask a question..."></textarea>
|
| 496 |
-
<button type="submit">Send</button>
|
| 497 |
-
</form>
|
| 498 |
-
</section>
|
| 499 |
</div>
|
| 500 |
</div>
|
| 501 |
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
const
|
| 514 |
-
const
|
| 515 |
-
const
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
if (
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
addMessage("user", question);
|
| 547 |
-
questionEl.value = "";
|
| 548 |
-
statusEl.textContent = "Thinking...";
|
| 549 |
-
form.querySelector("button").disabled = true;
|
| 550 |
-
|
| 551 |
-
try {
|
| 552 |
-
const payload = {
|
| 553 |
-
question: question,
|
| 554 |
-
domain: domainEl.value || "biomedical",
|
| 555 |
-
};
|
| 556 |
-
|
| 557 |
-
const resp = await fetch("/chat", {
|
| 558 |
-
method: "POST",
|
| 559 |
-
headers: { "Content-Type": "application/json" },
|
| 560 |
-
body: JSON.stringify(payload),
|
| 561 |
-
});
|
| 562 |
-
|
| 563 |
-
if (!resp.ok) {
|
| 564 |
-
throw new Error("HTTP " + resp.status);
|
| 565 |
-
}
|
| 566 |
-
|
| 567 |
-
const data = await resp.json();
|
| 568 |
-
addMessage("assistant", data.answer || "[No answer returned]");
|
| 569 |
-
statusEl.textContent = "";
|
| 570 |
-
} catch (err) {
|
| 571 |
-
statusEl.textContent = "Error: " + err;
|
| 572 |
-
} finally {
|
| 573 |
-
form.querySelector("button").disabled = false;
|
| 574 |
-
}
|
| 575 |
-
});
|
| 576 |
-
|
| 577 |
-
evalForm.addEventListener("submit", async (event) => {
|
| 578 |
-
event.preventDefault();
|
| 579 |
-
evalStatusEl.textContent = "Running evaluation...";
|
| 580 |
-
evalOutputEl.textContent = "";
|
| 581 |
-
|
| 582 |
-
const payload = {
|
| 583 |
-
domain: evalDomainEl.value || "biomedical",
|
| 584 |
-
k: parseInt(evalKEl.value || "3", 10),
|
| 585 |
-
max_examples: parseInt(evalMaxExamplesEl.value || "5", 10),
|
| 586 |
-
split: evalSplitEl.value || "validation",
|
| 587 |
-
};
|
| 588 |
-
|
| 589 |
-
try {
|
| 590 |
-
const resp = await fetch("/run_domain", {
|
| 591 |
-
method: "POST",
|
| 592 |
-
headers: { "Content-Type": "application/json" },
|
| 593 |
-
body: JSON.stringify(payload),
|
| 594 |
-
});
|
| 595 |
-
|
| 596 |
-
if (!resp.ok) {
|
| 597 |
-
throw new Error("HTTP " + resp.status);
|
| 598 |
-
}
|
| 599 |
-
|
| 600 |
-
const data = await resp.json();
|
| 601 |
-
evalStatusEl.textContent = "Done.";
|
| 602 |
-
evalOutputEl.textContent = JSON.stringify(data, null, 2);
|
| 603 |
-
} catch (err) {
|
| 604 |
-
evalStatusEl.textContent = "Error: " + err;
|
| 605 |
}
|
| 606 |
-
|
| 607 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 608 |
</body>
|
| 609 |
-
</html>
|
|
|
|
| 610 |
return HTMLResponse(content=html)
|
| 611 |
-
|
|
|
|
| 221 |
|
| 222 |
@app.get("/", response_class=HTMLResponse)
|
| 223 |
def chat_ui():
|
| 224 |
+
html = """
|
| 225 |
+
<!DOCTYPE html>
|
| 226 |
<html lang="en">
|
| 227 |
<head>
|
| 228 |
<meta charset="UTF-8" />
|
| 229 |
+
<title>RAGBench Chat</title>
|
| 230 |
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
| 231 |
<style>
|
| 232 |
* { box-sizing: border-box; }
|
|
|
|
| 238 |
color: #111827;
|
| 239 |
}
|
| 240 |
.app {
|
| 241 |
+
max-width: 960px;
|
| 242 |
margin: 0 auto;
|
| 243 |
height: 100vh;
|
| 244 |
display: flex;
|
| 245 |
flex-direction: column;
|
| 246 |
padding: 0.75rem;
|
|
|
|
| 247 |
}
|
| 248 |
header {
|
| 249 |
display: flex;
|
| 250 |
align-items: center;
|
| 251 |
justify-content: space-between;
|
| 252 |
+
margin-bottom: 0.75rem;
|
| 253 |
}
|
| 254 |
header h1 {
|
| 255 |
+
font-size: 1.1rem;
|
| 256 |
margin: 0;
|
|
|
|
| 257 |
}
|
| 258 |
header small {
|
| 259 |
color: #6b7280;
|
| 260 |
+
font-size: 0.75rem;
|
| 261 |
}
|
| 262 |
+
select {
|
| 263 |
+
padding: 0.3rem 0.5rem;
|
|
|
|
|
|
|
| 264 |
border-radius: 999px;
|
| 265 |
border: 1px solid #d1d5db;
|
|
|
|
|
|
|
| 266 |
background: white;
|
| 267 |
+
font-size: 0.85rem;
|
| 268 |
}
|
| 269 |
+
.chat-window {
|
|
|
|
|
|
|
| 270 |
flex: 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
background: white;
|
| 272 |
border-radius: 0.75rem;
|
| 273 |
+
padding: 0.75rem;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
overflow-y: auto;
|
| 275 |
+
border: 1px solid #e5e7eb;
|
| 276 |
}
|
| 277 |
.message-row {
|
| 278 |
display: flex;
|
|
|
|
| 281 |
.message-row.user {
|
| 282 |
justify-content: flex-end;
|
| 283 |
}
|
|
|
|
|
|
|
|
|
|
| 284 |
.bubble {
|
| 285 |
+
max-width: 70%;
|
| 286 |
+
padding: 0.5rem 0.75rem;
|
| 287 |
border-radius: 0.75rem;
|
| 288 |
+
font-size: 0.9rem;
|
|
|
|
| 289 |
line-height: 1.35;
|
| 290 |
+
white-space: pre-wrap;
|
| 291 |
+
word-wrap: break-word;
|
| 292 |
}
|
| 293 |
.bubble.user {
|
| 294 |
background: #2563eb;
|
| 295 |
color: white;
|
| 296 |
+
border-bottom-right-radius: 0.15rem;
|
| 297 |
}
|
| 298 |
.bubble.assistant {
|
| 299 |
background: #e5e7eb;
|
| 300 |
color: #111827;
|
| 301 |
+
border-bottom-left-radius: 0.15rem;
|
| 302 |
}
|
| 303 |
.status {
|
| 304 |
margin-top: 0.25rem;
|
|
|
|
| 306 |
color: #6b7280;
|
| 307 |
min-height: 1.1rem;
|
| 308 |
}
|
| 309 |
+
form {
|
| 310 |
margin-top: 0.5rem;
|
| 311 |
display: flex;
|
| 312 |
gap: 0.5rem;
|
|
|
|
| 330 |
font-size: 0.9rem;
|
| 331 |
font-weight: 500;
|
| 332 |
cursor: pointer;
|
|
|
|
| 333 |
}
|
| 334 |
button:disabled {
|
| 335 |
opacity: 0.6;
|
| 336 |
cursor: default;
|
| 337 |
}
|
| 338 |
@media (max-width: 640px) {
|
| 339 |
+
.app { padding: 0.5rem; }
|
| 340 |
+
.chat-window { padding: 0.5rem; }
|
| 341 |
+
.bubble { max-width: 82%; }
|
|
|
|
|
|
|
|
|
|
| 342 |
}
|
| 343 |
</style>
|
| 344 |
</head>
|
|
|
|
| 350 |
<small>Select a domain, then start chatting.</small>
|
| 351 |
</div>
|
| 352 |
<div>
|
|
|
|
| 353 |
<select id="domain">
|
| 354 |
<option value="biomedical">Biomedical</option>
|
| 355 |
<option value="general_knowledge">General knowledge</option>
|
|
|
|
| 360 |
</div>
|
| 361 |
</header>
|
| 362 |
|
| 363 |
+
<div id="chat" class="chat-window"></div>
|
| 364 |
+
<div id="status" class="status"></div>
|
| 365 |
+
|
| 366 |
+
<form id="chat-form">
|
| 367 |
+
<textarea id="question" placeholder="Ask a question..."></textarea>
|
| 368 |
+
<button type="submit">Send</button>
|
| 369 |
+
</form>
|
| 370 |
+
</div>
|
| 371 |
+
|
| 372 |
+
<script>
|
| 373 |
+
const form = document.getElementById("chat-form");
|
| 374 |
+
const questionEl = document.getElementById("question");
|
| 375 |
+
const domainEl = document.getElementById("domain");
|
| 376 |
+
const chatEl = document.getElementById("chat");
|
| 377 |
+
const statusEl = document.getElementById("status");
|
| 378 |
+
|
| 379 |
+
function addMessage(role, text) {
|
| 380 |
+
const row = document.createElement("div");
|
| 381 |
+
row.className = "message-row " + role;
|
| 382 |
+
|
| 383 |
+
const bubble = document.createElement("div");
|
| 384 |
+
bubble.className = "bubble " + role;
|
| 385 |
+
bubble.textContent = text;
|
| 386 |
+
|
| 387 |
+
row.appendChild(bubble);
|
| 388 |
+
chatEl.appendChild(row);
|
| 389 |
+
chatEl.scrollTop = chatEl.scrollHeight;
|
| 390 |
+
}
|
| 391 |
+
|
| 392 |
+
form.addEventListener("submit", async (e) => {
|
| 393 |
+
e.preventDefault();
|
| 394 |
+
const question = questionEl.value.trim();
|
| 395 |
+
if (!question) return;
|
| 396 |
+
|
| 397 |
+
const domain = domainEl.value;
|
| 398 |
+
addMessage("user", question);
|
| 399 |
+
questionEl.value = "";
|
| 400 |
+
statusEl.textContent = "Thinking...";
|
| 401 |
+
form.querySelector("button").disabled = true;
|
| 402 |
+
|
| 403 |
+
try {
|
| 404 |
+
const resp = await fetch("/chat", {
|
| 405 |
+
method: "POST",
|
| 406 |
+
headers: { "Content-Type": "application/json" },
|
| 407 |
+
body: JSON.stringify({ domain, question }),
|
| 408 |
+
});
|
| 409 |
+
|
| 410 |
+
if (!resp.ok) {
|
| 411 |
+
throw new Error("HTTP " + resp.status);
|
| 412 |
+
}
|
| 413 |
+
|
| 414 |
+
const data = await resp.json();
|
| 415 |
+
addMessage("assistant", data.answer || "[No answer returned]");
|
| 416 |
+
statusEl.textContent = "";
|
| 417 |
+
} catch (err) {
|
| 418 |
+
statusEl.textContent = "Error: " + err;
|
| 419 |
+
} finally {
|
| 420 |
+
form.querySelector("button").disabled = false;
|
| 421 |
+
}
|
| 422 |
+
});
|
| 423 |
+
</script>
|
| 424 |
+
</body>
|
| 425 |
+
</html>
|
| 426 |
+
"""
|
| 427 |
+
return HTMLResponse(content=html)
|
| 428 |
+
|
| 429 |
+
|
| 430 |
+
|
| 431 |
+
# ---------------------------------------------------------------------
|
| 432 |
+
# HTML RAGBench Evaluation UI at "/eval"
|
| 433 |
+
# ---------------------------------------------------------------------
|
| 434 |
+
|
| 435 |
+
@app.get("/eval", response_class=HTMLResponse)
|
| 436 |
+
def eval_ui():
|
| 437 |
+
"""Simple page to run /run_domain evaluations from the browser."""
|
| 438 |
+
html = """
|
| 439 |
+
<!DOCTYPE html>
|
| 440 |
+
<html lang="en">
|
| 441 |
+
<head>
|
| 442 |
+
<meta charset="UTF-8" />
|
| 443 |
+
<title>RAGBench RAG Evaluation</title>
|
| 444 |
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
| 445 |
+
<style>
|
| 446 |
+
* { box-sizing: border-box; }
|
| 447 |
+
body {
|
| 448 |
+
margin: 0;
|
| 449 |
+
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
|
| 450 |
+
background: #f3f4f6;
|
| 451 |
+
color: #111827;
|
| 452 |
+
}
|
| 453 |
+
.app {
|
| 454 |
+
max-width: 960px;
|
| 455 |
+
margin: 0 auto;
|
| 456 |
+
padding: 24px 16px 40px;
|
| 457 |
+
}
|
| 458 |
+
header {
|
| 459 |
+
display: flex;
|
| 460 |
+
justify-content: space-between;
|
| 461 |
+
align-items: baseline;
|
| 462 |
+
margin-bottom: 20px;
|
| 463 |
+
}
|
| 464 |
+
header h1 {
|
| 465 |
+
font-size: 1.6rem;
|
| 466 |
+
margin: 0;
|
| 467 |
+
}
|
| 468 |
+
header nav a {
|
| 469 |
+
font-size: 0.9rem;
|
| 470 |
+
color: #2563eb;
|
| 471 |
+
text-decoration: none;
|
| 472 |
+
margin-left: 12px;
|
| 473 |
+
}
|
| 474 |
+
header nav a:hover {
|
| 475 |
+
text-decoration: underline;
|
| 476 |
+
}
|
| 477 |
+
.card {
|
| 478 |
+
background: #ffffff;
|
| 479 |
+
border-radius: 12px;
|
| 480 |
+
box-shadow: 0 10px 25px rgba(15, 23, 42, 0.08);
|
| 481 |
+
padding: 20px 20px 24px;
|
| 482 |
+
margin-bottom: 24px;
|
| 483 |
+
}
|
| 484 |
+
.card-title {
|
| 485 |
+
font-size: 1.1rem;
|
| 486 |
+
font-weight: 600;
|
| 487 |
+
margin-bottom: 12px;
|
| 488 |
+
}
|
| 489 |
+
.form-row {
|
| 490 |
+
display: grid;
|
| 491 |
+
grid-template-columns: repeat(4, minmax(0, 1fr));
|
| 492 |
+
gap: 12px;
|
| 493 |
+
margin-bottom: 12px;
|
| 494 |
+
}
|
| 495 |
+
label {
|
| 496 |
+
display: block;
|
| 497 |
+
font-size: 0.8rem;
|
| 498 |
+
font-weight: 500;
|
| 499 |
+
color: #4b5563;
|
| 500 |
+
margin-bottom: 4px;
|
| 501 |
+
}
|
| 502 |
+
select, input {
|
| 503 |
+
width: 100%;
|
| 504 |
+
padding: 6px 8px;
|
| 505 |
+
border-radius: 8px;
|
| 506 |
+
border: 1px solid #d1d5db;
|
| 507 |
+
font-size: 0.9rem;
|
| 508 |
+
outline: none;
|
| 509 |
+
}
|
| 510 |
+
select:focus, input:focus {
|
| 511 |
+
border-color: #2563eb;
|
| 512 |
+
box-shadow: 0 0 0 1px #2563eb33;
|
| 513 |
+
}
|
| 514 |
+
button {
|
| 515 |
+
padding: 8px 16px;
|
| 516 |
+
border-radius: 999px;
|
| 517 |
+
border: none;
|
| 518 |
+
background: #2563eb;
|
| 519 |
+
color: #ffffff;
|
| 520 |
+
font-weight: 500;
|
| 521 |
+
font-size: 0.95rem;
|
| 522 |
+
cursor: pointer;
|
| 523 |
+
}
|
| 524 |
+
button:disabled {
|
| 525 |
+
opacity: 0.6;
|
| 526 |
+
cursor: default;
|
| 527 |
+
}
|
| 528 |
+
#status {
|
| 529 |
+
font-size: 0.85rem;
|
| 530 |
+
color: #6b7280;
|
| 531 |
+
margin-left: 12px;
|
| 532 |
+
}
|
| 533 |
+
pre {
|
| 534 |
+
background: #111827;
|
| 535 |
+
color: #e5e7eb;
|
| 536 |
+
padding: 16px;
|
| 537 |
+
border-radius: 10px;
|
| 538 |
+
font-size: 0.8rem;
|
| 539 |
+
overflow: auto;
|
| 540 |
+
max-height: 60vh;
|
| 541 |
+
}
|
| 542 |
+
@media (max-width: 768px) {
|
| 543 |
+
.form-row {
|
| 544 |
+
grid-template-columns: repeat(2, minmax(0, 1fr));
|
| 545 |
+
}
|
| 546 |
+
}
|
| 547 |
+
@media (max-width: 480px) {
|
| 548 |
+
.form-row {
|
| 549 |
+
grid-template-columns: 1fr;
|
| 550 |
+
}
|
| 551 |
+
}
|
| 552 |
+
</style>
|
| 553 |
+
</head>
|
| 554 |
+
<body>
|
| 555 |
+
<div class="app">
|
| 556 |
+
<header>
|
| 557 |
+
<h1>RAGBench RAG Evaluation</h1>
|
| 558 |
+
<nav>
|
| 559 |
+
<a href="/">Chat</a>
|
| 560 |
+
<a href="/eval"><strong>Evaluation</strong></a>
|
| 561 |
+
</nav>
|
| 562 |
+
</header>
|
| 563 |
+
|
| 564 |
+
<div class="card">
|
| 565 |
+
<div class="card-title">Run Domain Evaluation</div>
|
| 566 |
+
<form id="eval-form">
|
| 567 |
+
<div class="form-row">
|
| 568 |
+
<div>
|
| 569 |
+
<label for="domain">Domain</label>
|
| 570 |
+
<select id="domain" name="domain">
|
| 571 |
+
<option value="biomedical">Biomedical</option>
|
| 572 |
+
<option value="general_knowledge">General Knowledge</option>
|
| 573 |
+
<option value="legal">Legal</option>
|
| 574 |
+
<option value="customer_support">Customer Support</option>
|
| 575 |
+
<option value="finance">Finance</option>
|
| 576 |
+
</select>
|
| 577 |
+
</div>
|
| 578 |
+
<div>
|
| 579 |
+
<label for="k">Top-k documents</label>
|
| 580 |
+
<input id="k" name="k" type="number" min="1" value="3" />
|
| 581 |
</div>
|
| 582 |
+
<div>
|
| 583 |
+
<label for="max_examples">Max examples</label>
|
| 584 |
+
<input id="max_examples" name="max_examples" type="number" min="1" value="20" />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 585 |
</div>
|
| 586 |
+
<div>
|
| 587 |
+
<label for="split">Dataset split</label>
|
| 588 |
+
<select id="split" name="split">
|
| 589 |
+
<option value="test">test</option>
|
| 590 |
+
<option value="validation">validation</option>
|
| 591 |
+
</select>
|
| 592 |
</div>
|
| 593 |
+
</div>
|
| 594 |
+
<button type="submit">Run Domain Evaluation</button>
|
| 595 |
+
<span id="status"></span>
|
| 596 |
+
</form>
|
| 597 |
+
</div>
|
| 598 |
+
|
| 599 |
+
<div class="card">
|
| 600 |
+
<div class="card-title">Results</div>
|
| 601 |
+
<pre id="output">{}</pre>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 602 |
</div>
|
| 603 |
</div>
|
| 604 |
|
| 605 |
+
<script>
|
| 606 |
+
const form = document.getElementById("eval-form");
|
| 607 |
+
const statusEl = document.getElementById("status");
|
| 608 |
+
const outputEl = document.getElementById("output");
|
| 609 |
+
|
| 610 |
+
form.addEventListener("submit", async (ev) => {
|
| 611 |
+
ev.preventDefault();
|
| 612 |
+
statusEl.textContent = "Running evaluation...";
|
| 613 |
+
outputEl.textContent = "{}";
|
| 614 |
+
form.querySelector("button").disabled = true;
|
| 615 |
+
|
| 616 |
+
const domain = document.getElementById("domain").value;
|
| 617 |
+
const k = parseInt(document.getElementById("k").value || "3", 10);
|
| 618 |
+
const maxExamplesRaw = document.getElementById("max_examples").value;
|
| 619 |
+
const split = document.getElementById("split").value;
|
| 620 |
+
|
| 621 |
+
const payload = {
|
| 622 |
+
domain: domain,
|
| 623 |
+
k: k,
|
| 624 |
+
split: split,
|
| 625 |
+
};
|
| 626 |
+
if (maxExamplesRaw !== "") {
|
| 627 |
+
payload.max_examples = parseInt(maxExamplesRaw, 10);
|
| 628 |
+
}
|
| 629 |
+
|
| 630 |
+
try {
|
| 631 |
+
const resp = await fetch("/run_domain", {
|
| 632 |
+
method: "POST",
|
| 633 |
+
headers: {
|
| 634 |
+
"Content-Type": "application/json",
|
| 635 |
+
},
|
| 636 |
+
body: JSON.stringify(payload),
|
| 637 |
+
});
|
| 638 |
+
|
| 639 |
+
if (!resp.ok) {
|
| 640 |
+
let msg = "HTTP " + resp.status;
|
| 641 |
+
try {
|
| 642 |
+
const errData = await resp.json();
|
| 643 |
+
if (errData.detail) {
|
| 644 |
+
msg += " - " + errData.detail;
|
| 645 |
+
}
|
| 646 |
+
} catch (_) {}
|
| 647 |
+
throw new Error(msg);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 648 |
}
|
| 649 |
+
|
| 650 |
+
const data = await resp.json();
|
| 651 |
+
statusEl.textContent = "Done.";
|
| 652 |
+
outputEl.textContent = JSON.stringify(data, null, 2);
|
| 653 |
+
} catch (err) {
|
| 654 |
+
statusEl.textContent = "Error: " + err;
|
| 655 |
+
outputEl.textContent = "{}";
|
| 656 |
+
} finally {
|
| 657 |
+
form.querySelector("button").disabled = false;
|
| 658 |
+
}
|
| 659 |
+
});
|
| 660 |
+
</script>
|
| 661 |
</body>
|
| 662 |
+
</html>
|
| 663 |
+
"""
|
| 664 |
return HTMLResponse(content=html)
|
|
|