Spaces:
Running
Running
File size: 9,953 Bytes
0e0299f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
#!/usr/bin/env python
"""Streamlit app for the Drug Interaction System."""
import os
import sys
import streamlit as st
import matplotlib.pyplot as plt
import io
import base64
import networkx as nx
import uuid
# Add the current directory to the Python path
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
# Import the necessary components from your package
from src.models.chatbot import DrugInteractionChatbot
# Initialize the chatbot
@st.cache_resource
def get_chatbot():
"""Get or create the chatbot instance with caching."""
return DrugInteractionChatbot()
# Set page config
st.set_page_config(
page_title="Drug Interaction Assistant",
page_icon="💊",
layout="wide",
initial_sidebar_state="expanded"
)
# Title and description
st.title("Drug Interaction Assistant")
st.markdown("""
This application helps you analyze drug interactions, get information about medications,
and visualize drug interaction networks. Powered by biomedical language models.
""")
# Initialize session state for chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Sidebar with information
with st.sidebar:
st.header("About")
st.markdown("""
This Drug Interaction Assistant can:
- Analyze potential interactions between medications
- Provide detailed information about specific drugs
- Analyze clinical notes for drug mentions and interactions
- Generate visualizations of drug interaction networks
""")
st.header("Example Questions")
st.markdown("""
- "Can I take aspirin and warfarin together?"
- "Tell me about metformin"
- "Analyze this clinical note: Patient is taking..."
- "Show me a visualization for warfarin"
""")
# Main content area
col1, col2 = st.columns([2, 1])
with col1:
# Chat interface
st.header("Chat with the Assistant")
# Display chat history
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Chat input
if prompt := st.chat_input("Ask about drug interactions..."):
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# Display user message
with st.chat_message("user"):
st.markdown(prompt)
# Get chatbot response
chatbot = get_chatbot()
response = chatbot.process_message(prompt)
# Check if we need to generate a visualization
visualization_needed = False
drug_name = None
if "interaction found between" in response:
# Extract drug name from response
import re
match = re.search(r'interaction found between (.+?) and', response)
if match:
drug_name = match.group(1)
visualization_needed = True
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": response})
# Display assistant response
with st.chat_message("assistant"):
st.markdown(response)
# Generate and display visualization if needed
if visualization_needed and drug_name:
try:
G, error = chatbot.processor.generate_network(drug_name)
if not error:
# Create visualization
plt.figure(figsize=(10, 8))
# Get positions
pos = nx.spring_layout(G, seed=42)
# Draw nodes
node_sizes = [G.nodes[node].get('size', 10) for node in G.nodes()]
node_colors = [G.nodes[node].get('color', 'blue') for node in G.nodes()]
nx.draw_networkx_nodes(G, pos, node_size=node_sizes, node_color=node_colors, alpha=0.8)
# Draw edges with colors based on severity
edge_colors = []
edge_widths = []
for u, v, data in G.edges(data=True):
edge_colors.append(data.get('color', 'gray'))
edge_widths.append(data.get('weight', 1))
nx.draw_networkx_edges(G, pos, edge_color=edge_colors, width=edge_widths, alpha=0.7)
# Add labels
nx.draw_networkx_labels(G, pos, font_size=10, font_family="sans-serif")
# Save to BytesIO
buf = io.BytesIO()
plt.axis('off')
plt.tight_layout()
plt.savefig(buf, format='png', dpi=150)
buf.seek(0)
plt.close()
# Convert to base64 for display
img_str = base64.b64encode(buf.read()).decode('utf-8')
st.image(f"data:image/png;base64,{img_str}", caption=f"Interaction Network for {drug_name}")
except Exception as e:
st.error(f"Error generating visualization: {str(e)}")
with col2:
# Drug information section
st.header("Drug Information")
# Drug search
drug_search = st.text_input("Search for a drug", key="drug_search")
if drug_search:
chatbot = get_chatbot()
drug_info = chatbot.processor.get_drug_information(drug_search)
if drug_info:
st.subheader(drug_info.get("drug_name", drug_search))
if drug_info.get("drug_class") and drug_info["drug_class"] != "Information not available":
st.markdown(f"**Drug Class:** {drug_info['drug_class']}")
if drug_info.get("mechanism") and drug_info["mechanism"] != "Information not available":
st.markdown(f"**Mechanism of Action:** {drug_info['mechanism']}")
if drug_info.get("indications") and drug_info["indications"][0] != "Information not available":
st.markdown("**Common Indications:**")
for indication in drug_info["indications"]:
st.markdown(f"- {indication}")
if drug_info.get("side_effects") and drug_info["side_effects"][0] != "Information not available":
st.markdown("**Common Side Effects:**")
for effect in drug_info["side_effects"]:
st.markdown(f"- {effect}")
if drug_info.get("common_interactions") and drug_info["common_interactions"][0] != "Information not available":
st.markdown("**Common Interactions:**")
for interaction in drug_info["common_interactions"]:
st.markdown(f"- {interaction}")
if drug_info.get("contraindications") and drug_info["contraindications"][0] != "Information not available":
st.markdown("**Contraindications:**")
for contraindication in drug_info["contraindications"]:
st.markdown(f"- {contraindication}")
else:
st.warning(f"No information found for {drug_search}")
# Clinical note analysis section
st.header("Clinical Note Analysis")
clinical_note = st.text_area("Enter clinical note to analyze", height=150)
if clinical_note and st.button("Analyze Note"):
chatbot = get_chatbot()
results = chatbot.processor.extract_drugs_from_clinical_notes(clinical_note)
# Display medications
if results["medications"]:
st.subheader("Medications Identified")
for med in results["medications"]:
name = med.get("name", "Unknown")
dosage = med.get("dosage", "Not specified")
frequency = med.get("frequency", "Not specified")
if dosage != "Not specified" or frequency != "Not specified":
st.markdown(f"- **{name}**: {dosage} {frequency}")
else:
st.markdown(f"- **{name}**")
else:
st.info("No medications were identified in the clinical notes.")
# Display potential interactions
if results.get("potential_interactions"):
st.subheader("Potential Interactions")
for interaction in results["potential_interactions"]:
drug1 = interaction.get("drug1", "Unknown")
drug2 = interaction.get("drug2", "Unknown")
concern = interaction.get("concern", "Potential interaction")
st.markdown(f"- **{drug1}** + **{drug2}**: {concern}")
elif results.get("database_interactions"):
st.subheader("Potential Interactions")
for interaction in results["database_interactions"]:
drug1 = interaction.get("drug1", "Unknown")
drug2 = interaction.get("drug2", "Unknown")
desc = interaction.get("description", "Potential interaction")
severity = interaction.get("severity", "Unknown")
st.markdown(f"- **{drug1}** + **{drug2}**: {desc} ({severity})")
else:
st.info("No potential interactions were identified.")
# Footer
st.markdown("---")
st.markdown("""
<div style='text-align: center'>
<p>Drug Interaction Assistant | Powered by Biomedical Language Models</p>
<p><small>This information is for educational purposes only. Always consult a healthcare professional for medical advice.</small></p>
</div>
""", unsafe_allow_html=True) |