Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ from langchain_groq import ChatGroq
|
|
| 3 |
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 4 |
from langchain_core.messages import HumanMessage, SystemMessage
|
| 5 |
from langchain_core.tools import tool
|
| 6 |
-
from typing import
|
| 7 |
import json
|
| 8 |
|
| 9 |
# Configuration
|
|
@@ -24,32 +24,15 @@ class MedicalConfig:
|
|
| 24 |
# Define tools with proper schemas
|
| 25 |
@tool
|
| 26 |
def order_lab_test(test_name: str, reason: str) -> str:
|
| 27 |
-
"""Orders a lab test with specified parameters.
|
| 28 |
-
|
| 29 |
-
Args:
|
| 30 |
-
test_name: Name of the lab test to order
|
| 31 |
-
reason: Clinical justification for the test
|
| 32 |
-
|
| 33 |
-
Returns:
|
| 34 |
-
Confirmation message with test details
|
| 35 |
-
"""
|
| 36 |
-
return f"Lab ordered: {test_name} ({reason})"
|
| 37 |
|
| 38 |
@tool
|
| 39 |
def prescribe_medication(name: str, dosage: str, frequency: str) -> str:
|
| 40 |
-
"""Prescribes medication with specific dosage instructions.
|
| 41 |
-
|
| 42 |
-
Args:
|
| 43 |
-
name: Name of the medication
|
| 44 |
-
dosage: Dosage amount (e.g., '500mg')
|
| 45 |
-
frequency: Administration frequency (e.g., 'every 6 hours')
|
| 46 |
-
|
| 47 |
-
Returns:
|
| 48 |
-
Confirmation message with prescription details
|
| 49 |
-
"""
|
| 50 |
-
return f"Prescribed: {name} {dosage} {frequency}"
|
| 51 |
|
| 52 |
-
# Initialize tools
|
| 53 |
tools = [order_lab_test, prescribe_medication, TavilySearchResults(max_results=3)]
|
| 54 |
|
| 55 |
class MedicalAgent:
|
|
@@ -82,4 +65,57 @@ class MedicalAgent:
|
|
| 82 |
except Exception as e:
|
| 83 |
return f"Error processing action: {str(e)}"
|
| 84 |
|
| 85 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 4 |
from langchain_core.messages import HumanMessage, SystemMessage
|
| 5 |
from langchain_core.tools import tool
|
| 6 |
+
from typing import Optional
|
| 7 |
import json
|
| 8 |
|
| 9 |
# Configuration
|
|
|
|
| 24 |
# Define tools with proper schemas
|
| 25 |
@tool
|
| 26 |
def order_lab_test(test_name: str, reason: str) -> str:
|
| 27 |
+
"""Orders a lab test with specified parameters."""
|
| 28 |
+
return f"✅ Lab ordered: {test_name} ({reason})"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
@tool
|
| 31 |
def prescribe_medication(name: str, dosage: str, frequency: str) -> str:
|
| 32 |
+
"""Prescribes medication with specific dosage instructions."""
|
| 33 |
+
return f"✅ Prescribed: {name} {dosage} {frequency}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
# Initialize tools and model
|
| 36 |
tools = [order_lab_test, prescribe_medication, TavilySearchResults(max_results=3)]
|
| 37 |
|
| 38 |
class MedicalAgent:
|
|
|
|
| 65 |
except Exception as e:
|
| 66 |
return f"Error processing action: {str(e)}"
|
| 67 |
|
| 68 |
+
# Streamlit UI
|
| 69 |
+
def main():
|
| 70 |
+
st.set_page_config(page_title="AI Clinic", layout="wide")
|
| 71 |
+
|
| 72 |
+
# Initialize agent
|
| 73 |
+
if 'agent' not in st.session_state:
|
| 74 |
+
st.session_state.agent = MedicalAgent()
|
| 75 |
+
|
| 76 |
+
# Patient intake form
|
| 77 |
+
with st.sidebar:
|
| 78 |
+
st.header("Patient Intake")
|
| 79 |
+
symptoms = st.multiselect("Symptoms", ["Fever", "Cough", "Chest Pain", "Shortness of Breath"])
|
| 80 |
+
medical_history = st.text_input("Medical History")
|
| 81 |
+
current_meds = st.text_input("Current Medications")
|
| 82 |
+
temp = st.number_input("Temperature (°C)", 35.0, 42.0, 37.0)
|
| 83 |
+
bp = st.text_input("Blood Pressure (mmHg)", "120/80")
|
| 84 |
+
|
| 85 |
+
patient_data = {
|
| 86 |
+
"symptoms": symptoms,
|
| 87 |
+
"history": {
|
| 88 |
+
"conditions": medical_history,
|
| 89 |
+
"medications": current_meds
|
| 90 |
+
},
|
| 91 |
+
"vitals": {
|
| 92 |
+
"temp": temp,
|
| 93 |
+
"bp": bp
|
| 94 |
+
}
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
# Main interface
|
| 98 |
+
st.title("AI-Powered Clinical Support System")
|
| 99 |
+
|
| 100 |
+
# Analysis button
|
| 101 |
+
if st.button("Start Analysis", type="primary"):
|
| 102 |
+
with st.spinner("Analyzing patient data..."):
|
| 103 |
+
response = st.session_state.agent.analyze_patient(patient_data)
|
| 104 |
+
|
| 105 |
+
if response:
|
| 106 |
+
st.subheader("Clinical Recommendations")
|
| 107 |
+
|
| 108 |
+
# Check for tool calls
|
| 109 |
+
if hasattr(response, 'tool_calls') and response.tool_calls:
|
| 110 |
+
for action in response.tool_calls:
|
| 111 |
+
result = st.session_state.agent.process_action({
|
| 112 |
+
"name": action['name'],
|
| 113 |
+
"args": action['args']
|
| 114 |
+
})
|
| 115 |
+
st.success(result)
|
| 116 |
+
else:
|
| 117 |
+
st.info("No specific actions recommended. Clinical Summary:")
|
| 118 |
+
st.write(response.content)
|
| 119 |
+
|
| 120 |
+
if __name__ == "__main__":
|
| 121 |
+
main()
|