112 lines
4.2 KiB
Python
112 lines
4.2 KiB
Python
"""Capture agreement authorization states using synthetic data and no network."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from copy import deepcopy
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
|
|
|
from PySide6.QtWidgets import QApplication
|
|
from render_issued_prescription_ai import PreviewRepository
|
|
|
|
from doctor_workstation.services import DemoDoctorRepository
|
|
from doctor_workstation.ui.dialogs import issued_prescription_ai as ai
|
|
from doctor_workstation.ui.pages import prescriptions
|
|
from doctor_workstation.ui.theme import apply_theme
|
|
|
|
|
|
class VisibilityPreviewRepository(DemoDoctorRepository, PreviewRepository):
|
|
def __init__(self) -> None:
|
|
DemoDoctorRepository.__init__(self)
|
|
PreviewRepository.__init__(self)
|
|
self.visible = True
|
|
|
|
def get_prescription_ai_capabilities(self) -> dict[str, Any]:
|
|
return {"agreement_visible": self.visible, "can_manage_visibility": True}
|
|
|
|
def list_prescription_ai_statuses(self, ids: list[int]) -> dict[str, Any]:
|
|
return {"enabled": True, "agreement_visible": self.visible, "items": [
|
|
{"prescription_id": value, "agreement_visible": self.visible, "status": "success",
|
|
"models": {"qwen": {"status": "success", "score": 68.5},
|
|
"openai": {"status": "success", "score": 55.2}}} for value in ids]}
|
|
|
|
def list_prescription_ai_reports(self, **_params: Any) -> dict[str, Any]:
|
|
rows = [{**row, "agreement_visible": self.visible} for row in self.history()]
|
|
return {"agreement_visible": self.visible, "enabled": True, "lists": deepcopy(rows), "count": len(rows)}
|
|
|
|
def get_prescription_ai_report(self, _batch_id: int) -> dict[str, Any]:
|
|
return {**deepcopy(self.batch), "agreement_visible": self.visible}
|
|
|
|
|
|
def main() -> None:
|
|
application = QApplication.instance() or QApplication([])
|
|
apply_theme(application)
|
|
output = Path(__file__).resolve().parents[2] / "artifacts" / "prescription-ai-visibility"
|
|
output.mkdir(parents=True, exist_ok=True)
|
|
|
|
def immediate(function: Any, **callbacks: Any) -> None:
|
|
try:
|
|
result = function()
|
|
except Exception as error:
|
|
if callbacks.get("on_error"):
|
|
callbacks["on_error"](error)
|
|
else:
|
|
raise
|
|
else:
|
|
if callbacks.get("on_success"):
|
|
callbacks["on_success"](result)
|
|
finally:
|
|
if callbacks.get("on_finished"):
|
|
callbacks["on_finished"]()
|
|
|
|
ai.run_async = immediate
|
|
prescriptions.run_async = immediate
|
|
repository = VisibilityPreviewRepository()
|
|
|
|
def capture(widget: Any, filename: str) -> None:
|
|
for _ in range(4):
|
|
application.processEvents()
|
|
assert widget.grab().save(str(output / filename))
|
|
|
|
page = prescriptions.PrescriptionsPage(repository, ["*"], {"name": "演示医生", "root": 1})
|
|
page.resize(1366, 800)
|
|
page.show()
|
|
capture(page, "list-authorized.png")
|
|
repository.visible = False
|
|
page.refresh()
|
|
capture(page, "list-restricted.png")
|
|
page.close()
|
|
|
|
repository.visible = True
|
|
dialog = ai.IssuedPrescriptionAiDialog(repository, ["*"], prescription_id=7556,
|
|
current_user={"name": "演示医生", "root": 1})
|
|
dialog.resize(1280, 860)
|
|
dialog.show()
|
|
capture(dialog, "report-authorized.png")
|
|
repository.visible = False
|
|
repository.batch["status"] = "partial"
|
|
repository.batch["models"]["openai"].update(
|
|
status="failed", report=None, candidate=None, comparison=None,
|
|
error_code="upstream_timeout", error_message="演示:模型连接超时,可重试。")
|
|
dialog.refresh()
|
|
dialog._goto("report")
|
|
capture(dialog, "report-restricted.png")
|
|
dialog._goto("overview")
|
|
capture(dialog, "review-restricted.png")
|
|
dialog._goto("per_herb")
|
|
capture(dialog, "candidate-restricted.png")
|
|
dialog.close()
|
|
statistics = ai.PrescriptionAiStatisticsDialog(repository, ["*"])
|
|
statistics.resize(1180, 860)
|
|
statistics.show()
|
|
capture(statistics, "statistics-restricted.png")
|
|
statistics.close()
|
|
print(output)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|