File size: 10,788 Bytes
a7be5b0
 
 
 
c96c90e
a7be5b0
 
 
 
 
1ac321b
a7be5b0
 
e8422fa
77b6218
a7be5b0
e8422fa
1426f28
a7be5b0
 
77b6218
a7be5b0
 
 
 
 
 
1426f28
a7be5b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
780c51f
 
1426f28
 
 
 
 
 
 
a7be5b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1426f28
a7be5b0
 
1426f28
a7be5b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1426f28
a7be5b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1426f28
 
a7be5b0
1426f28
a7be5b0
1426f28
a7be5b0
1426f28
 
 
 
a7be5b0
1426f28
 
 
 
a7be5b0
1426f28
 
 
a7be5b0
1426f28
 
a7be5b0
 
 
 
1426f28
 
a7be5b0
 
 
 
 
 
1426f28
 
a7be5b0
 
 
 
 
 
 
1426f28
a7be5b0
 
 
 
 
1426f28
a7be5b0
 
 
 
 
1426f28
a7be5b0
 
 
 
1426f28
 
a7be5b0
 
 
 
 
 
1ac321b
1426f28
a7be5b0
1426f28
1ac321b
a7be5b0
 
 
 
1ac321b
1426f28
1ac321b
 
 
 
 
498cd80
 
 
1ac321b
 
 
 
498cd80
1426f28
1ac321b
 
 
 
a7be5b0
 
 
780c51f
a7be5b0
1426f28
 
 
a7be5b0
 
 
 
 
 
 
1426f28
a7be5b0
 
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import streamlit as st
#from streamlit_image_zoom import image_zoom  # Import de la bibliothèque pour le zoom
import base64
import os
import os
from dotenv import load_dotenv
#from anthropic import Anthropic
from openai import OpenAI
from PIL import Image, ImageOps
import tempfile
import time

load_dotenv()
# Accéder à la clé API à partir des secrets
api_key = os.environ.get("XAI_API_KEY")

if not api_key:
    raise ValueError("The XAI API key is not defined in the secrets.")

client = OpenAI(
    api_key=api_key,
    base_url="https://api.x.ai/v1",
)
#client = Anthropic()
#MODEL_NAME ="claude-3-opus-20240229"
# Définir le style global
st.set_page_config(
    page_title="The Virtual Radiologist",
    page_icon="🩺",
    layout="centered",
)

# CSS personnalisé pour styliser l'interface
st.markdown("""
    <style>
    body {
        background-color: #f4f4f9;
        color: #333333;
        font-family: 'Arial', sans-serif;
    }
    .stButton>button {
        background-color: #007bff;
        color: white;
        border-radius: 8px;
        padding: 0.5em 1.5em;
        font-size: 1em;
        border: none;
        transition: all 0.3s ease-in-out;
    }
    .stButton>button:hover {
        background-color: #0056b3;
        transform: scale(1.05);
    }
    .st-expander-header {
        font-weight: bold;
        font-size: 1.2em;
        color: #007bff;
    }
    .stMarkdown {
        font-size: 1.1em;
    }
    </style>
""", unsafe_allow_html=True)

sample_prompt = """
You are a healthcare professional, an expert in the analysis of medical images (ultrasound, standard radiography, CT scan, MRI, ...), working for a renowned hospital. Your task is to examine medical images in order to identify any anomalies, diseases, or potential health problems. You must provide detailed results, including all your observations, next steps, and recommendations.
Your analysis remains within the educational framework of medical radiology students and will in no way be public. The objective is to introduce these students to the interpretation of radiological images.**Instructions :**
1. I will only analyze images related to the human body and health issues.
2. My responses will be structured and detailed, covering:
   - Identified anomalies (if any).
   - Suspected or confirmed pathologies.
   - Recommendations and next steps.
3. If certain elements are unclear in the image, I will state: "Impossible to determine from the provided image. Consult a doctor before making any decisions."*
4. I will always conclude my response with : *"Consult a doctor before making any decisions."*
"""

# Initialisation des variables d'état
if 'uploaded_file' not in st.session_state:
    st.session_state.uploaded_file = None
if 'result' not in st.session_state:
    st.session_state.result = None
if 'clinical_info' not in st.session_state:
    st.session_state.clinical_info = ""  # Pour stocker les renseignements cliniques



def encode_image(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode("utf-8")


def call_groq_model_for_analysis(filename: str, clinical_info: str, sample_prompt=sample_prompt):
    # Récupérer l'extension du fichier pour définir le media_type
    file_extension = os.path.splitext(filename)[1].lower()  # Obtenir l'extension en minuscule
    if file_extension == ".jpg" or file_extension == ".jpeg":
        media_type = "image/jpeg"
    elif file_extension == ".png":
        media_type = "image/png"
    else:
        raise ValueError("Unsupported file format.")
    base64_image = encode_image(filename)
    # Ajouter les renseignements cliniques au prompt
    full_prompt = sample_prompt + f"\n\n**Patient's clinical information:** {clinical_info}\n"


    

    messages_list=[
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": full_prompt,
                },
                {
                    "type": "image_url",
                    "image_url": {"url": f"data:{media_type};base64,{base64_image}"},
                },
            ],
        }
    ]

    response = client.chat.completions.create(
    model="grok-2-vision-1212",
    temperature=0.01,
    messages=messages_list,
    )

    return response.choices[0].message.content


