Ali2206 commited on
Commit
88317c7
·
verified ·
1 Parent(s): 2880588

Update ui/ui_core.py

Browse files
Files changed (1) hide show
  1. ui/ui_core.py +19 -44
ui/ui_core.py CHANGED
@@ -1,42 +1,20 @@
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,
@@ -45,24 +23,21 @@ def create_ui(agent):
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
 
1
  # ui/ui_core.py
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
+ import gradio as gr
 
 
 
 
 
4
 
5
  def create_ui(agent):
6
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
7
  gr.Markdown("<h1 style='text-align: center;'>💊 TxAgent: Therapeutic Reasoning</h1>")
8
  chatbot = gr.Chatbot(label="TxAgent", height=600, type="messages")
9
+
10
+ file_upload = gr.File(label="Upload Medical File", file_types=[".pdf", ".txt", ".docx", ".jpg", ".png"])
11
  message_input = gr.Textbox(placeholder="Ask a biomedical question...", show_label=False)
12
  send_button = gr.Button("Send", variant="primary")
13
  conversation_state = gr.State([])
14
+ file_state = gr.State(None)
15
 
16
+ def handle_chat(message, history, conversation, uploaded_file):
17
+ # Use the file with the message
18
  generator = agent.run_gradio_chat(
19
  message=message,
20
  history=history,
 
23
  max_token=8192,
24
  call_agent=False,
25
  conversation=conversation,
26
+ uploaded_file=uploaded_file,
27
  max_round=30
28
  )
29
  for update in generator:
30
+ yield update
31
+
32
+ inputs = [message_input, chatbot, conversation_state, file_upload]
33
+ send_button.click(fn=handle_chat, inputs=inputs, outputs=chatbot)
34
+ message_input.submit(fn=handle_chat, inputs=inputs, outputs=chatbot)
35
+
36
+ gr.Examples(
37
+ examples=[
38
+ ["Upload the files"],
39
+ ],
40
+ inputs=message_input,
41
+ )
 
 
 
 
42
 
43
  return demo