Adam Cseresznye commited on
Commit ·
a1bbee2
1
Parent(s): 35f8b4d
UI search progress indicator added
Browse files- main.py +1 -1
- services/query_service.py +19 -2
- ui/components.py +47 -0
- ui/pages.py +122 -161
main.py
CHANGED
|
@@ -132,4 +132,4 @@ ui.run_with(
|
|
| 132 |
if __name__ == "__main__":
|
| 133 |
import uvicorn
|
| 134 |
|
| 135 |
-
uvicorn.run("main:app", host="0.0.0.0", port=7860, log_level="info", reload=
|
|
|
|
| 132 |
if __name__ == "__main__":
|
| 133 |
import uvicorn
|
| 134 |
|
| 135 |
+
uvicorn.run("main:app", host="0.0.0.0", port=7860, log_level="info", reload=True)
|
services/query_service.py
CHANGED
|
@@ -20,7 +20,7 @@ from __future__ import annotations
|
|
| 20 |
|
| 21 |
import asyncio
|
| 22 |
import time
|
| 23 |
-
from typing import Dict, List, Any, Optional, TYPE_CHECKING
|
| 24 |
from dataclasses import dataclass, field
|
| 25 |
from loguru import logger
|
| 26 |
|
|
@@ -338,7 +338,9 @@ class QueryService:
|
|
| 338 |
|
| 339 |
return papers, quality_result
|
| 340 |
|
| 341 |
-
async def process_query(
|
|
|
|
|
|
|
| 342 |
"""Process a user query through the complete pipeline.
|
| 343 |
|
| 344 |
Stages:
|
|
@@ -355,6 +357,7 @@ class QueryService:
|
|
| 355 |
|
| 356 |
Args:
|
| 357 |
query: The user query string
|
|
|
|
| 358 |
|
| 359 |
Returns:
|
| 360 |
PipelineResult with all processing results and metadata
|
|
@@ -377,9 +380,19 @@ class QueryService:
|
|
| 377 |
refinement_count=0,
|
| 378 |
)
|
| 379 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 380 |
try:
|
| 381 |
logger.info("Starting query pipeline for: {}", query[:100])
|
| 382 |
|
|
|
|
|
|
|
| 383 |
# Stage 1: Query Optimization
|
| 384 |
logger.info("Stage 1: Query optimization - input query: {}", query[:100])
|
| 385 |
stage_start = time.perf_counter()
|
|
@@ -406,6 +419,7 @@ class QueryService:
|
|
| 406 |
) from e
|
| 407 |
|
| 408 |
# Stage 2 & 3: Semantic Scholar Search + Quality Gate (with refinement loop)
|
|
|
|
| 409 |
queries = [
|
| 410 |
optimized_queries["primary"],
|
| 411 |
optimized_queries["broad"],
|
|
@@ -523,6 +537,7 @@ class QueryService:
|
|
| 523 |
return result
|
| 524 |
|
| 525 |
# Stage 5: PDF Acquisition
|
|
|
|
| 526 |
logger.info(
|
| 527 |
"🧮 Stage 5: PDF Acquisition - downloading PDFs for {} finalists",
|
| 528 |
len(finalists),
|
|
@@ -568,6 +583,7 @@ class QueryService:
|
|
| 568 |
return result
|
| 569 |
|
| 570 |
# Stage 6: Processing & Embedding
|
|
|
|
| 571 |
logger.info(
|
| 572 |
"📄 Stage 6: Processing & Embedding - chunking {} passages",
|
| 573 |
len(passages),
|
|
@@ -727,6 +743,7 @@ class QueryService:
|
|
| 727 |
|
| 728 |
# Stage 9: Synthesis
|
| 729 |
if final_passages:
|
|
|
|
| 730 |
logger.info(
|
| 731 |
"📝 Stage 9: Synthesis - generating answer from {} passages",
|
| 732 |
len(final_passages),
|
|
|
|
| 20 |
|
| 21 |
import asyncio
|
| 22 |
import time
|
| 23 |
+
from typing import Dict, List, Any, Optional, TYPE_CHECKING, Callable
|
| 24 |
from dataclasses import dataclass, field
|
| 25 |
from loguru import logger
|
| 26 |
|
|
|
|
| 338 |
|
| 339 |
return papers, quality_result
|
| 340 |
|
| 341 |
+
async def process_query(
|
| 342 |
+
self, query: str, progress_callback: Optional[Callable] = None
|
| 343 |
+
) -> PipelineResult:
|
| 344 |
"""Process a user query through the complete pipeline.
|
| 345 |
|
| 346 |
Stages:
|
|
|
|
| 357 |
|
| 358 |
Args:
|
| 359 |
query: The user query string
|
| 360 |
+
progress_callback: Optional async callback to report progress
|
| 361 |
|
| 362 |
Returns:
|
| 363 |
PipelineResult with all processing results and metadata
|
|
|
|
| 380 |
refinement_count=0,
|
| 381 |
)
|
| 382 |
|
| 383 |
+
# Helper to safely await callback
|
| 384 |
+
async def report_progress():
|
| 385 |
+
if progress_callback:
|
| 386 |
+
if asyncio.iscoroutinefunction(progress_callback):
|
| 387 |
+
await progress_callback()
|
| 388 |
+
else:
|
| 389 |
+
progress_callback()
|
| 390 |
+
|
| 391 |
try:
|
| 392 |
logger.info("Starting query pipeline for: {}", query[:100])
|
| 393 |
|
| 394 |
+
await report_progress()
|
| 395 |
+
|
| 396 |
# Stage 1: Query Optimization
|
| 397 |
logger.info("Stage 1: Query optimization - input query: {}", query[:100])
|
| 398 |
stage_start = time.perf_counter()
|
|
|
|
| 419 |
) from e
|
| 420 |
|
| 421 |
# Stage 2 & 3: Semantic Scholar Search + Quality Gate (with refinement loop)
|
| 422 |
+
await report_progress()
|
| 423 |
queries = [
|
| 424 |
optimized_queries["primary"],
|
| 425 |
optimized_queries["broad"],
|
|
|
|
| 537 |
return result
|
| 538 |
|
| 539 |
# Stage 5: PDF Acquisition
|
| 540 |
+
await report_progress()
|
| 541 |
logger.info(
|
| 542 |
"🧮 Stage 5: PDF Acquisition - downloading PDFs for {} finalists",
|
| 543 |
len(finalists),
|
|
|
|
| 583 |
return result
|
| 584 |
|
| 585 |
# Stage 6: Processing & Embedding
|
| 586 |
+
await report_progress()
|
| 587 |
logger.info(
|
| 588 |
"📄 Stage 6: Processing & Embedding - chunking {} passages",
|
| 589 |
len(passages),
|
|
|
|
| 743 |
|
| 744 |
# Stage 9: Synthesis
|
| 745 |
if final_passages:
|
| 746 |
+
await report_progress()
|
| 747 |
logger.info(
|
| 748 |
"📝 Stage 9: Synthesis - generating answer from {} passages",
|
| 749 |
len(final_passages),
|
ui/components.py
CHANGED
|
@@ -133,3 +133,50 @@ def render_exception_error(exception: Exception):
|
|
| 133 |
ui.icon("error_outline", color="#ef4444", size="sm")
|
| 134 |
ui.label("Error").classes("text-lg font-semibold text-red-600")
|
| 135 |
ui.label(str(exception)).classes("text-red-500 mt-2")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
ui.icon("error_outline", color="#ef4444", size="sm")
|
| 134 |
ui.label("Error").classes("text-lg font-semibold text-red-600")
|
| 135 |
ui.label(str(exception)).classes("text-red-500 mt-2")
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
class SearchProgress:
|
| 139 |
+
"""Vertical stepper to visualize search progress."""
|
| 140 |
+
|
| 141 |
+
def __init__(self):
|
| 142 |
+
with ui.column().classes("w-full items-center"):
|
| 143 |
+
with ui.card().classes(
|
| 144 |
+
"w-full max-w-md p-6 shadow-lg border border-slate-100"
|
| 145 |
+
):
|
| 146 |
+
ui.label("Researching...").classes(
|
| 147 |
+
"text-xl font-bold text-slate-700 mb-4 text-center w-full"
|
| 148 |
+
)
|
| 149 |
+
self.stepper = (
|
| 150 |
+
ui.stepper().props("vertical animated flat").classes("w-full")
|
| 151 |
+
)
|
| 152 |
+
with self.stepper:
|
| 153 |
+
with ui.step("Refining Query"):
|
| 154 |
+
ui.label("Clarifying your research question...").classes(
|
| 155 |
+
"text-slate-600"
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
with ui.step("Searching Sources"):
|
| 159 |
+
ui.label("Scanning millions of open-access papers...").classes(
|
| 160 |
+
"text-slate-600"
|
| 161 |
+
)
|
| 162 |
+
|
| 163 |
+
with ui.step("Reading Documents"):
|
| 164 |
+
with ui.column().classes("gap-2"):
|
| 165 |
+
ui.label(
|
| 166 |
+
"Downloading and reading full-text PDFs..."
|
| 167 |
+
).classes("text-slate-600")
|
| 168 |
+
ui.spinner("dots", size="lg", color="primary")
|
| 169 |
+
|
| 170 |
+
with ui.step("Analyzing Data"):
|
| 171 |
+
ui.label("Extracting key evidence and citations...").classes(
|
| 172 |
+
"text-slate-600"
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
with ui.step("Writing Answer"):
|
| 176 |
+
ui.label("Drafting your cited summary...").classes(
|
| 177 |
+
"text-slate-600"
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
def next_step(self):
|
| 181 |
+
"""Advance to the next step."""
|
| 182 |
+
self.stepper.next()
|
ui/pages.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
"""Page definitions for the Necthrall UI."""
|
| 2 |
|
|
|
|
| 3 |
from pathlib import Path
|
| 4 |
from nicegui import ui, context, app
|
| 5 |
from loguru import logger
|
|
@@ -11,6 +12,7 @@ from ui.components import (
|
|
| 11 |
render_sources,
|
| 12 |
render_error,
|
| 13 |
render_exception_error,
|
|
|
|
| 14 |
)
|
| 15 |
from ui.policies import PRIVACY_POLICY, TERMS_OF_SERVICE
|
| 16 |
from ui.constants import POSTHOG_SCRIPT, BUY_ME_COFFEE_WIDGET
|
|
@@ -140,67 +142,26 @@ def init_ui(fastapi_app):
|
|
| 140 |
# Client deleted, exit gracefully
|
| 141 |
return
|
| 142 |
|
| 143 |
-
# Hide
|
| 144 |
-
if
|
| 145 |
-
|
| 146 |
-
example_queries_row.set_visibility(False)
|
| 147 |
-
except RuntimeError:
|
| 148 |
-
pass
|
| 149 |
|
| 150 |
-
# Show
|
| 151 |
-
|
| 152 |
-
# --- START: Enhanced Loading UI ---
|
| 153 |
-
loading_timer = None
|
| 154 |
with results_container:
|
| 155 |
-
|
| 156 |
-
"w-full items-center justify-center py-12 gap-6"
|
| 157 |
-
):
|
| 158 |
-
# 1. Modern Ring Spinner
|
| 159 |
-
ui.spinner("rings", size="4rem", color="primary").props(
|
| 160 |
-
"thickness=4"
|
| 161 |
-
)
|
| 162 |
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
# 3. Tip (responsive)
|
| 169 |
-
ui.label(
|
| 170 |
-
"This may take 30-60 seconds. We are reading real papers. Please do not close this tab."
|
| 171 |
-
).classes(
|
| 172 |
-
"text-sm md:text-base lg:text-lg text-slate-400 italic text-center max-w-xl mx-auto"
|
| 173 |
-
)
|
| 174 |
-
|
| 175 |
-
# Simulated progress steps
|
| 176 |
-
messages = [
|
| 177 |
-
"Refining your research question...",
|
| 178 |
-
"Reading metadata from 200M+ papers...",
|
| 179 |
-
"Filtering for high-impact studies...",
|
| 180 |
-
"Downloading full-text documents...",
|
| 181 |
-
"Reading and analyzing paper content...",
|
| 182 |
-
"Isolating key evidence and data...",
|
| 183 |
-
"Cross-referencing claims against sources...",
|
| 184 |
-
"Drafting the final response...",
|
| 185 |
-
"Double-checking citation links...",
|
| 186 |
-
"Finalizing your research summary...",
|
| 187 |
-
"Almost ready...",
|
| 188 |
-
]
|
| 189 |
-
msg_idx = 0
|
| 190 |
-
|
| 191 |
-
def update_progress():
|
| 192 |
-
nonlocal msg_idx
|
| 193 |
-
if msg_idx < len(messages):
|
| 194 |
-
status_label.set_text(messages[msg_idx])
|
| 195 |
-
msg_idx = (msg_idx + 1) % len(messages)
|
| 196 |
-
|
| 197 |
-
# Update text every 2.5 seconds
|
| 198 |
-
loading_timer = ui.timer(2.5, update_progress)
|
| 199 |
-
# --- END: Enhanced Loading UI ---
|
| 200 |
|
| 201 |
try:
|
| 202 |
-
# Call the query service
|
| 203 |
-
result = await fastapi_app.state.query_service.process_query(
|
|
|
|
|
|
|
| 204 |
|
| 205 |
# Check if client is still connected before updating UI
|
| 206 |
if not is_connected():
|
|
@@ -216,6 +177,14 @@ def init_ui(fastapi_app):
|
|
| 216 |
# Client was deleted, silently ignore
|
| 217 |
return
|
| 218 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
with results_container:
|
| 220 |
if result.success and result.answer:
|
| 221 |
# Success - show answer and sources
|
|
@@ -282,15 +251,13 @@ def init_ui(fastapi_app):
|
|
| 282 |
return
|
| 283 |
try:
|
| 284 |
results_container.clear()
|
|
|
|
|
|
|
| 285 |
with results_container:
|
| 286 |
render_exception_error(e)
|
| 287 |
except RuntimeError:
|
| 288 |
# Client was deleted, silently ignore
|
| 289 |
pass
|
| 290 |
-
finally:
|
| 291 |
-
# Clean up the timer to prevent errors
|
| 292 |
-
if loading_timer:
|
| 293 |
-
loading_timer.cancel()
|
| 294 |
|
| 295 |
# =====================================================================
|
| 296 |
# HEADER
|
|
@@ -334,7 +301,7 @@ def init_ui(fastapi_app):
|
|
| 334 |
)
|
| 335 |
# --- HERO SECTION END ---
|
| 336 |
# Search container
|
| 337 |
-
with ui.column().classes("search-container"):
|
| 338 |
# Search wrapper
|
| 339 |
with ui.row().classes("search-wrapper w-full items-center gap-2"):
|
| 340 |
ui.icon("search", size="sm").classes("text-slate-400 ml-3")
|
|
@@ -371,116 +338,110 @@ def init_ui(fastapi_app):
|
|
| 371 |
on_click=lambda e=example: set_example(e),
|
| 372 |
).props("flat dense").classes("example-btn")
|
| 373 |
|
| 374 |
-
|
| 375 |
-
|
| 376 |
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
|
|
|
| 420 |
):
|
| 421 |
-
with (
|
| 422 |
-
ui.
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
passage["title"]
|
| 430 |
-
).classes("source-title")
|
| 431 |
-
|
| 432 |
-
meta_parts = []
|
| 433 |
-
if passage["section"]:
|
| 434 |
-
meta_parts.append(
|
| 435 |
-
passage["section"]
|
| 436 |
-
)
|
| 437 |
meta_parts.append(
|
| 438 |
-
|
| 439 |
)
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
)
|
| 460 |
-
else:
|
| 461 |
-
with ui.row().classes(
|
| 462 |
-
"items-center mt-2"
|
| 463 |
):
|
| 464 |
-
ui.
|
| 465 |
-
|
| 466 |
-
sanitize=False,
|
| 467 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 468 |
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
passage["content"]
|
| 474 |
-
).classes("source-snippet")
|
| 475 |
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
|
| 482 |
-
|
| 483 |
-
|
| 484 |
|
| 485 |
# =====================================================================
|
| 486 |
# FOOTER (centered)
|
|
|
|
| 1 |
"""Page definitions for the Necthrall UI."""
|
| 2 |
|
| 3 |
+
import asyncio
|
| 4 |
from pathlib import Path
|
| 5 |
from nicegui import ui, context, app
|
| 6 |
from loguru import logger
|
|
|
|
| 12 |
render_sources,
|
| 13 |
render_error,
|
| 14 |
render_exception_error,
|
| 15 |
+
SearchProgress,
|
| 16 |
)
|
| 17 |
from ui.policies import PRIVACY_POLICY, TERMS_OF_SERVICE
|
| 18 |
from ui.constants import POSTHOG_SCRIPT, BUY_ME_COFFEE_WIDGET
|
|
|
|
| 142 |
# Client deleted, exit gracefully
|
| 143 |
return
|
| 144 |
|
| 145 |
+
# Hide search container to focus on progress
|
| 146 |
+
if search_container_ui:
|
| 147 |
+
search_container_ui.set_visibility(False)
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
+
# Show progress stepper
|
| 150 |
+
progress_ui = None
|
|
|
|
|
|
|
| 151 |
with results_container:
|
| 152 |
+
progress_ui = SearchProgress()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
+
# Async callback to ensure UI updates
|
| 155 |
+
async def advance_progress():
|
| 156 |
+
progress_ui.next_step()
|
| 157 |
+
# Force UI update by yielding control
|
| 158 |
+
await asyncio.sleep(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
try:
|
| 161 |
+
# Call the query service with progress callback
|
| 162 |
+
result = await fastapi_app.state.query_service.process_query(
|
| 163 |
+
query_text, progress_callback=advance_progress
|
| 164 |
+
)
|
| 165 |
|
| 166 |
# Check if client is still connected before updating UI
|
| 167 |
if not is_connected():
|
|
|
|
| 177 |
# Client was deleted, silently ignore
|
| 178 |
return
|
| 179 |
|
| 180 |
+
# Restore search container
|
| 181 |
+
if search_container_ui:
|
| 182 |
+
search_container_ui.set_visibility(True)
|
| 183 |
+
|
| 184 |
+
# Ensure example queries remain hidden after search
|
| 185 |
+
if example_queries_row:
|
| 186 |
+
example_queries_row.set_visibility(False)
|
| 187 |
+
|
| 188 |
with results_container:
|
| 189 |
if result.success and result.answer:
|
| 190 |
# Success - show answer and sources
|
|
|
|
| 251 |
return
|
| 252 |
try:
|
| 253 |
results_container.clear()
|
| 254 |
+
if search_container_ui:
|
| 255 |
+
search_container_ui.set_visibility(True)
|
| 256 |
with results_container:
|
| 257 |
render_exception_error(e)
|
| 258 |
except RuntimeError:
|
| 259 |
# Client was deleted, silently ignore
|
| 260 |
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
# =====================================================================
|
| 263 |
# HEADER
|
|
|
|
| 301 |
)
|
| 302 |
# --- HERO SECTION END ---
|
| 303 |
# Search container
|
| 304 |
+
with ui.column().classes("search-container") as search_container_ui:
|
| 305 |
# Search wrapper
|
| 306 |
with ui.row().classes("search-wrapper w-full items-center gap-2"):
|
| 307 |
ui.icon("search", size="sm").classes("text-slate-400 ml-3")
|
|
|
|
| 338 |
on_click=lambda e=example: set_example(e),
|
| 339 |
).props("flat dense").classes("example-btn")
|
| 340 |
|
| 341 |
+
# Results container
|
| 342 |
+
results_container = ui.column().classes("w-full max-w-4xl mt-6")
|
| 343 |
|
| 344 |
+
# Attempt to restore cached result from localStorage on page load
|
| 345 |
+
def restore_cached_result():
|
| 346 |
+
"""Restore result from localStorage if available."""
|
| 347 |
+
try:
|
| 348 |
+
import json
|
| 349 |
|
| 350 |
+
# Retrieve cached result from localStorage
|
| 351 |
+
cached_json = ui.run_javascript(
|
| 352 |
+
"localStorage.getItem('necthrall_last_result')", timeout=5
|
| 353 |
+
)
|
| 354 |
+
if cached_json:
|
| 355 |
+
cached_data = json.loads(cached_json)
|
| 356 |
+
search_input.value = cached_data.get("query", "")
|
| 357 |
+
|
| 358 |
+
# Populate the results container
|
| 359 |
+
with results_container:
|
| 360 |
+
# Render answer
|
| 361 |
+
with ui.column().classes("answer-section w-full"):
|
| 362 |
+
with ui.row().classes("items-center gap-3 mb-4"):
|
| 363 |
+
with ui.row().classes("gap-2"):
|
| 364 |
+
ui.label(
|
| 365 |
+
f"⏱️ {cached_data['execution_time']:.1f}s"
|
| 366 |
+
).classes("stats-badge")
|
| 367 |
+
ui.label(
|
| 368 |
+
f"📄 {cached_data['finalists']} papers analyzed"
|
| 369 |
+
).classes("stats-badge")
|
| 370 |
+
ui.label(
|
| 371 |
+
f"📝 {len(cached_data['passages'])} sources cited"
|
| 372 |
+
).classes("stats-badge")
|
| 373 |
+
|
| 374 |
+
ui.markdown(cached_data["answer"]).classes("answer-text")
|
| 375 |
+
|
| 376 |
+
# Render sources
|
| 377 |
+
if cached_data["passages"]:
|
| 378 |
+
with ui.column().classes("sources-section w-full"):
|
| 379 |
+
ui.label("SOURCES CITED").classes("sources-title")
|
| 380 |
+
|
| 381 |
+
for idx, passage in enumerate(
|
| 382 |
+
cached_data["passages"]
|
| 383 |
+
):
|
| 384 |
+
with (
|
| 385 |
+
ui.expansion()
|
| 386 |
+
.classes("source-card")
|
| 387 |
+
.props("dense") as expansion
|
| 388 |
):
|
| 389 |
+
with expansion.add_slot("header"):
|
| 390 |
+
with ui.column().classes("w-full"):
|
| 391 |
+
ui.label(passage["title"]).classes(
|
| 392 |
+
"source-title"
|
| 393 |
+
)
|
| 394 |
+
|
| 395 |
+
meta_parts = []
|
| 396 |
+
if passage["section"]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
meta_parts.append(
|
| 398 |
+
passage["section"]
|
| 399 |
)
|
| 400 |
+
meta_parts.append(
|
| 401 |
+
f"Citation [{idx + 1}]"
|
| 402 |
+
)
|
| 403 |
+
ui.label(
|
| 404 |
+
" • ".join(meta_parts)
|
| 405 |
+
).classes("source-meta")
|
| 406 |
+
|
| 407 |
+
if passage["url"]:
|
| 408 |
+
with ui.row().classes(
|
| 409 |
+
"items-center mt-2"
|
| 410 |
+
):
|
| 411 |
+
with (
|
| 412 |
+
ui.element("a")
|
| 413 |
+
.props(
|
| 414 |
+
f'href="{passage["url"]}" target="_blank"'
|
| 415 |
+
)
|
| 416 |
+
.classes(
|
| 417 |
+
"source-link-btn"
|
| 418 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 419 |
):
|
| 420 |
+
ui.label(
|
| 421 |
+
"📄 Open Access PDF"
|
|
|
|
| 422 |
)
|
| 423 |
+
else:
|
| 424 |
+
with ui.row().classes(
|
| 425 |
+
"items-center mt-2"
|
| 426 |
+
):
|
| 427 |
+
ui.html(
|
| 428 |
+
'<span class="source-link-badge">📄 PDF not available</span>',
|
| 429 |
+
sanitize=False,
|
| 430 |
+
)
|
| 431 |
|
| 432 |
+
with ui.column().classes("passage-content"):
|
| 433 |
+
ui.label(passage["content"]).classes(
|
| 434 |
+
"source-snippet"
|
| 435 |
+
)
|
|
|
|
|
|
|
| 436 |
|
| 437 |
+
logger.info(
|
| 438 |
+
f"✅ Restored cached result for query: {cached_data['query']}"
|
| 439 |
+
)
|
| 440 |
+
except Exception as e:
|
| 441 |
+
logger.debug(f"No cached result to restore: {e}")
|
| 442 |
|
| 443 |
+
# Run restoration on page load
|
| 444 |
+
ui.timer(0.5, restore_cached_result, once=True)
|
| 445 |
|
| 446 |
# =====================================================================
|
| 447 |
# FOOTER (centered)
|