|
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") |
|
|
|
|
|
api_key = st.sidebar.text_input("Entrez votre clé API Google", type="password") |
|
if not api_key: |
|
st.warning("Veuillez entrer votre clé API Google dans la barre latérale") |
|
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: |
|
st.subheader("Réflexions") |
|
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() |