Update app.py
Browse files
app.py
CHANGED
@@ -36,8 +36,8 @@ question_examples = [
|
|
36 |
["What treatment options exist for HER2+ breast cancer resistant to trastuzumab?"]
|
37 |
]
|
38 |
|
39 |
-
#
|
40 |
-
def format_collapsible(content):
|
41 |
if isinstance(content, (dict, list)):
|
42 |
try:
|
43 |
formatted = json.dumps(content, indent=2)
|
@@ -46,39 +46,61 @@ def format_collapsible(content):
|
|
46 |
else:
|
47 |
formatted = str(content)
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
55 |
|
56 |
# UI setup
|
57 |
def create_ui(agent):
|
58 |
-
|
59 |
-
body {
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
conversation_state = gr.State([])
|
|
|
77 |
chatbot = gr.Chatbot(label="TxAgent", height=600, type="messages")
|
78 |
message_input = gr.Textbox(placeholder="Ask your biomedical question...", show_label=False)
|
79 |
-
send_button = gr.Button("Send"
|
80 |
|
81 |
-
#
|
82 |
def handle_chat(message, history, temperature, max_new_tokens, max_tokens, multi_agent, conversation, max_round):
|
83 |
generator = agent.run_gradio_chat(
|
84 |
message=message,
|
@@ -90,26 +112,31 @@ def create_ui(agent):
|
|
90 |
conversation=conversation,
|
91 |
max_round=max_round
|
92 |
)
|
|
|
93 |
for update in generator:
|
94 |
formatted = []
|
95 |
for m in update:
|
96 |
role = m["role"] if isinstance(m, dict) else getattr(m, "role", "assistant")
|
97 |
content = m["content"] if isinstance(m, dict) else getattr(m, "content", "")
|
|
|
|
|
98 |
if role == "assistant":
|
99 |
-
content = format_collapsible(content)
|
|
|
100 |
formatted.append({"role": role, "content": content})
|
101 |
yield formatted
|
102 |
|
|
|
103 |
inputs = [message_input, chatbot, temperature, max_new_tokens, max_tokens, multi_agent, conversation_state, max_round]
|
104 |
send_button.click(fn=handle_chat, inputs=inputs, outputs=chatbot)
|
105 |
message_input.submit(fn=handle_chat, inputs=inputs, outputs=chatbot)
|
106 |
|
107 |
gr.Examples(examples=question_examples, inputs=message_input)
|
108 |
-
gr.Markdown("<
|
109 |
|
110 |
return demo
|
111 |
|
112 |
-
#
|
113 |
if __name__ == "__main__":
|
114 |
freeze_support()
|
115 |
try:
|
@@ -129,13 +156,8 @@ if __name__ == "__main__":
|
|
129 |
raise AttributeError("TxAgent missing run_gradio_chat")
|
130 |
|
131 |
demo = create_ui(agent)
|
132 |
-
demo.queue().launch(
|
133 |
-
server_name="0.0.0.0",
|
134 |
-
server_port=7860,
|
135 |
-
share=True,
|
136 |
-
show_error=True
|
137 |
-
)
|
138 |
|
139 |
except Exception as e:
|
140 |
-
print(f"
|
141 |
raise
|
|
|
36 |
["What treatment options exist for HER2+ breast cancer resistant to trastuzumab?"]
|
37 |
]
|
38 |
|
39 |
+
# Helper: collapsible format with tool name
|
40 |
+
def format_collapsible(content, tool_name=None):
|
41 |
if isinstance(content, (dict, list)):
|
42 |
try:
|
43 |
formatted = json.dumps(content, indent=2)
|
|
|
46 |
else:
|
47 |
formatted = str(content)
|
48 |
|
49 |
+
title = f"Answer from: {tool_name}" if tool_name else "Answer"
|
50 |
+
|
51 |
+
return f"""
|
52 |
+
<details style='border: 1px solid #444; background-color: #1e1e1e; border-radius: 8px; padding: 10px; margin: 10px 0;'>
|
53 |
+
<summary style='color: #37B6E9; font-weight: 600; font-size: 16px;'>{title}</summary>
|
54 |
+
<pre style='color: #eee; font-family: Consolas, monospace; padding-top: 10px;'>{formatted}</pre>
|
55 |
+
</details>
|
56 |
+
"""
|
57 |
|
58 |
# UI setup
|
59 |
def create_ui(agent):
|
60 |
+
custom_css = """
|
61 |
+
body {
|
62 |
+
font-family: Inter, sans-serif;
|
63 |
+
background-color: #121212;
|
64 |
+
color: #ffffff;
|
65 |
+
}
|
66 |
+
.gradio-container {
|
67 |
+
max-width: 900px;
|
68 |
+
margin: auto;
|
69 |
+
}
|
70 |
+
textarea, input, .gr-button {
|
71 |
+
font-size: 16px;
|
72 |
+
}
|
73 |
+
.gr-button {
|
74 |
+
background: linear-gradient(to right, #37B6E9, #4B4CED);
|
75 |
+
color: white;
|
76 |
+
border-radius: 8px;
|
77 |
+
font-weight: bold;
|
78 |
+
}
|
79 |
+
.gr-button:hover {
|
80 |
+
background: linear-gradient(to right, #4B4CED, #37B6E9);
|
81 |
+
}
|
82 |
+
.gr-chatbot {
|
83 |
+
background-color: #1e1e1e;
|
84 |
+
border-radius: 10px;
|
85 |
+
}
|
86 |
+
"""
|
87 |
+
|
88 |
+
with gr.Blocks(css=custom_css) as demo:
|
89 |
+
gr.Markdown("<h1 style='text-align: center; color: #4B4CED;'>TxAgent: Therapeutic Reasoning</h1>")
|
90 |
+
gr.Markdown("Ask biomedical or therapeutic questions. Powered by step-by-step reasoning and intelligent tool use.")
|
91 |
+
|
92 |
+
temperature = gr.Slider(0, 1, value=0.3, label="Temperature")
|
93 |
+
max_new_tokens = gr.Slider(128, 4096, value=1024, label="Max New Tokens")
|
94 |
+
max_tokens = gr.Slider(128, 32000, value=8192, label="Max Total Tokens")
|
95 |
+
max_round = gr.Slider(1, 50, value=30, label="Max Rounds")
|
96 |
+
multi_agent = gr.Checkbox(label="Enable Multi-agent Reasoning", value=False)
|
97 |
conversation_state = gr.State([])
|
98 |
+
|
99 |
chatbot = gr.Chatbot(label="TxAgent", height=600, type="messages")
|
100 |
message_input = gr.Textbox(placeholder="Ask your biomedical question...", show_label=False)
|
101 |
+
send_button = gr.Button("Send")
|
102 |
|
103 |
+
# Chat logic
|
104 |
def handle_chat(message, history, temperature, max_new_tokens, max_tokens, multi_agent, conversation, max_round):
|
105 |
generator = agent.run_gradio_chat(
|
106 |
message=message,
|
|
|
112 |
conversation=conversation,
|
113 |
max_round=max_round
|
114 |
)
|
115 |
+
|
116 |
for update in generator:
|
117 |
formatted = []
|
118 |
for m in update:
|
119 |
role = m["role"] if isinstance(m, dict) else getattr(m, "role", "assistant")
|
120 |
content = m["content"] if isinstance(m, dict) else getattr(m, "content", "")
|
121 |
+
tool_name = m.get("metadata", {}).get("tool") if isinstance(m, dict) else getattr(m, "metadata", {}).get("tool", None)
|
122 |
+
|
123 |
if role == "assistant":
|
124 |
+
content = format_collapsible(content, tool_name)
|
125 |
+
|
126 |
formatted.append({"role": role, "content": content})
|
127 |
yield formatted
|
128 |
|
129 |
+
# Events
|
130 |
inputs = [message_input, chatbot, temperature, max_new_tokens, max_tokens, multi_agent, conversation_state, max_round]
|
131 |
send_button.click(fn=handle_chat, inputs=inputs, outputs=chatbot)
|
132 |
message_input.submit(fn=handle_chat, inputs=inputs, outputs=chatbot)
|
133 |
|
134 |
gr.Examples(examples=question_examples, inputs=message_input)
|
135 |
+
gr.Markdown("<div style='text-align: center; font-size: 0.9em; color: #999;'>This demo is for research purposes only and does not provide medical advice.</div>")
|
136 |
|
137 |
return demo
|
138 |
|
139 |
+
# Main
|
140 |
if __name__ == "__main__":
|
141 |
freeze_support()
|
142 |
try:
|
|
|
156 |
raise AttributeError("TxAgent missing run_gradio_chat")
|
157 |
|
158 |
demo = create_ui(agent)
|
159 |
+
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=True, show_error=True)
|
|
|
|
|
|
|
|
|
|
|
160 |
|
161 |
except Exception as e:
|
162 |
+
print(f"❌ App failed to start: {e}")
|
163 |
raise
|