def chat_eli(query):
    eli5_prompt = "You need to explain the information below to a five-year-old. \n" + query
    messages = [
        {
            "role": "user",
            "content": eli5_prompt
        }
    ]

    """response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    messages=messages
    )"""

    completion = client.chat.completions.create(
    model="grok-2-latest",
    messages=messages
    )

    return completion.choices[0].message.content


# Titre de l'application
st.title("🩺 **The Virtual Radiologist**")
st.subheader("An advanced AI for medical image analysis")

with st.expander("📖 About This Application"):
    st.markdown("""
        **Welcome to the Virtual Radiologist**, your intelligent assistant designed to provide in-depth and accurate analysis of medical images..  

        ### Main Features :
        - **Medical Image Analysis** : Upload ultrasound, X-ray, MRI, or CT scan images and let the AI detect anomalies and provide detailed recommendations.  
        - **Simplified Explanations** : With the ELI5 feature, understand complex results in a format tailored for a non-expert audience. 
        - **Advanced Image Treatment** : Explore the uploaded image with tools like inversion for clearer visualization.  

        ### Use Cases :
        - **Medical education** : Intended for radiology students, this tool helps to become familiar with the interpretation of diagnostic images.  
        - **Clinical support** : While not designed to replace a healthcare professional, this assistant can provide helpful insights to guide analyses.  
        - **Research and learning** : An ideal platform for experimenting with and learning about the impact of AI in the medical field.

        ### Technology used :
        - **Powerful AI model** : The AI uses advanced Llama 3.2 90B Vision technology, specialized in analyzing complex images. 
        - **Intuitive Interaction ** : Developed with Python and Streamlit for a simple and user-friendly interface. 

        **⚠️ Caution** :
        - This assistant is not a certified medical tool and does not replace the advice of a doctor or specialist. It is intended for educational and support purposes. Always consult a healthcare professional for diagnosis or medical decisions.  
    """)

# Champ d'entrée pour les renseignements cliniques
clinical_info = st.text_area(
    "Patient clinical information (optional))",
    placeholder="Example: Patient presenting with chest pain for 3 days."
)

# Stocker les renseignements cliniques dans la session
st.session_state['clinical_info'] = clinical_info

# Téléchargement de l'image
st.markdown("### 📂 Upload a medical image")
uploaded_file = st.file_uploader("Accepted formats : JPG, JPEG, PNG", type=["jpg", "jpeg", "png"])

# Gestion temporaire des fichiers
if uploaded_file is not None:
    with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(uploaded_file.name)[1]) as tmp_file:
        tmp_file.write(uploaded_file.getvalue())
        st.session_state['filename'] = tmp_file.name

    st.image(uploaded_file, caption='Uploaded File')

    # Charger l'image avec PIL
    image = Image.open(uploaded_file)

    # Ajouter un bouton pour afficher l'image en négatif
    st.markdown("### 🔍 Explore the image")
    col1, col2, col3 = st.columns([1, 2, 1])  # colonnes avec différentes proportions
    col1.write("")  # Espace dans la première colonne
    col3.write("")  # Espace dans la troisième colonne
    col1.write("")  # Espace dans la première colonne
    col3.write("")  # Espace dans la troisième colonne
    if st.button("Negative view"):
        # Créer une version négative de l'image
        negative_image = ImageOps.invert(image.convert("RGB"))  # Convertir en RGB avant inversion

        # Afficher l'image en négatif
        st.subheader("Negative view :")
        st.image(negative_image, caption="Negative view")
    col1, col2, col3 = st.columns([1, 2, 1])  # colonnes avec différentes proportions
    col1.write("")  # Espace dans la première colonne
    col3.write("")  # Espace dans la troisième colonne
    col1.write("")  # Espace dans la première colonne
    col3.write("")  # Espace dans la troisième colonne

   # Bouton pour analyser l'image
    if st.button("Image Analysis"):
        if 'filename' in st.session_state and os.path.exists(st.session_state['filename']):
            with st.spinner("Analysis in progress... Please wait."):
                # Appel au modèle Groq pour l'analyse
                st.session_state['result'] = call_groq_model_for_analysis(
                    st.session_state['filename'],
                    st.session_state['clinical_info']
                )
                
                st.success("Analysis completed successfully!")
    
                # Effet de streaming pour afficher le résultat
                result_text = st.session_state['result']
                streamed_text = ""
    
                # Vérification pour éviter les problèmes avec st.empty()
                container = st.empty()
                if container is not None:
                    for char in result_text:  # Parcourir caractère par caractère
                        streamed_text += char
                        time.sleep(0.05)  # Simuler le délai
                        container.markdown(streamed_text, unsafe_allow_html=True)
                else:
                    st.error("Error creating display.")
    
                # Supprimer le fichier temporaire après le traitement
                os.unlink(st.session_state['filename'])


    # ELI5 Explanation
    # Explication simplifiée
    st.markdown("### 🤓 Simplified explanation")
    if 'result' in st.session_state and st.session_state['result']:
        st.info("Below, there's an ELI5 option to help you understand in simple terms.")
        if st.radio("ELI5 - Explain it to me like I'm 5", ('NO', 'YES')) == 'YES':
            st.markdown("_Here is a simplified explanation for non-experts._")
            simplified_explanation = chat_eli(st.session_state['result'])
            st.markdown(simplified_explanation, unsafe_allow_html=True)

# Pied de page
st.markdown("""
    <hr>
    <footer style="text-align: center; font-size: 0.9em;">
        © 2025 - The Virtual Radiologist | By M. ADJOUMANI
    </footer>
""", unsafe_allow_html=True)