Update app.py
Browse files
app.py
CHANGED
@@ -1,67 +1,67 @@
|
|
1 |
import streamlit as st
|
2 |
from google import genai
|
3 |
from google.genai import types
|
4 |
-
import os
|
5 |
from PIL import Image
|
6 |
-
import io
|
7 |
-
import base64
|
8 |
import json
|
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 |
-
mode = 'starting'
|
34 |
try:
|
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 |
except Exception as e:
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
1 |
import streamlit as st
|
2 |
from google import genai
|
3 |
from google.genai import types
|
|
|
4 |
from PIL import Image
|
|
|
|
|
5 |
import json
|
6 |
|
7 |
+
def main():
|
8 |
+
st.title("Analyseur d'Images Géométriques avec Gemini")
|
9 |
+
|
10 |
+
# Configuration de l'API key
|
11 |
+
api_key = st.sidebar.text_input("Entrez votre clé API Google", type="password")
|
12 |
+
if not api_key:
|
13 |
+
st.warning("Veuillez entrer votre clé API Google dans la barre latérale")
|
14 |
+
return
|
15 |
+
|
16 |
+
# Initialisation du client
|
17 |
+
try:
|
18 |
+
client = genai.Client(
|
19 |
+
api_key=api_key,
|
20 |
+
http_options={'api_version':'v1alpha'}
|
21 |
+
)
|
22 |
+
except Exception as e:
|
23 |
+
st.error(f"Erreur lors de l'initialisation du client: {e}")
|
24 |
+
return
|
25 |
+
|
26 |
+
# Upload de l'image
|
27 |
+
uploaded_file = st.file_uploader("Choisissez une image géométrique", type=['png', 'jpg', 'jpeg'])
|
28 |
+
|
29 |
+
if uploaded_file:
|
|
|
30 |
try:
|
31 |
+
# Affichage de l'image
|
32 |
+
image = Image.open(uploaded_file)
|
33 |
+
st.image(image, caption="Image téléchargée", use_column_width=True)
|
34 |
+
|
35 |
+
# Configuration du modèle
|
36 |
+
model_name = "gemini-2.0-flash-thinking-exp-01-21"
|
37 |
+
|
38 |
+
if st.button("Analyser l'image"):
|
39 |
+
with st.spinner("Analyse en cours..."):
|
40 |
+
try:
|
41 |
+
# Génération de la réponse
|
42 |
+
response = client.models.generate_content(
|
43 |
+
model=model_name,
|
44 |
+
config={'thinking_config': {'include_thoughts': True}},
|
45 |
+
contents=[
|
46 |
+
image,
|
47 |
+
"What's the area of the overlapping region?"
|
48 |
+
]
|
49 |
+
)
|
50 |
+
|
51 |
+
# Affichage des résultats
|
52 |
+
for part in response.candidates[0].content.parts:
|
53 |
+
if part.thought:
|
54 |
+
st.subheader("Réflexions")
|
55 |
+
st.markdown(part.text)
|
56 |
+
else:
|
57 |
+
st.subheader("Réponse")
|
58 |
+
st.markdown(part.text)
|
59 |
+
|
60 |
+
except Exception as e:
|
61 |
+
st.error(f"Erreur lors de l'analyse: {e}")
|
62 |
+
|
63 |
except Exception as e:
|
64 |
+
st.error(f"Erreur lors du traitement de l'image: {e}")
|
65 |
+
|
66 |
+
if __name__ == "__main__":
|
67 |
+
main()
|