Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,13 +8,14 @@ from docx import Document
|
|
| 8 |
from io import BytesIO
|
| 9 |
import base64
|
| 10 |
|
|
|
|
| 11 |
load_dotenv()
|
| 12 |
|
| 13 |
-
#
|
| 14 |
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
| 15 |
os.environ["SERPER_API_KEY"] = os.getenv("SERPER_API_KEY")
|
| 16 |
|
| 17 |
-
|
| 18 |
def generate_docx(result):
|
| 19 |
doc = Document()
|
| 20 |
doc.add_heading('Healthcare Diagnosis and Treatment Recommendations', 0)
|
|
@@ -26,23 +27,111 @@ def generate_docx(result):
|
|
| 26 |
|
| 27 |
def get_download_link(bio, filename):
|
| 28 |
b64 = base64.b64encode(bio.read()).decode()
|
| 29 |
-
return f'<a href="data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,{b64}" download="{filename}">Download
|
| 30 |
|
|
|
|
| 31 |
st.set_page_config(
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
)
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
st.
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
search_tool = SerperDevTool()
|
| 47 |
scrape_tool = ScrapeWebsiteTool()
|
| 48 |
|
|
@@ -56,7 +145,7 @@ llm = ChatOpenAI(
|
|
| 56 |
diagnostician = Agent(
|
| 57 |
role="Medical Diagnostician",
|
| 58 |
goal="Analyze patient symptoms and medical history to provide a preliminary diagnosis.",
|
| 59 |
-
backstory="
|
| 60 |
verbose=True,
|
| 61 |
allow_delegation=False,
|
| 62 |
tools=[search_tool, scrape_tool],
|
|
@@ -65,8 +154,8 @@ diagnostician = Agent(
|
|
| 65 |
|
| 66 |
treatment_advisor = Agent(
|
| 67 |
role="Treatment Advisor",
|
| 68 |
-
goal="Recommend appropriate treatment plans based on the diagnosis
|
| 69 |
-
backstory="
|
| 70 |
verbose=True,
|
| 71 |
allow_delegation=False,
|
| 72 |
tools=[search_tool, scrape_tool],
|
|
@@ -76,41 +165,70 @@ treatment_advisor = Agent(
|
|
| 76 |
# Define Tasks
|
| 77 |
diagnose_task = Task(
|
| 78 |
description=(
|
| 79 |
-
"1. Analyze the patient's symptoms ({symptoms}) and medical history ({medical_history}).\n"
|
| 80 |
-
"2.
|
| 81 |
-
"3.
|
|
|
|
| 82 |
),
|
| 83 |
-
expected_output="A preliminary diagnosis with
|
| 84 |
agent=diagnostician
|
| 85 |
)
|
| 86 |
|
| 87 |
treatment_task = Task(
|
| 88 |
description=(
|
| 89 |
-
"1. Based on the diagnosis,
|
| 90 |
-
"2. Consider
|
| 91 |
-
"3.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
),
|
| 93 |
-
expected_output="A comprehensive treatment plan
|
| 94 |
agent=treatment_advisor
|
| 95 |
)
|
| 96 |
|
| 97 |
# Create Crew
|
| 98 |
-
|
| 99 |
crew = Crew(
|
| 100 |
agents=[diagnostician, treatment_advisor],
|
| 101 |
tasks=[diagnose_task, treatment_task],
|
| 102 |
-
verbose=True
|
| 103 |
)
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
from io import BytesIO
|
| 9 |
import base64
|
| 10 |
|
| 11 |
+
# Load environment variables
|
| 12 |
load_dotenv()
|
| 13 |
|
| 14 |
+
# Configure API keys
|
| 15 |
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
| 16 |
os.environ["SERPER_API_KEY"] = os.getenv("SERPER_API_KEY")
|
| 17 |
|
| 18 |
+
# Helper Functions
|
| 19 |
def generate_docx(result):
|
| 20 |
doc = Document()
|
| 21 |
doc.add_heading('Healthcare Diagnosis and Treatment Recommendations', 0)
|
|
|
|
| 27 |
|
| 28 |
def get_download_link(bio, filename):
|
| 29 |
b64 = base64.b64encode(bio.read()).decode()
|
| 30 |
+
return f'<a href="data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,{b64}" download="{filename}" class="download-button">Download Report</a>'
|
| 31 |
|
| 32 |
+
# Page Configuration
|
| 33 |
st.set_page_config(
|
| 34 |
+
page_title="Medical AI Assistant",
|
| 35 |
+
page_icon="π₯",
|
| 36 |
+
layout="wide",
|
| 37 |
+
initial_sidebar_state="expanded"
|
| 38 |
)
|
| 39 |
|
| 40 |
+
# Custom CSS
|
| 41 |
+
st.markdown("""
|
| 42 |
+
<style>
|
| 43 |
+
.main {
|
| 44 |
+
padding: 2rem;
|
| 45 |
+
}
|
| 46 |
+
.stButton > button {
|
| 47 |
+
width: 100%;
|
| 48 |
+
background-color: #007bff;
|
| 49 |
+
color: white;
|
| 50 |
+
padding: 0.5rem 1rem;
|
| 51 |
+
border-radius: 0.5rem;
|
| 52 |
+
border: none;
|
| 53 |
+
margin-top: 1rem;
|
| 54 |
+
}
|
| 55 |
+
.stButton > button:hover {
|
| 56 |
+
background-color: #0056b3;
|
| 57 |
+
}
|
| 58 |
+
.download-button {
|
| 59 |
+
display: inline-block;
|
| 60 |
+
padding: 0.5rem 1rem;
|
| 61 |
+
background-color: #28a745;
|
| 62 |
+
color: white;
|
| 63 |
+
text-decoration: none;
|
| 64 |
+
border-radius: 0.5rem;
|
| 65 |
+
margin-top: 1rem;
|
| 66 |
+
}
|
| 67 |
+
.download-button:hover {
|
| 68 |
+
background-color: #218838;
|
| 69 |
+
color: white;
|
| 70 |
+
}
|
| 71 |
+
.stTextInput > div > div > input {
|
| 72 |
+
border-radius: 0.5rem;
|
| 73 |
+
}
|
| 74 |
+
.stTextArea > div > div > textarea {
|
| 75 |
+
border-radius: 0.5rem;
|
| 76 |
+
}
|
| 77 |
+
</style>
|
| 78 |
+
""", unsafe_allow_html=True)
|
| 79 |
+
|
| 80 |
+
# Sidebar for Patient Information
|
| 81 |
+
with st.sidebar:
|
| 82 |
+
st.image("https://img.icons8.com/color/96/000000/healthcare.png", width=100)
|
| 83 |
+
st.title("Patient Information")
|
| 84 |
+
|
| 85 |
+
with st.form("patient_info"):
|
| 86 |
+
gender = st.selectbox('Gender', ('Male', 'Female', 'Other'))
|
| 87 |
+
age = st.number_input('Age', min_value=0, max_value=120, value=25)
|
| 88 |
+
height = st.number_input('Height (cm)', min_value=0, max_value=300, value=170)
|
| 89 |
+
weight = st.number_input('Weight (kg)', min_value=0, max_value=500, value=70)
|
| 90 |
+
submit_button = st.form_submit_button("Save Patient Info")
|
| 91 |
+
|
| 92 |
+
# Main Content
|
| 93 |
+
st.title("π₯ Medical AI Assistant")
|
| 94 |
+
st.markdown("""
|
| 95 |
+
<div style='background-color: #f8f9fa; padding: 1rem; border-radius: 0.5rem; margin-bottom: 2rem;'>
|
| 96 |
+
<h4>Welcome to the Medical AI Assistant</h4>
|
| 97 |
+
<p>This AI-powered system helps medical professionals with diagnosis and treatment recommendations.
|
| 98 |
+
Please enter the patient's symptoms and medical history below.</p>
|
| 99 |
+
</div>
|
| 100 |
+
""", unsafe_allow_html=True)
|
| 101 |
+
|
| 102 |
+
# Create tabs for different sections
|
| 103 |
+
tab1, tab2 = st.tabs(["π Patient Assessment", "π Results"])
|
| 104 |
+
|
| 105 |
+
with tab1:
|
| 106 |
+
col1, col2 = st.columns(2)
|
| 107 |
+
|
| 108 |
+
with col1:
|
| 109 |
+
st.subheader("Current Symptoms")
|
| 110 |
+
symptoms = st.text_area(
|
| 111 |
+
'Describe the symptoms in detail',
|
| 112 |
+
placeholder='e.g., persistent fever for 3 days, dry cough, fatigue',
|
| 113 |
+
height=200
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
with col2:
|
| 117 |
+
st.subheader("Medical History")
|
| 118 |
+
medical_history = st.text_area(
|
| 119 |
+
'Enter relevant medical history',
|
| 120 |
+
placeholder='e.g., Type 2 diabetes diagnosed in 2019, hypertension',
|
| 121 |
+
height=200
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
# Additional Information
|
| 125 |
+
with st.expander("Additional Information (Optional)"):
|
| 126 |
+
col3, col4 = st.columns(2)
|
| 127 |
+
with col3:
|
| 128 |
+
allergies = st.text_area("Known Allergies", placeholder="e.g., penicillin, peanuts")
|
| 129 |
+
current_medications = st.text_area("Current Medications", placeholder="e.g., metformin 500mg twice daily")
|
| 130 |
+
with col4:
|
| 131 |
+
family_history = st.text_area("Family History", placeholder="e.g., heart disease, diabetes")
|
| 132 |
+
lifestyle = st.text_area("Lifestyle Factors", placeholder="e.g., smoker, exercises 3 times a week")
|
| 133 |
+
|
| 134 |
+
# Initialize Tools and Agents
|
| 135 |
search_tool = SerperDevTool()
|
| 136 |
scrape_tool = ScrapeWebsiteTool()
|
| 137 |
|
|
|
|
| 145 |
diagnostician = Agent(
|
| 146 |
role="Medical Diagnostician",
|
| 147 |
goal="Analyze patient symptoms and medical history to provide a preliminary diagnosis.",
|
| 148 |
+
backstory="Expert in diagnosing medical conditions using advanced algorithms and comprehensive medical knowledge.",
|
| 149 |
verbose=True,
|
| 150 |
allow_delegation=False,
|
| 151 |
tools=[search_tool, scrape_tool],
|
|
|
|
| 154 |
|
| 155 |
treatment_advisor = Agent(
|
| 156 |
role="Treatment Advisor",
|
| 157 |
+
goal="Recommend appropriate treatment plans based on the diagnosis.",
|
| 158 |
+
backstory="Specialist in creating personalized treatment plans considering patient history and current medical best practices.",
|
| 159 |
verbose=True,
|
| 160 |
allow_delegation=False,
|
| 161 |
tools=[search_tool, scrape_tool],
|
|
|
|
| 165 |
# Define Tasks
|
| 166 |
diagnose_task = Task(
|
| 167 |
description=(
|
| 168 |
+
f"1. Analyze the patient's symptoms ({symptoms}) and medical history ({medical_history}).\n"
|
| 169 |
+
f"2. Consider additional factors: Age: {age}, Gender: {gender}\n"
|
| 170 |
+
"3. Provide a preliminary diagnosis with possible conditions.\n"
|
| 171 |
+
"4. List the most likely conditions in order of probability."
|
| 172 |
),
|
| 173 |
+
expected_output="A detailed preliminary diagnosis with ranked possible conditions.",
|
| 174 |
agent=diagnostician
|
| 175 |
)
|
| 176 |
|
| 177 |
treatment_task = Task(
|
| 178 |
description=(
|
| 179 |
+
"1. Based on the diagnosis, create a comprehensive treatment plan.\n"
|
| 180 |
+
f"2. Consider patient profile: Age: {age}, Gender: {gender}\n"
|
| 181 |
+
f"3. Account for medical history: {medical_history}\n"
|
| 182 |
+
"4. Provide detailed recommendations including:\n"
|
| 183 |
+
" - Medications and dosages\n"
|
| 184 |
+
" - Lifestyle modifications\n"
|
| 185 |
+
" - Follow-up care schedule\n"
|
| 186 |
+
" - Warning signs to watch for"
|
| 187 |
),
|
| 188 |
+
expected_output="A comprehensive, personalized treatment plan.",
|
| 189 |
agent=treatment_advisor
|
| 190 |
)
|
| 191 |
|
| 192 |
# Create Crew
|
|
|
|
| 193 |
crew = Crew(
|
| 194 |
agents=[diagnostician, treatment_advisor],
|
| 195 |
tasks=[diagnose_task, treatment_task],
|
| 196 |
+
verbose=True
|
| 197 |
)
|
| 198 |
|
| 199 |
+
# Analysis Button
|
| 200 |
+
if st.button("Generate Analysis"):
|
| 201 |
+
if not symptoms or not medical_history:
|
| 202 |
+
st.error("Please provide both symptoms and medical history before generating analysis.")
|
| 203 |
+
else:
|
| 204 |
+
with tab2:
|
| 205 |
+
with st.status("π Processing..."):
|
| 206 |
+
st.write("Analyzing patient data...")
|
| 207 |
+
st.write("Generating diagnosis...")
|
| 208 |
+
st.write("Creating treatment plan...")
|
| 209 |
+
|
| 210 |
+
result = crew.kickoff(inputs={
|
| 211 |
+
"symptoms": symptoms,
|
| 212 |
+
"medical_history": medical_history
|
| 213 |
+
})
|
| 214 |
+
|
| 215 |
+
st.success("Analysis Complete!")
|
| 216 |
+
|
| 217 |
+
# Display Results
|
| 218 |
+
st.markdown("### π Analysis Results")
|
| 219 |
+
st.markdown(result)
|
| 220 |
+
|
| 221 |
+
# Generate and offer download
|
| 222 |
+
docx_file = generate_docx(result)
|
| 223 |
+
download_link = get_download_link(docx_file, "medical_analysis_report.docx")
|
| 224 |
+
st.markdown("### π₯ Download Report")
|
| 225 |
+
st.markdown(download_link, unsafe_allow_html=True)
|
| 226 |
+
|
| 227 |
+
# Additional recommendations
|
| 228 |
+
st.markdown("### β‘ Next Steps")
|
| 229 |
+
st.info("""
|
| 230 |
+
1. Review the generated report in detail
|
| 231 |
+
2. Consider additional specialist consultations if needed
|
| 232 |
+
3. Schedule necessary follow-up appointments
|
| 233 |
+
4. Monitor patient progress and adjust treatment as needed
|
| 234 |
+
""")
|