Create ui/ui_core.py
Browse files- ui/ui_core.py +68 -0
ui/ui_core.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# ui/ui_core.py
|
2 |
+
import gradio as gr
|
3 |
+
import json
|
4 |
+
|
5 |
+
question_examples = [
|
6 |
+
["Given a patient with WHIM syndrome on prophylactic antibiotics, is it advisable to co-administer Xolremdi with fluconazole?"],
|
7 |
+
["What treatment options exist for HER2+ breast cancer resistant to trastuzumab?"]
|
8 |
+
]
|
9 |
+
|
10 |
+
def extract_tool_name_and_clean_content(msg):
|
11 |
+
tool_name = "Tool Result"
|
12 |
+
content = msg.get("content") if isinstance(msg, dict) else getattr(msg, "content", "")
|
13 |
+
try:
|
14 |
+
parsed = json.loads(content)
|
15 |
+
if isinstance(parsed, dict):
|
16 |
+
tool_name = parsed.get("tool_name", tool_name)
|
17 |
+
content = parsed.get("content", content)
|
18 |
+
except Exception:
|
19 |
+
pass
|
20 |
+
if isinstance(content, (dict, list)):
|
21 |
+
content = json.dumps(content, indent=2)
|
22 |
+
return f"Tool: {tool_name}", content
|
23 |
+
|
24 |
+
def format_collapsible(content, title="Answer"):
|
25 |
+
return (
|
26 |
+
f"<details style='border: 1px solid #ccc; border-radius: 8px; padding: 10px; margin-top: 10px;'>"
|
27 |
+
f"<summary style='font-size: 16px; font-weight: bold; color: #3B82F6;'>{title}</summary>"
|
28 |
+
f"<div style='margin-top: 8px; font-size: 15px; line-height: 1.6; white-space: pre-wrap;'>{content}</div></details>"
|
29 |
+
)
|
30 |
+
|
31 |
+
def create_ui(agent):
|
32 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
33 |
+
gr.Markdown("<h1 style='text-align: center;'>💊 TxAgent: Therapeutic Reasoning</h1>")
|
34 |
+
chatbot = gr.Chatbot(label="TxAgent", height=600, type="messages")
|
35 |
+
message_input = gr.Textbox(placeholder="Ask a biomedical question...", show_label=False)
|
36 |
+
send_button = gr.Button("Send", variant="primary")
|
37 |
+
conversation_state = gr.State([])
|
38 |
+
|
39 |
+
def handle_chat(message, history, conversation):
|
40 |
+
generator = agent.run_gradio_chat(
|
41 |
+
message=message,
|
42 |
+
history=history,
|
43 |
+
temperature=0.3,
|
44 |
+
max_new_tokens=1024,
|
45 |
+
max_token=8192,
|
46 |
+
call_agent=False,
|
47 |
+
conversation=conversation,
|
48 |
+
max_round=30
|
49 |
+
)
|
50 |
+
for update in generator:
|
51 |
+
formatted = []
|
52 |
+
for m in update:
|
53 |
+
role = m.get("role") if isinstance(m, dict) else getattr(m, "role", "assistant")
|
54 |
+
if role == "assistant":
|
55 |
+
title, clean = extract_tool_name_and_clean_content(m)
|
56 |
+
content = format_collapsible(clean, title)
|
57 |
+
else:
|
58 |
+
content = m.get("content") if isinstance(m, dict) else getattr(m, "content", "")
|
59 |
+
formatted.append({"role": role, "content": content})
|
60 |
+
yield formatted
|
61 |
+
|
62 |
+
send_button.click(fn=handle_chat, inputs=[message_input, chatbot, conversation_state], outputs=chatbot)
|
63 |
+
message_input.submit(fn=handle_chat, inputs=[message_input, chatbot, conversation_state], outputs=chatbot)
|
64 |
+
|
65 |
+
gr.Examples(examples=question_examples, inputs=message_input)
|
66 |
+
gr.Markdown("<small style='color: gray;'>DISCLAIMER: This demo is for research purposes only and does not provide medical advice.</small>")
|
67 |
+
|
68 |
+
return demo
|