|
import streamlit as st |
|
from google import genai |
|
from google.genai import types |
|
from PIL import Image |
|
import json |
|
|
|
def main(): |
|
st.title("Analyseur d'Images Géométriques avec Gemini") |
|
|
|
|
|
try: |
|
api_key = st.secrets["GOOGLE_API_KEY"] |
|
except Exception as e: |
|
st.error("Erreur: La clé API Google n'est pas configurée dans les secrets.") |
|
st.info("Créez un fichier .streamlit/secrets.toml avec le contenu suivant:") |
|
st.code(""" |
|
GOOGLE_API_KEY = "votre_clé_api" |
|
""") |
|
return |
|
|
|
|
|
try: |
|
client = genai.Client( |
|
api_key=api_key, |
|
http_options={'api_version':'v1alpha'} |
|
) |
|
except Exception as e: |
|
st.error(f"Erreur lors de l'initialisation du client: {e}") |
|
return |
|
|
|
|
|
uploaded_file = st.file_uploader("Choisissez une image géométrique", type=['png', 'jpg', 'jpeg']) |
|
|
|
if uploaded_file: |
|
try: |
|
|
|
image = Image.open(uploaded_file) |
|
st.image(image, caption="Image téléchargée", use_column_width=True) |
|
|
|
|
|
model_name = "gemini-2.0-flash-thinking-exp-01-21" |
|
|
|
if st.button("Analyser l'image"): |
|
with st.spinner("Analyse en cours..."): |
|
try: |
|
|
|
response = client.models.generate_content( |
|
model=model_name, |
|
config={'thinking_config': {'include_thoughts': True}}, |
|
contents=[ |
|
image, |
|
"What's the area of the overlapping region?" |
|
] |
|
) |
|
|
|
|
|
for part in response.candidates[0].content.parts: |
|
if part.thought: |
|
with st.expander("Voir le raisonnement"): |
|
st.markdown(part.text) |
|
else: |
|
st.subheader("Réponse") |
|
st.markdown(part.text) |
|
|
|
except Exception as e: |
|
st.error(f"Erreur lors de l'analyse: {e}") |
|
|
|
except Exception as e: |
|
st.error(f"Erreur lors du traitement de l'image: {e}") |
|
|
|
if __name__ == "__main__": |
|
main() |