chalisesagun commited on
Commit
12b0821
·
verified ·
1 Parent(s): 0f2c1a0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +157 -0
app.py ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from PIL import Image
3
+ from phi.agent import Agent
4
+ from phi.model.google import Gemini
5
+ import streamlit as st
6
+ from phi.tools.duckduckgo import DuckDuckGo
7
+
8
+ if "GOOGLE_API_KEY" not in st.session_state:
9
+ st.session_state.GOOGLE_API_KEY = None
10
+
11
+ with st.sidebar:
12
+ st.title("ℹ️ Configuration")
13
+
14
+ if not st.session_state.GOOGLE_API_KEY:
15
+ api_key = st.text_input(
16
+ "Enter your Google API Key:",
17
+ type="password"
18
+ )
19
+ st.caption(
20
+ "Get your API key from [Google AI Studio]"
21
+ "(https://aistudio.google.com/apikey) 🔑"
22
+ )
23
+ if api_key:
24
+ st.session_state.GOOGLE_API_KEY = api_key
25
+ st.success("API Key saved!")
26
+ st.rerun()
27
+ else:
28
+ st.success("API Key is configured")
29
+ if st.button("🔄 Reset API Key"):
30
+ st.session_state.GOOGLE_API_KEY = None
31
+ st.rerun()
32
+
33
+ st.info(
34
+ "This tool provides AI-powered analysis of medical imaging data using "
35
+ "advanced computer vision and radiological expertise."
36
+ )
37
+ st.warning(
38
+ "⚠DISCLAIMER: This tool is for educational and informational purposes only. "
39
+ "All analyses should be reviewed by qualified healthcare professionals. "
40
+ "Do not make medical decisions based solely on this analysis."
41
+ )
42
+
43
+ medical_agent = Agent(
44
+ model=Gemini(
45
+ api_key=st.session_state.GOOGLE_API_KEY,
46
+ id="gemini-2.0-flash-exp"
47
+ ),
48
+ tools=[DuckDuckGo()],
49
+ markdown=True
50
+ ) if st.session_state.GOOGLE_API_KEY else None
51
+
52
+ if not medical_agent:
53
+ st.warning("Please configure your API key in the sidebar to continue")
54
+
55
+ # Medical Analysis Query
56
+ query = """
57
+ You are a highly skilled medical imaging expert with extensive knowledge in radiology and diagnostic imaging. Analyze the patient's medical image and structure your response as follows:
58
+
59
+ ### 1. Image Type & Region
60
+ - Specify imaging modality (X-ray/MRI/CT/Ultrasound/etc.)
61
+ - Identify the patient's anatomical region and positioning
62
+ - Comment on image quality and technical adequacy
63
+
64
+ ### 2. Key Findings
65
+ - List primary observations systematically
66
+ - Note any abnormalities in the patient's imaging with precise descriptions
67
+ - Include measurements and densities where relevant
68
+ - Describe location, size, shape, and characteristics
69
+ - Rate severity: Normal/Mild/Moderate/Severe
70
+
71
+ ### 3. Diagnostic Assessment
72
+ - Provide primary diagnosis with confidence level
73
+ - List differential diagnoses in order of likelihood
74
+ - Support each diagnosis with observed evidence from the patient's imaging
75
+ - Note any critical or urgent findings
76
+
77
+ ### 4. Patient-Friendly Explanation
78
+ - Explain the findings in simple, clear language that the patient can understand
79
+ - Avoid medical jargon or provide clear definitions
80
+ - Include visual analogies if helpful
81
+ - Address common patient concerns related to these findings
82
+
83
+ ### 5. Research Context
84
+ IMPORTANT: Use the DuckDuckGo search tool to:
85
+ - Find recent medical literature about similar cases
86
+ - Search for standard treatment protocols
87
+ - Provide a list of relevant medical links of them too
88
+ - Research any relevant technological advances
89
+ - Include 2-3 key references to support your analysis
90
+
91
+ Format your response using clear markdown headers and bullet points. Be concise yet thorough.
92
+ """
93
+
94
+ st.title("🏥 Medical Imaging Diagnosis Agent")
95
+ st.write("Upload a medical image for professional analysis")
96
+
97
+ # Create containers for better organization
98
+ upload_container = st.container()
99
+ image_container = st.container()
100
+ analysis_container = st.container()
101
+
102
+ with upload_container:
103
+ uploaded_file = st.file_uploader(
104
+ "Upload Medical Image",
105
+ type=["jpg", "jpeg", "png", "dicom"],
106
+ help="Supported formats: JPG, JPEG, PNG, DICOM"
107
+ )
108
+
109
+ if uploaded_file is not None:
110
+ with image_container:
111
+ # Center the image using columns
112
+ col1, col2, col3 = st.columns([1, 2, 1])
113
+ with col2:
114
+ image = Image.open(uploaded_file)
115
+ # Calculate aspect ratio for resizing
116
+ width, height = image.size
117
+ aspect_ratio = width / height
118
+ new_width = 500
119
+ new_height = int(new_width / aspect_ratio)
120
+ resized_image = image.resize((new_width, new_height))
121
+
122
+ st.image(
123
+ resized_image,
124
+ caption="Uploaded Medical Image",
125
+ use_container_width=True
126
+ )
127
+
128
+ analyze_button = st.button(
129
+ "🔍 Analyze Image",
130
+ type="primary",
131
+ use_container_width=True
132
+ )
133
+
134
+ with analysis_container:
135
+ if analyze_button:
136
+ image_path = "temp_medical_image.png"
137
+ with open(image_path, "wb") as f:
138
+ f.write(uploaded_file.getbuffer())
139
+
140
+ with st.spinner("🔄 Analyzing image... Please wait."):
141
+ try:
142
+ response = medical_agent.run(query, images=[image_path])
143
+ st.markdown("### 📋 Analysis Results")
144
+ st.markdown("---")
145
+ st.markdown(response.content)
146
+ st.markdown("---")
147
+ st.caption(
148
+ "Note: This analysis is generated by AI and should be reviewed by "
149
+ "a qualified healthcare professional."
150
+ )
151
+ except Exception as e:
152
+ st.error(f"Analysis error: {e}")
153
+ finally:
154
+ if os.path.exists(image_path):
155
+ os.remove(image_path)
156
+ else:
157
+ st.info("👆 Please upload a medical image to begin analysis")