jbondy007's picture
language correction
780c51f verified
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)