Update app.py
Browse files
app.py
CHANGED
|
@@ -36,88 +36,61 @@ question_examples = [
|
|
| 36 |
["What treatment options exist for HER2+ breast cancer resistant to trastuzumab?"]
|
| 37 |
]
|
| 38 |
|
| 39 |
-
# Helper:
|
|
|
|
| 40 |
def format_collapsible(content, tool_name=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
if isinstance(content, dict) and "results" in content:
|
| 42 |
readable = ""
|
| 43 |
for i, result in enumerate(content["results"], 1):
|
| 44 |
-
readable += f"Result {i}
|
| 45 |
for key, value in result.items():
|
| 46 |
key_str = key.replace("openfda.", "").replace("_", " ").capitalize()
|
| 47 |
val_str = ", ".join(value) if isinstance(value, list) else str(value)
|
| 48 |
-
readable += f"- {key_str}
|
| 49 |
-
readable += "\n"
|
| 50 |
formatted = readable.strip()
|
| 51 |
elif isinstance(content, (dict, list)):
|
| 52 |
formatted = json.dumps(content, indent=2)
|
| 53 |
else:
|
| 54 |
formatted = str(content)
|
| 55 |
|
| 56 |
-
title = f"{tool_name
|
| 57 |
-
|
| 58 |
return (
|
| 59 |
"<details style='border: 1px solid #aaa; border-radius: 8px; padding: 10px; margin: 12px 0; background-color: #f8f8f8;'>"
|
| 60 |
f"<summary style='font-weight: bold; font-size: 16px; color: #333;'>{title}</summary>"
|
| 61 |
-
f"<
|
| 62 |
"</details>"
|
| 63 |
)
|
| 64 |
|
| 65 |
-
# UI setup
|
| 66 |
def create_ui(agent):
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
color: #ffffff;
|
| 72 |
-
}
|
| 73 |
-
.gradio-container {
|
| 74 |
-
max-width: 900px;
|
| 75 |
-
margin: auto;
|
| 76 |
-
}
|
| 77 |
-
textarea, input, .gr-button {
|
| 78 |
-
font-size: 16px;
|
| 79 |
-
}
|
| 80 |
-
.gr-button {
|
| 81 |
-
background: linear-gradient(to right, #37B6E9, #4B4CED);
|
| 82 |
-
color: white;
|
| 83 |
-
border-radius: 8px;
|
| 84 |
-
font-weight: bold;
|
| 85 |
-
}
|
| 86 |
-
.gr-button:hover {
|
| 87 |
-
background: linear-gradient(to right, #4B4CED, #37B6E9);
|
| 88 |
-
}
|
| 89 |
-
.gr-chatbot {
|
| 90 |
-
background-color: #1e1e1e;
|
| 91 |
-
border-radius: 10px;
|
| 92 |
-
}
|
| 93 |
-
"""
|
| 94 |
-
|
| 95 |
-
with gr.Blocks(css=custom_css) as demo:
|
| 96 |
-
gr.Markdown("<h1 style='text-align: center; color: #4B4CED;'>TxAgent: Therapeutic Reasoning</h1>")
|
| 97 |
-
gr.Markdown("Ask biomedical or therapeutic questions. Powered by step-by-step reasoning and intelligent tool use.")
|
| 98 |
-
|
| 99 |
-
temperature = gr.Slider(0, 1, value=0.3, label="Temperature")
|
| 100 |
-
max_new_tokens = gr.Slider(128, 4096, value=1024, label="Max New Tokens")
|
| 101 |
-
max_tokens = gr.Slider(128, 32000, value=8192, label="Max Total Tokens")
|
| 102 |
-
max_round = gr.Slider(1, 50, value=30, label="Max Rounds")
|
| 103 |
-
multi_agent = gr.Checkbox(label="Enable Multi-agent Reasoning", value=False)
|
| 104 |
conversation_state = gr.State([])
|
| 105 |
|
| 106 |
chatbot = gr.Chatbot(label="TxAgent", height=600, type="messages")
|
| 107 |
message_input = gr.Textbox(placeholder="Ask your biomedical question...", show_label=False)
|
| 108 |
-
send_button = gr.Button("Send")
|
| 109 |
|
| 110 |
-
#
|
| 111 |
-
def handle_chat(message, history,
|
| 112 |
generator = agent.run_gradio_chat(
|
| 113 |
message=message,
|
| 114 |
history=history,
|
| 115 |
-
temperature=
|
| 116 |
-
max_new_tokens=
|
| 117 |
-
max_token=
|
| 118 |
-
call_agent=
|
| 119 |
conversation=conversation,
|
| 120 |
-
max_round=
|
| 121 |
)
|
| 122 |
|
| 123 |
for update in generator:
|
|
@@ -125,7 +98,7 @@ def create_ui(agent):
|
|
| 125 |
for m in update:
|
| 126 |
role = m["role"] if isinstance(m, dict) else getattr(m, "role", "assistant")
|
| 127 |
content = m["content"] if isinstance(m, dict) else getattr(m, "content", "")
|
| 128 |
-
tool_name = m.get("
|
| 129 |
|
| 130 |
if role == "assistant":
|
| 131 |
content = format_collapsible(content, tool_name)
|
|
@@ -133,17 +106,16 @@ def create_ui(agent):
|
|
| 133 |
formatted.append({"role": role, "content": content})
|
| 134 |
yield formatted
|
| 135 |
|
| 136 |
-
|
| 137 |
-
inputs = [message_input, chatbot, temperature, max_new_tokens, max_tokens, multi_agent, conversation_state, max_round]
|
| 138 |
send_button.click(fn=handle_chat, inputs=inputs, outputs=chatbot)
|
| 139 |
message_input.submit(fn=handle_chat, inputs=inputs, outputs=chatbot)
|
| 140 |
|
| 141 |
gr.Examples(examples=question_examples, inputs=message_input)
|
| 142 |
-
gr.Markdown("<
|
| 143 |
|
| 144 |
return demo
|
| 145 |
|
| 146 |
-
#
|
| 147 |
if __name__ == "__main__":
|
| 148 |
freeze_support()
|
| 149 |
try:
|
|
@@ -166,5 +138,5 @@ if __name__ == "__main__":
|
|
| 166 |
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=True, show_error=True)
|
| 167 |
|
| 168 |
except Exception as e:
|
| 169 |
-
print(f"
|
| 170 |
-
raise
|
|
|
|
| 36 |
["What treatment options exist for HER2+ breast cancer resistant to trastuzumab?"]
|
| 37 |
]
|
| 38 |
|
| 39 |
+
# Helper: format assistant responses in collapsible panels
|
| 40 |
+
|
| 41 |
def format_collapsible(content, tool_name=None):
|
| 42 |
+
# Try parsing if it's a JSON string
|
| 43 |
+
if isinstance(content, str):
|
| 44 |
+
try:
|
| 45 |
+
content = json.loads(content)
|
| 46 |
+
except Exception:
|
| 47 |
+
pass
|
| 48 |
+
|
| 49 |
if isinstance(content, dict) and "results" in content:
|
| 50 |
readable = ""
|
| 51 |
for i, result in enumerate(content["results"], 1):
|
| 52 |
+
readable += f"\n🔹 **Result {i}:**\n"
|
| 53 |
for key, value in result.items():
|
| 54 |
key_str = key.replace("openfda.", "").replace("_", " ").capitalize()
|
| 55 |
val_str = ", ".join(value) if isinstance(value, list) else str(value)
|
| 56 |
+
readable += f"- **{key_str}**: {val_str}\n"
|
|
|
|
| 57 |
formatted = readable.strip()
|
| 58 |
elif isinstance(content, (dict, list)):
|
| 59 |
formatted = json.dumps(content, indent=2)
|
| 60 |
else:
|
| 61 |
formatted = str(content)
|
| 62 |
|
| 63 |
+
title = f"{tool_name or 'Answer'}"
|
|
|
|
| 64 |
return (
|
| 65 |
"<details style='border: 1px solid #aaa; border-radius: 8px; padding: 10px; margin: 12px 0; background-color: #f8f8f8;'>"
|
| 66 |
f"<summary style='font-weight: bold; font-size: 16px; color: #333;'>{title}</summary>"
|
| 67 |
+
f"<div style='white-space: pre-wrap; font-family: sans-serif; color: #222; padding-top: 6px;'>{formatted}</div>"
|
| 68 |
"</details>"
|
| 69 |
)
|
| 70 |
|
| 71 |
+
# === UI setup
|
| 72 |
def create_ui(agent):
|
| 73 |
+
with gr.Blocks(css="body { background-color: #f5f5f5; font-family: sans-serif; }") as demo:
|
| 74 |
+
gr.Markdown("<h1 style='text-align: center;'>TxAgent: Therapeutic Reasoning</h1>")
|
| 75 |
+
gr.Markdown("<p style='text-align: center;'>Ask biomedical or therapeutic questions. Powered by step-by-step reasoning and tools.</p>")
|
| 76 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
conversation_state = gr.State([])
|
| 78 |
|
| 79 |
chatbot = gr.Chatbot(label="TxAgent", height=600, type="messages")
|
| 80 |
message_input = gr.Textbox(placeholder="Ask your biomedical question...", show_label=False)
|
| 81 |
+
send_button = gr.Button("Send", variant="primary")
|
| 82 |
|
| 83 |
+
# Main handler
|
| 84 |
+
def handle_chat(message, history, conversation):
|
| 85 |
generator = agent.run_gradio_chat(
|
| 86 |
message=message,
|
| 87 |
history=history,
|
| 88 |
+
temperature=0.3,
|
| 89 |
+
max_new_tokens=1024,
|
| 90 |
+
max_token=8192,
|
| 91 |
+
call_agent=False,
|
| 92 |
conversation=conversation,
|
| 93 |
+
max_round=30
|
| 94 |
)
|
| 95 |
|
| 96 |
for update in generator:
|
|
|
|
| 98 |
for m in update:
|
| 99 |
role = m["role"] if isinstance(m, dict) else getattr(m, "role", "assistant")
|
| 100 |
content = m["content"] if isinstance(m, dict) else getattr(m, "content", "")
|
| 101 |
+
tool_name = m.get("tool_name") if isinstance(m, dict) else getattr(m, "tool_name", None)
|
| 102 |
|
| 103 |
if role == "assistant":
|
| 104 |
content = format_collapsible(content, tool_name)
|
|
|
|
| 106 |
formatted.append({"role": role, "content": content})
|
| 107 |
yield formatted
|
| 108 |
|
| 109 |
+
inputs = [message_input, chatbot, conversation_state]
|
|
|
|
| 110 |
send_button.click(fn=handle_chat, inputs=inputs, outputs=chatbot)
|
| 111 |
message_input.submit(fn=handle_chat, inputs=inputs, outputs=chatbot)
|
| 112 |
|
| 113 |
gr.Examples(examples=question_examples, inputs=message_input)
|
| 114 |
+
gr.Markdown("<p style='font-size: 12px; text-align: center; color: gray;'>This demo is for research purposes only and does not provide medical advice.</p>")
|
| 115 |
|
| 116 |
return demo
|
| 117 |
|
| 118 |
+
# === Entry point
|
| 119 |
if __name__ == "__main__":
|
| 120 |
freeze_support()
|
| 121 |
try:
|
|
|
|
| 138 |
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=True, show_error=True)
|
| 139 |
|
| 140 |
except Exception as e:
|
| 141 |
+
print(f"\u274c App failed to start: {e}")
|
| 142 |
+
raise
|