Docfile commited on
Commit
979f189
·
verified ·
1 Parent(s): cd8a1b9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -23
app.py CHANGED
@@ -3,6 +3,11 @@ from google import genai
3
  from google.genai import types
4
  from PIL import Image
5
  import json
 
 
 
 
 
6
 
7
  def stream_response(container, response):
8
  """Gère le streaming de la réponse avec un affichage progressif"""
@@ -12,24 +17,36 @@ def stream_response(container, response):
12
  thinking_text = ""
13
  answer_text = ""
14
 
15
- for chunk in response:
16
- for part in chunk.candidates[0].content.parts:
17
- if part.thought:
18
- if mode != "thinking":
19
- if thinking_placeholder is None:
20
- with container.expander("Voir le raisonnement", expanded=False):
21
- thinking_placeholder = st.empty()
22
- mode = "thinking"
23
- thinking_text += part.text
24
- thinking_placeholder.markdown(thinking_text)
25
- else:
26
- if mode != "answering":
27
- if answer_placeholder is None:
28
- answer_placeholder = container.empty()
29
- container.subheader("Réponse")
30
- mode = "answering"
31
- answer_text += part.text
32
- answer_placeholder.markdown(answer_text)
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  def main():
35
  st.title("Analyseur d'Images Géométriques avec Gemini")
@@ -38,8 +55,8 @@ def main():
38
  try:
39
  api_key = st.secrets["GEMINI_API_KEY"]
40
  except Exception as e:
41
- st.error("Erreur: dans les secrets.")
42
-
43
  return
44
 
45
  # Initialisation du client
@@ -49,7 +66,8 @@ def main():
49
  http_options={'api_version':'v1alpha'}
50
  )
51
  except Exception as e:
52
- st.error(f"Erreur lors de l'initialisation du client: {e}")
 
53
  return
54
 
55
  # Upload de l'image
@@ -70,6 +88,16 @@ def main():
70
 
71
  with st.spinner("Analyse en cours..."):
72
  try:
 
 
 
 
 
 
 
 
 
 
73
  # Génération de la réponse en streaming
74
  response = client.models.generate_content_stream(
75
  model=model_name,
@@ -84,10 +112,12 @@ def main():
84
  stream_response(response_container, response)
85
 
86
  except Exception as e:
87
- st.error(f"Erreur lors de l'analyse: {e}")
 
88
 
89
  except Exception as e:
90
- st.error(f"Erreur lors du traitement de l'image: {e}")
 
91
 
92
  if __name__ == "__main__":
93
  main()
 
3
  from google.genai import types
4
  from PIL import Image
5
  import json
6
+ import logging
7
+
8
+ # Configuration du logging
9
+ logging.basicConfig(level=logging.DEBUG)
10
+ logger = logging.getLogger(__name__)
11
 
12
  def stream_response(container, response):
13
  """Gère le streaming de la réponse avec un affichage progressif"""
 
17
  thinking_text = ""
18
  answer_text = ""
19
 
20
+ try:
21
+ for chunk in response:
22
+ logger.debug(f"Chunk reçu: {chunk}")
23
+
24
+ try:
25
+ for part in chunk.candidates[0].content.parts:
26
+ if hasattr(part, 'thought') and part.thought:
27
+ if mode != "thinking":
28
+ if thinking_placeholder is None:
29
+ with container.expander("Voir le raisonnement", expanded=False):
30
+ thinking_placeholder = st.empty()
31
+ mode = "thinking"
32
+ thinking_text += part.text
33
+ thinking_placeholder.markdown(thinking_text)
34
+ else:
35
+ if mode != "answering":
36
+ if answer_placeholder is None:
37
+ answer_placeholder = container.empty()
38
+ container.subheader("Réponse")
39
+ mode = "answering"
40
+ answer_text += part.text
41
+ answer_placeholder.markdown(answer_text)
42
+ except Exception as e:
43
+ logger.error(f"Erreur lors du traitement d'un chunk: {e}")
44
+ logger.debug(f"Contenu du chunk problématique: {chunk}")
45
+ raise
46
+
47
+ except Exception as e:
48
+ logger.error(f"Erreur dans le streaming de la réponse: {e}")
49
+ raise
50
 
51
  def main():
52
  st.title("Analyseur d'Images Géométriques avec Gemini")
 
55
  try:
56
  api_key = st.secrets["GEMINI_API_KEY"]
57
  except Exception as e:
58
+ logger.error(f"Erreur dans la récupération des secrets: {e}")
59
+ st.error("Erreur: Impossible d'accéder aux secrets de l'application.")
60
  return
61
 
62
  # Initialisation du client
 
66
  http_options={'api_version':'v1alpha'}
67
  )
68
  except Exception as e:
69
+ logger.error(f"Erreur d'initialisation du client: {e}")
70
+ st.error(f"Erreur lors de l'initialisation du client Gemini: {e}")
71
  return
72
 
73
  # Upload de l'image
 
88
 
89
  with st.spinner("Analyse en cours..."):
90
  try:
91
+ # Préparation de la requête
92
+ request = {
93
+ "contents": [
94
+ {"image": image},
95
+ {"text": "What's the area of the overlapping region?"}
96
+ ]
97
+ }
98
+
99
+ logger.debug(f"Envoi de la requête au modèle: {request}")
100
+
101
  # Génération de la réponse en streaming
102
  response = client.models.generate_content_stream(
103
  model=model_name,
 
112
  stream_response(response_container, response)
113
 
114
  except Exception as e:
115
+ logger.error(f"Erreur lors de l'analyse: {e}", exc_info=True)
116
+ st.error(f"Erreur lors de l'analyse de l'image: {str(e)}")
117
 
118
  except Exception as e:
119
+ logger.error(f"Erreur lors du traitement de l'image: {e}", exc_info=True)
120
+ st.error(f"Erreur lors du traitement de l'image: {str(e)}")
121
 
122
  if __name__ == "__main__":
123
  main()