Spaces:
Running on Zero
Running on Zero
File size: 5,548 Bytes
e8c5cc2 | 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 | """Gradio UI for ResearchOS.
Talks to the FastAPI app over HTTP rather than importing the pipeline, so the
demo exercises the same path a real client would and the API stays the single
place where queries and clicks are logged.
python scripts/serve_ui.py --api http://127.0.0.1:8000
"""
from __future__ import annotations
from collections.abc import Callable
from typing import Any
import gradio as gr
import httpx
MAX_CITATIONS = 5
REQUEST_TIMEOUT = 120.0
EXAMPLES = [
"0-dimensional biomaterials show inductive properties.",
"Vitamin D supplementation reduces the risk of respiratory tract infection.",
"Aspirin lowers the risk of colorectal cancer.",
"Sleep deprivation impairs immune function.",
]
def _post(api_url: str, path: str, payload: dict[str, Any]) -> dict[str, Any]:
response = httpx.post(f"{api_url.rstrip('/')}{path}", json=payload, timeout=REQUEST_TIMEOUT)
response.raise_for_status()
return response.json()
def build_demo(api_url: str) -> gr.Blocks:
"""Construct the Gradio app bound to a running API."""
def _blank(message: str) -> list[Any]:
return [gr.update(value=message), None, [], ""] + [
gr.update(visible=False) for _ in range(MAX_CITATIONS * 2)
]
def ask(question: str) -> list[Any]:
question = (question or "").strip()
if len(question) < 3:
return _blank("Enter a claim or question first.")
try:
result = _post(api_url, "/ask", {"query": question, "top_k": MAX_CITATIONS})
except Exception as exc: # noqa: BLE001 - show transport errors in the UI
return _blank(f"**API error:** {exc}")
citations = result.get("citations", [])
timing = result.get("timing_ms", {})
footer = (
f"backend `{result.get('backend')}` | "
f"retrieve+rerank {timing.get('retrieval_and_rerank')} ms | "
f"generate {timing.get('generation')} ms"
)
if not result.get("generation_configured"):
footer += " | `ANTHROPIC_API_KEY` unset - passages only"
updates: list[Any] = [
gr.update(value=result.get("answer") or "_No answer generated._"),
result.get("query_id"),
[c["doc_id"] for c in citations],
footer,
]
for i in range(MAX_CITATIONS):
if i < len(citations):
c = citations[i]
label = f"[{c['doc_id']}] {c['title'][:90]} (score {c['score']:.2f})"
updates.append(gr.update(visible=True, label=label))
updates.append(gr.update(value=c["text"]))
else:
updates.append(gr.update(visible=False))
updates.append(gr.update(value=""))
return updates
def make_click_handler(index: int) -> Callable[[int | None, list[str]], str]:
def handler(query_id: int | None, doc_ids: list[str]) -> str:
if query_id is None or not doc_ids or index >= len(doc_ids):
return "Ask something first."
doc_id = doc_ids[index]
try:
stats = _post(
api_url, "/feedback/click", {"query_id": int(query_id), "doc_id": doc_id}
)
except Exception as exc: # noqa: BLE001
return f"Click not logged: {exc}"
return f"Logged click on [{doc_id}] - {stats['clicks']} clicks recorded so far."
return handler
# Gradio 6 moved `theme` from the Blocks constructor to launch().
with gr.Blocks(title="ResearchOS") as demo:
gr.Markdown(
"# ResearchOS\n"
"Scientific claim verification over SciFact. BM25 + dense retrieval, fused with "
"RRF, reranked by a cross-encoder fine-tuned on **hard negatives**, answered by "
"Claude with span citations."
)
with gr.Row():
question_box = gr.Textbox(
label="Claim or question",
placeholder="e.g. Vitamin D supplementation reduces respiratory infection risk.",
scale=4,
)
submit = gr.Button("Search", variant="primary", scale=1)
gr.Examples(examples=EXAMPLES, inputs=question_box)
answer_box = gr.Markdown(label="Answer")
footer_box = gr.Markdown()
query_id_state = gr.State(None)
doc_ids_state = gr.State([])
click_status = gr.Markdown()
gr.Markdown("### Retrieved passages")
accordions: list[gr.Accordion] = []
bodies: list[gr.Markdown] = []
for i in range(MAX_CITATIONS):
with gr.Accordion(label=f"Passage {i + 1}", open=(i == 0), visible=False) as acc:
body = gr.Markdown()
mark = gr.Button("Mark as useful", size="sm")
mark.click(
make_click_handler(i),
inputs=[query_id_state, doc_ids_state],
outputs=click_status,
)
accordions.append(acc)
bodies.append(body)
outputs: list[Any] = [answer_box, query_id_state, doc_ids_state, footer_box]
for acc, body in zip(accordions, bodies):
outputs.extend([acc, body])
submit.click(ask, inputs=question_box, outputs=outputs)
question_box.submit(ask, inputs=question_box, outputs=outputs)
return demo
|