File size: 8,417 Bytes
3b091bc
 
 
 
 
 
 
 
 
 
c119d0c
3b091bc
 
c119d0c
3b091bc
 
 
c119d0c
3b091bc
 
 
5b2fcd5
 
 
 
 
 
 
 
3b091bc
 
 
 
 
 
 
c119d0c
3b091bc
c119d0c
3b091bc
c119d0c
 
 
 
3b091bc
 
c119d0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4a2c1e
c119d0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b091bc
 
 
 
 
 
 
 
 
 
 
 
 
c119d0c
3b091bc
 
 
 
 
 
 
 
c119d0c
 
3b091bc
 
 
 
 
 
 
 
 
c119d0c
 
 
 
3b091bc
c119d0c
3b091bc
 
 
 
 
c119d0c
 
 
 
 
 
 
 
3b091bc
c119d0c
3b091bc
 
 
 
 
 
 
c119d0c
3b091bc
 
c119d0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b2fcd5
 
 
 
 
 
c119d0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
243
244
245
246
import streamlit as st
from crewai import Agent, Task, Crew, Process
import os
from crewai_tools import ScrapeWebsiteTool, SerperDevTool
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from docx import Document
from io import BytesIO
import base64

# Load environment variables
load_dotenv()

# Configure API keys
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
os.environ["SERPER_API_KEY"] = os.getenv("SERPER_API_KEY")

# Helper Functions
def generate_docx(result):
    doc = Document()
    doc.add_heading('Healthcare Diagnosis and Treatment Recommendations', 0)
    
    # Convert result to string if it's a tuple
    if isinstance(result, tuple):
        result_str = '\n\n'.join(str(item) for item in result)
    else:
        result_str = str(result)
    
    doc.add_paragraph(result_str)
    bio = BytesIO()
    doc.save(bio)
    bio.seek(0)
    return bio

def get_download_link(bio, filename):
    b64 = base64.b64encode(bio.read()).decode()
    return f'<a href="data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,{b64}" download="{filename}" class="download-button">Download Report</a>'

# Page Configuration
st.set_page_config(
    page_title="Medical AI Assistant",
    page_icon="πŸ₯",
    layout="wide",
    initial_sidebar_state="expanded"
)

# Custom CSS
st.markdown("""
    <style>
    .main {
        padding: 2rem;
    }
    .stButton > button {
        width: 100%;
        background-color: #007bff;
        color: white;
        padding: 0.5rem 1rem;
        border-radius: 0.5rem;
        border: none;
        margin-top: 1rem;
    }
    .stButton > button:hover {
        background-color: #0056b3;
    }
    .download-button {
        display: inline-block;
        padding: 0.5rem 1rem;
        background-color: #28a745;
        color: white;
        text-decoration: none;
        border-radius: 0.5rem;
        margin-top: 1rem;
    }
    .download-button:hover {
        background-color: #218838;
        color: white;
    }
    .stTextInput > div > div > input {
        border-radius: 0.5rem;
    }
    .stTextArea > div > div > textarea {
        border-radius: 0.5rem;
    }
    </style>
""", unsafe_allow_html=True)

# Sidebar for Patient Information
with st.sidebar:
    st.image("https://formaspace.com/wp-content/uploads/2024/04/ai-dr.jpeg", width=100)
    st.title("Patient Information")
    
    with st.form("patient_info"):
        gender = st.selectbox('Gender', ('Male', 'Female', 'Other'))
        age = st.number_input('Age', min_value=0, max_value=120, value=25)
        height = st.number_input('Height (cm)', min_value=0, max_value=300, value=170)
        weight = st.number_input('Weight (kg)', min_value=0, max_value=500, value=70)
        submit_button = st.form_submit_button("Save Patient Info")

# Main Content
st.title("πŸ₯ Medical AI Assistant")
st.markdown("""
    <div style='background-color: #f8f9fa; padding: 1rem; border-radius: 0.5rem; margin-bottom: 2rem;'>
        <h4>Welcome to the Medical AI Assistant</h4>
        <p>This AI-powered system helps medical professionals with diagnosis and treatment recommendations. 
        Please enter the patient's symptoms and medical history below.</p>
    </div>
""", unsafe_allow_html=True)

# Create tabs for different sections
tab1, tab2 = st.tabs(["πŸ“ Patient Assessment", "πŸ“Š Results"])

