|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: MIT-0 |
| 3 | + |
| 4 | +# Adapted from https://github.com/kyopark2014/strands-agent |
| 5 | +# SPDX-License-Identifier: MIT |
| 6 | + |
| 7 | +import logging |
| 8 | +import sys |
| 9 | + |
| 10 | +import streamlit as st |
| 11 | + |
| 12 | +import health_agent_async |
| 13 | + |
| 14 | +logging.basicConfig( |
| 15 | + level=logging.INFO, # Default to INFO level |
| 16 | + format="%(filename)s:%(lineno)d | %(message)s", |
| 17 | + handlers=[logging.StreamHandler(sys.stderr)], |
| 18 | +) |
| 19 | +logger = logging.getLogger("streamlit") |
| 20 | + |
| 21 | +# title |
| 22 | +st.set_page_config( |
| 23 | + page_title="Clinical Decision Support AI Assistant", |
| 24 | + page_icon="🏥", |
| 25 | + layout="centered", |
| 26 | + initial_sidebar_state="auto", |
| 27 | + menu_items=None, |
| 28 | +) |
| 29 | + |
| 30 | +with st.sidebar: |
| 31 | + st.title("Clinical Assistant Menu") |
| 32 | + |
| 33 | + st.markdown( |
| 34 | + "**Clinical Decision Support AI Assistant** with access to patient data through MCP healthcare server. " |
| 35 | + "This assistant helps healthcare professionals with diagnostic reasoning and clinical decision-making. " |
| 36 | + ) |
| 37 | + |
| 38 | + |
| 39 | + # Healthcare-specific information |
| 40 | + st.markdown("### Available Patient Data") |
| 41 | + st.markdown(""" |
| 42 | + **Sample Patient IDs:** |
| 43 | + - PAT001, PAT002, PAT003 |
| 44 | + |
| 45 | + **Available Tools:** |
| 46 | + - Patient Demographics |
| 47 | + - Medical History |
| 48 | + - Lab Results |
| 49 | + - Patient Search |
| 50 | + - Risk Assessment |
| 51 | + """) |
| 52 | + |
| 53 | + st.markdown("---") |
| 54 | + |
| 55 | + st.markdown("### ⚠️ Important Notice") |
| 56 | + st.markdown(""" |
| 57 | + This is a **clinical decision support tool** for healthcare professionals only. |
| 58 | + |
| 59 | + - Not a replacement for clinical judgment |
| 60 | + - Final decisions must be made by qualified healthcare professionals |
| 61 | + - Maintains HIPAA compliance principles |
| 62 | + """) |
| 63 | + |
| 64 | + clear_button = st.button("Reset Conversation", key="clear") |
| 65 | + |
| 66 | +st.title("🏥 Clinical Decision Support AI Assistant") |
| 67 | + |
| 68 | +# Initialize chat history |
| 69 | +if "messages" not in st.session_state: |
| 70 | + st.session_state.messages = [] |
| 71 | + st.session_state.greetings = False |
| 72 | + |
| 73 | +# Display chat messages from history on app rerun |
| 74 | +def display_chat_messages(): |
| 75 | + """Print message history |
| 76 | + @returns None |
| 77 | + """ |
| 78 | + for message in st.session_state.messages: |
| 79 | + with st.chat_message(message["role"]): |
| 80 | + if "images" in message: |
| 81 | + for url in message["images"]: |
| 82 | + logger.info(f"url: {url}") |
| 83 | + file_name = url[url.rfind("/") + 1 :] |
| 84 | + st.image(url, caption=file_name, use_container_width=True) |
| 85 | + st.markdown(message["content"]) |
| 86 | + |
| 87 | +display_chat_messages() |
| 88 | + |
| 89 | +# Greet user |
| 90 | +if not st.session_state.greetings: |
| 91 | + with st.chat_message("assistant"): |
| 92 | + intro = """👋 Welcome to the Clinical Decision Support AI Assistant! |
| 93 | +
|
| 94 | +I'm here to help healthcare professionals with: |
| 95 | +- **Diagnostic reasoning** and differential diagnoses |
| 96 | +- **Patient data analysis** using our MCP healthcare server |
| 97 | +- **Clinical insights** based on medical guidelines |
| 98 | +- **Treatment recommendations** considering patient history |
| 99 | +- **Lab result interpretation** and clinical findings |
| 100 | +
|
| 101 | +I have access to patient data for sample patients (PAT001, PAT002, PAT003) and can help with clinical decision-making. |
| 102 | +
|
| 103 | +**Important:** I'm a support tool for healthcare professionals, not a replacement for clinical judgment. All recommendations are for clinical decision support only. |
| 104 | +
|
| 105 | +How can I assist you with your clinical analysis today?""" |
| 106 | + |
| 107 | + st.markdown(intro) |
| 108 | + # Add assistant response to chat history |
| 109 | + st.session_state.messages.append({"role": "assistant", "content": intro}) |
| 110 | + st.session_state.greetings = True |
| 111 | + |
| 112 | +if clear_button or "messages" not in st.session_state: |
| 113 | + st.session_state.messages = [] |
| 114 | + st.session_state.greetings = False |
| 115 | + st.rerun() |
| 116 | + |
| 117 | +# Always show the chat input |
| 118 | +if prompt := st.chat_input("Enter your clinical question or patient case..."): |
| 119 | + with st.chat_message("user"): # display user message in chat message container |
| 120 | + st.markdown(prompt) |
| 121 | + |
| 122 | + st.session_state.messages.append( |
| 123 | + {"role": "user", "content": prompt} |
| 124 | + ) # add user message to chat history |
| 125 | + prompt = prompt.replace('"', "").replace("'", "") |
| 126 | + logger.info(f"Clinical prompt: {prompt}") |
| 127 | + |
| 128 | + with st.chat_message("assistant"): |
| 129 | + response = health_agent_async.run_health_agent(prompt, st) |
| 130 | + |
| 131 | + st.session_state.messages.append({"role": "assistant", "content": response}) |
0 commit comments