-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai_medical_imaging_FR_EN.py
192 lines (154 loc) · 6.46 KB
/
ai_medical_imaging_FR_EN.py
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
import os
from PIL import Image
from phi.agent import Agent
from phi.model.google import Gemini
import streamlit as st
from phi.tools.duckduckgo import DuckDuckGo
import googletrans
from googletrans import Translator
if "GOOGLE_API_KEY" not in st.session_state:
st.session_state.GOOGLE_API_KEY = None
with st.sidebar:
st.title("ℹ️ Configuration")
if not st.session_state.GOOGLE_API_KEY:
api_key = st.text_input(
"Enter your Google API Key:",
type="password"
)
st.caption(
"Get your API key from [Google AI Studio]"
"(https://aistudio.google.com/apikey) 🔑"
)
if api_key:
st.session_state.GOOGLE_API_KEY = api_key
st.success("API Key saved!")
st.rerun()
else:
st.success("API Key is configured")
if st.button("🔄 Reset API Key"):
st.session_state.GOOGLE_API_KEY = None
st.rerun()
st.info(
"This tool provides AI-powered analysis of medical imaging data using "
"advanced computer vision and radiological expertise."
)
st.warning(
"⚠DISCLAIMER: This tool is for educational and informational purposes only. "
"All analyses should be reviewed by qualified healthcare professionals. "
"Do not make medical decisions based solely on this analysis."
)
medical_agent = Agent(
model=Gemini(
api_key=st.session_state.GOOGLE_API_KEY,
id="gemini-2.0-flash-exp"
),
tools=[DuckDuckGo()],
markdown=True
) if st.session_state.GOOGLE_API_KEY else None
if not medical_agent:
st.warning("Please configure your API key in the sidebar to continue")
# Medical Analysis Query
query = """
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:
### 1. Image Type & Region
- Specify imaging modality (X-ray/MRI/CT/Ultrasound/etc.)
- Identify the patient's anatomical region and positioning
- Comment on image quality and technical adequacy
### 2. Key Findings
- List primary observations systematically
- Note any abnormalities in the patient's imaging with precise descriptions
- Include measurements and densities where relevant
- Describe location, size, shape, and characteristics
- Rate severity: Normal/Mild/Moderate/Severe
### 3. Diagnostic Assessment
- Provide primary diagnosis with confidence level
- List differential diagnoses in order of likelihood
- Support each diagnosis with observed evidence from the patient's imaging
- Note any critical or urgent findings
### 4. Patient-Friendly Explanation
- Explain the findings in simple, clear language that the patient can understand
- Avoid medical jargon or provide clear definitions
- Include visual analogies if helpful
- Address common patient concerns related to these findings
### 5. Research Context
IMPORTANT: Use the DuckDuckGo search tool to:
- Find recent medical literature about similar cases
- Search for standard treatment protocols
- Provide a list of relevant medical links of them too
- Research any relevant technological advances
- Include 2-3 key references to support your analysis
Format your response using clear markdown headers and bullet points. Be concise yet thorough.
And write in French
"""
st.title("🏥 Medical Imaging Diagnosis Agent")
st.write("Upload a medical image for professional analysis")
# Définition d'une fonction de traduction
def traduire_texte(texte):
# Utilisation d'un service de traduction comme Google Translate
translator = Translator()
texte_fr = translator.translate(texte, dest='fr').text
return texte_fr
# Create containers for better organization
upload_container = st.container()
image_container = st.container()
analysis_container = st.container()
with upload_container:
uploaded_file = st.file_uploader(
"Upload Medical Image",
type=["jpg", "jpeg", "png", "dicom"],
help="Supported formats: JPG, JPEG, PNG, DICOM"
)
if uploaded_file is not None:
with image_container:
# Center the image using columns
col1, col2, col3 = st.columns([1, 2, 1])
with col2:
image = Image.open(uploaded_file)
# Calculate aspect ratio for resizing
width, height = image.size
aspect_ratio = width / height
new_width = 500
new_height = int(new_width / aspect_ratio)
resized_image = image.resize((new_width, new_height))
st.image(
resized_image,
caption="Uploaded Medical Image",
use_column_width=True
)
analyze_button = st.button(
"🔍 Analyze Image",
type="primary",
use_container_width=True
)
with analysis_container:
if analyze_button:
image_path = "temp_medical_image.png"
with open(image_path, "wb") as f:
f.write(uploaded_file.getbuffer())
with st.spinner("🔄 Analyzing image... Please wait."):
try:
response = medical_agent.run(query, images=[image_path])
st.markdown("### 📋 Analysis Results")
st.markdown("---")
st.markdown(response.content)
st.markdown("---")
st.caption(
"Note: This analysis is generated by AI and should be reviewed by "
"a qualified healthcare professional."
)
# Définition du texte à traduire
response_fr = traduire_texte(response.content)
# Création d'un bouton pour traduire le texte
bouton_traduire = st.button("Traduire en français")
# Si le bouton est cliqué, afficher le texte traduit
if bouton_traduire:
# Affichage du texte traduit
st.markdown(f"**Analyse médicale traduite en français :**")
st.write(response_fr)
except Exception as e:
st.error(f"Analysis error: {e}")
finally:
if os.path.exists(image_path):
os.remove(image_path)
else:
st.info("👆 Please upload a medical image to begin analysis")