with tab1:
    col1, col2 = st.columns(2)
    
    with col1:
        st.subheader("Current Symptoms")
        symptoms = st.text_area(
            'Describe the symptoms in detail',
            placeholder='e.g., persistent fever for 3 days, dry cough, fatigue',
            height=200
        )

    with col2:
        st.subheader("Medical History")
        medical_history = st.text_area(
            'Enter relevant medical history',
            placeholder='e.g., Type 2 diabetes diagnosed in 2019, hypertension',
            height=200
        )

    # Additional Information
    with st.expander("Additional Information (Optional)"):
        col3, col4 = st.columns(2)
        with col3:
            allergies = st.text_area("Known Allergies", placeholder="e.g., penicillin, peanuts")
            current_medications = st.text_area("Current Medications", placeholder="e.g., metformin 500mg twice daily")
        with col4:
            family_history = st.text_area("Family History", placeholder="e.g., heart disease, diabetes")
            lifestyle = st.text_area("Lifestyle Factors", placeholder="e.g., smoker, exercises 3 times a week")

# Initialize Tools and Agents
search_tool = SerperDevTool()
scrape_tool = ScrapeWebsiteTool()

llm = ChatOpenAI(
    model="gpt-3.5-turbo-16k",
    temperature=0.1,
    max_tokens=8000
)

# Define Agents
diagnostician = Agent(
    role="Medical Diagnostician",
    goal="Analyze patient symptoms and medical history to provide a preliminary diagnosis.",
    backstory="Expert in diagnosing medical conditions using advanced algorithms and comprehensive medical knowledge.",
    verbose=True,
    allow_delegation=False,
    tools=[search_tool, scrape_tool],
    llm=llm
)

treatment_advisor = Agent(
    role="Treatment Advisor",
    goal="Recommend appropriate treatment plans based on the diagnosis.",
    backstory="Specialist in creating personalized treatment plans considering patient history and current medical best practices.",
    verbose=True,
    allow_delegation=False,
    tools=[search_tool, scrape_tool],
    llm=llm
)

# Define Tasks
diagnose_task = Task(
    description=(
        f"1. Analyze the patient's symptoms ({symptoms}) and medical history ({medical_history}).\n"
        f"2. Consider additional factors: Age: {age}, Gender: {gender}\n"
        "3. Provide a preliminary diagnosis with possible conditions.\n"
        "4. List the most likely conditions in order of probability."
    ),
    expected_output="A detailed preliminary diagnosis with ranked possible conditions.",
    agent=diagnostician
)

treatment_task = Task(
    description=(
        "1. Based on the diagnosis, create a comprehensive treatment plan.\n"
        f"2. Consider patient profile: Age: {age}, Gender: {gender}\n"
        f"3. Account for medical history: {medical_history}\n"
        "4. Provide detailed recommendations including:\n"
        "   - Medications and dosages\n"
        "   - Lifestyle modifications\n"
        "   - Follow-up care schedule\n"
        "   - Warning signs to watch for"
    ),
    expected_output="A comprehensive, personalized treatment plan.",
    agent=treatment_advisor
)

# Create Crew
crew = Crew(
    agents=[diagnostician, treatment_advisor],
    tasks=[diagnose_task, treatment_task],
    verbose=True
)

# Analysis Button
if st.button("Generate Analysis"):
    if not symptoms or not medical_history:
        st.error("Please provide both symptoms and medical history before generating analysis.")
    else:
        with tab2:
            with st.status("πŸ”„ Processing..."):
                st.write("Analyzing patient data...")
                st.write("Generating diagnosis...")
                st.write("Creating treatment plan...")
                
                result = crew.kickoff(inputs={
                    "symptoms": symptoms,
                    "medical_history": medical_history
                })
                
            st.success("Analysis Complete!")
            
            # Display Results
            st.markdown("### πŸ“‹ Analysis Results")
            if isinstance(result, tuple):
                for item in result:
                    st.markdown(str(item))
                    st.markdown("---")  # Add a separator between items
            else:
                st.markdown(str(result))
            
            # Generate and offer download
            docx_file = generate_docx(result)
            download_link = get_download_link(docx_file, "medical_analysis_report.docx")
            st.markdown("### πŸ“₯ Download Report")
            st.markdown(download_link, unsafe_allow_html=True)
            
            # Additional recommendations
            st.markdown("### ⚑ Next Steps")
            st.info("""
                1. Review the generated report in detail
                2. Consider additional specialist consultations if needed
                3. Schedule necessary follow-up appointments
                4. Monitor patient progress and adjust treatment as needed
            """)