Spaces:
Sleeping
Sleeping
File size: 7,211 Bytes
2fd08bb 083d098 e15864e 2fd08bb 083d098 2fd08bb 22195c1 2fd08bb 083d098 e15864e 2fd08bb eb6d0dc 22195c1 2fd08bb 22195c1 2fd08bb eb6d0dc 2fd08bb eb6d0dc 22195c1 2fd08bb 22195c1 2fd08bb 22195c1 e15864e 22195c1 e15864e 22195c1 2fd08bb eb6d0dc 2fd08bb e15864e 2fd08bb eb6d0dc 2fd08bb eb6d0dc 2fd08bb eb6d0dc 2fd08bb eb6d0dc 2fd08bb eb6d0dc 2fd08bb eb6d0dc 2fd08bb eb6d0dc 2fd08bb eb6d0dc 2fd08bb eb6d0dc 2fd08bb | 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 | from __future__ import annotations
import dataclasses
import re
from typing import Generator
import anthropic
import gradio as gr
from beacon_logging import get_logger
from dotenv import load_dotenv
from agents.intake import intake_greeting, stream_intake_turn
from agents.research import stream_research_agent
from models import PatientProfile
from translations import LANGUAGES, UI
load_dotenv()
_logger = get_logger("app")
_VERDICT_STYLE = {
"✓": "background:#22c55e;color:#fff;padding:2px 10px;border-radius:12px;font-weight:700;",
"✗": "background:#ef4444;color:#fff;padding:2px 10px;border-radius:12px;font-weight:700;",
"!": "background:#eab308;color:#fff;padding:2px 10px;border-radius:12px;font-weight:700;",
}
_VERDICT_RE = re.compile(r"(?m)^(\s*)(✓|✗|!)\s")
def _colorize(text: str) -> str:
def _replace(m: re.Match) -> str:
symbol = m.group(2)
style = _VERDICT_STYLE[symbol]
return f'{m.group(1)}<span style="{style}">{symbol}</span> '
return _VERDICT_RE.sub(_replace, text)
def initialize(lang: str = "en"):
client = anthropic.Anthropic()
text, msgs = intake_greeting(client, lang)
chat = [{"role": "assistant", "content": text}]
return chat, msgs, None, "intake"
def respond(
user_msg: str,
chat_history: list,
intake_msgs: list,
profile: PatientProfile | None,
phase: str,
lang: str,
) -> Generator:
if not user_msg.strip() or phase == "done":
yield chat_history, intake_msgs, profile, phase, gr.update(), gr.update()
return
t = UI[lang]
client = anthropic.Anthropic()
chat_history = chat_history + [{"role": "user", "content": user_msg}]
new_intake_msgs = intake_msgs + [{"role": "user", "content": user_msg}]
yield chat_history, intake_msgs, profile, phase, gr.update(value=""), gr.update()
intake_text = ""
for event in stream_intake_turn(client, new_intake_msgs, lang):
if event[0] == "token":
intake_text += event[1]
yield (
chat_history + [{"role": "assistant", "content": intake_text}],
intake_msgs, profile, phase, gr.update(), gr.update(),
)
elif event[0] == "reset_stream":
intake_text = ""
elif event[0] == "text":
_, full_text, updated_msgs = event
chat_history = chat_history + [{"role": "assistant", "content": full_text}]
yield (
chat_history, updated_msgs, profile, "intake",
gr.update(interactive=True), gr.update(),
)
return
elif event[0] == "profile":
_, new_profile, updated_msgs = event
_logger.info(
"Patient intake complete (web)",
extra={"data": {"intake_summary": dataclasses.asdict(new_profile)}},
)
if intake_text:
chat_history = chat_history + [{"role": "assistant", "content": intake_text}]
chat_history = chat_history + [{"role": "assistant", "content": t["status_searching"]}]
yield (
chat_history, updated_msgs, new_profile, "researching",
gr.update(interactive=False, placeholder=t["searching"]),
gr.update(visible=False),
)
stream_text = ""
for rev in stream_research_agent(client, new_profile):
if rev[0] == "token":
stream_text += rev[1]
yield (
chat_history + [{"role": "assistant", "content": _colorize(stream_text)}],
updated_msgs, new_profile, "researching",
gr.update(interactive=False, placeholder=t["searching"]),
gr.update(visible=False),
)
elif rev[0] == "status":
yield (
chat_history + [{"role": "assistant", "content": rev[1]}],
updated_msgs, new_profile, "researching",
gr.update(interactive=False, placeholder=t["searching"]),
gr.update(visible=False),
)
elif rev[0] == "done":
analysis = _colorize(rev[1] or t["no_analysis"])
chat_history = chat_history + [{"role": "assistant", "content": analysis}]
yield (
chat_history, updated_msgs, new_profile, "done",
gr.update(interactive=False, placeholder=t["search_complete"]),
gr.update(visible=True),
)
return
def change_language(lang: str):
t = UI[lang]
chat, msgs, _, phase = initialize(lang)
return (
chat, msgs, None, phase,
gr.update(value=t["heading"]),
gr.update(placeholder=t["placeholder"], interactive=True),
gr.update(value=t["send"]),
gr.update(value=t["new_search"], visible=False),
lang,
)
with gr.Blocks(title=UI["en"]["page_title"]) as demo:
heading_md = gr.Markdown(UI["en"]["heading"])
with gr.Row():
gr.Markdown("") # spacer
lang_dropdown = gr.Dropdown(
choices=[(label, code) for code, label in LANGUAGES.items()],
value="en",
show_label=False,
scale=1,
min_width=140,
container=False,
)
chatbot = gr.Chatbot(height=550, show_label=False, sanitize_html=False)
with gr.Row():
msg_box = gr.Textbox(
placeholder=UI["en"]["placeholder"],
show_label=False,
scale=9,
autofocus=True,
)
send_btn = gr.Button(UI["en"]["send"], scale=1, variant="primary")
new_search_btn = gr.Button(UI["en"]["new_search"], visible=False, variant="secondary")
# State
intake_msgs_state = gr.State([])
profile_state = gr.State(None)
phase_state = gr.State("intake")
lang_state = gr.State("en")
respond_inputs = [msg_box, chatbot, intake_msgs_state, profile_state, phase_state, lang_state]
respond_outputs = [chatbot, intake_msgs_state, profile_state, phase_state, msg_box, new_search_btn]
demo.load(
lambda: initialize("en"),
outputs=[chatbot, intake_msgs_state, profile_state, phase_state],
)
msg_box.submit(respond, respond_inputs, respond_outputs)
send_btn.click(respond, respond_inputs, respond_outputs)
new_search_btn.click(
initialize,
inputs=[lang_state],
outputs=[chatbot, intake_msgs_state, profile_state, phase_state],
).then(
lambda lang: (
gr.update(interactive=True, placeholder=UI[lang]["placeholder"]),
gr.update(visible=False),
),
inputs=[lang_state],
outputs=[msg_box, new_search_btn],
)
lang_dropdown.change(
change_language,
inputs=[lang_dropdown],
outputs=[
chatbot, intake_msgs_state, profile_state, phase_state,
heading_md, msg_box, send_btn, new_search_btn,
lang_state,
],
)
if __name__ == "__main__":
demo.launch()
|