Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,6 @@ import gradio as gr
|
|
| 5 |
from multiprocessing import freeze_support
|
| 6 |
import importlib
|
| 7 |
import inspect
|
| 8 |
-
import json
|
| 9 |
|
| 10 |
# === Fix path to include src/txagent
|
| 11 |
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src"))
|
|
@@ -37,6 +36,14 @@ question_examples = [
|
|
| 37 |
["What treatment options exist for HER2+ breast cancer resistant to trastuzumab?"]
|
| 38 |
]
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# === UI creation
|
| 41 |
def create_ui(agent):
|
| 42 |
with gr.Blocks() as demo:
|
|
@@ -69,26 +76,12 @@ def create_ui(agent):
|
|
| 69 |
|
| 70 |
for update in generator:
|
| 71 |
formatted_messages = []
|
| 72 |
-
|
| 73 |
for m in update:
|
| 74 |
-
role = m
|
| 75 |
-
content = m
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
if "[TOOL_CALLS]" in content:
|
| 79 |
-
try:
|
| 80 |
-
thought, tools = content.split("[TOOL_CALLS]", 1)
|
| 81 |
-
tools_json = json.loads(tools.strip())
|
| 82 |
-
pretty_tools = json.dumps(tools_json, indent=2)
|
| 83 |
-
|
| 84 |
-
# Style it like a ChatGPT output
|
| 85 |
-
content = f"**🧠 Assistant reasoning:**\n\n{thought.strip()}\n\n" \
|
| 86 |
-
f"**🔧 Tool Calls:**\n\n```json\n{pretty_tools}\n```"
|
| 87 |
-
except Exception as e:
|
| 88 |
-
content = f"{content}\n\n⚠️ Failed to format tool calls: {e}"
|
| 89 |
-
|
| 90 |
formatted_messages.append({"role": role, "content": content})
|
| 91 |
-
|
| 92 |
yield formatted_messages
|
| 93 |
|
| 94 |
# === Trigger handlers
|
|
@@ -123,7 +116,7 @@ if __name__ == "__main__":
|
|
| 123 |
enable_checker=True,
|
| 124 |
step_rag_num=10,
|
| 125 |
seed=100,
|
| 126 |
-
additional_default_tools=[
|
| 127 |
)
|
| 128 |
agent.init_model()
|
| 129 |
|
|
|
|
| 5 |
from multiprocessing import freeze_support
|
| 6 |
import importlib
|
| 7 |
import inspect
|
|
|
|
| 8 |
|
| 9 |
# === Fix path to include src/txagent
|
| 10 |
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src"))
|
|
|
|
| 36 |
["What treatment options exist for HER2+ breast cancer resistant to trastuzumab?"]
|
| 37 |
]
|
| 38 |
|
| 39 |
+
# === Helper: Add collapsible formatting
|
| 40 |
+
def format_collapsible_response(content):
|
| 41 |
+
return (
|
| 42 |
+
f"<details style='border: 1px solid #ccc; padding: 8px; margin-top: 8px;'>"
|
| 43 |
+
f"<summary style='font-weight: bold;'>Answer</summary>"
|
| 44 |
+
f"<div style='margin-top: 8px;'>{content}</div></details>"
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
# === UI creation
|
| 48 |
def create_ui(agent):
|
| 49 |
with gr.Blocks() as demo:
|
|
|
|
| 76 |
|
| 77 |
for update in generator:
|
| 78 |
formatted_messages = []
|
|
|
|
| 79 |
for m in update:
|
| 80 |
+
role = m["role"] if isinstance(m, dict) else getattr(m, "role", "assistant")
|
| 81 |
+
content = m["content"] if isinstance(m, dict) else getattr(m, "content", "")
|
| 82 |
+
if role == "assistant":
|
| 83 |
+
content = format_collapsible_response(content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
formatted_messages.append({"role": role, "content": content})
|
|
|
|
| 85 |
yield formatted_messages
|
| 86 |
|
| 87 |
# === Trigger handlers
|
|
|
|
| 116 |
enable_checker=True,
|
| 117 |
step_rag_num=10,
|
| 118 |
seed=100,
|
| 119 |
+
additional_default_tools=[] # Removed DirectResponse/RequireClarification to avoid errors
|
| 120 |
)
|
| 121 |
agent.init_model()
|
| 122 |
|