Docfile commited on
Commit
2758e4e
·
verified ·
1 Parent(s): 603f541

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -13
app.py CHANGED
@@ -4,15 +4,45 @@ 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
  # Utilisation des secrets pour la clé API
11
  try:
12
- api_key = st.secrets["GEMINI_API_KEY"]
13
  except Exception as e:
14
- st.error("Erreur: .")
15
-
 
 
 
16
  return
17
 
18
  # Initialisation du client
@@ -38,10 +68,13 @@ def main():
38
  model_name = "gemini-2.0-flash-thinking-exp-01-21"
39
 
40
  if st.button("Analyser l'image"):
 
 
 
41
  with st.spinner("Analyse en cours..."):
42
  try:
43
- # Génération de la réponse
44
- response = client.models.generate_content(
45
  model=model_name,
46
  config={'thinking_config': {'include_thoughts': True}},
47
  contents=[
@@ -50,14 +83,8 @@ def main():
50
  ]
51
  )
52
 
53
- # Affichage des résultats
54
- for part in response.candidates[0].content.parts:
55
- if part.thought:
56
- with st.expander("Voir le raisonnement"):
57
- st.markdown(part.text)
58
- else:
59
- st.subheader("Réponse")
60
- st.markdown(part.text)
61
 
62
  except Exception as e:
63
  st.error(f"Erreur lors de l'analyse: {e}")
 
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"""
9
+ mode = 'starting'
10
+ thinking_placeholder = None
11
+ answer_placeholder = None
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")
36
 
37
  # Utilisation des secrets pour la clé API
38
  try:
39
+ api_key = st.secrets["GOOGLE_API_KEY"]
40
  except Exception as e:
41
+ st.error("Erreur: La clé API Google n'est pas configurée dans les secrets.")
42
+ st.info("Créez un fichier .streamlit/secrets.toml avec le contenu suivant:")
43
+ st.code("""
44
+ GOOGLE_API_KEY = "votre_clé_api"
45
+ """)
46
  return
47
 
48
  # Initialisation du client
 
68
  model_name = "gemini-2.0-flash-thinking-exp-01-21"
69
 
70
  if st.button("Analyser l'image"):
71
+ # Création d'un conteneur pour la réponse en streaming
72
+ response_container = st.container()
73
+
74
  with st.spinner("Analyse en cours..."):
75
  try:
76
+ # Génération de la réponse en streaming
77
+ response = client.models.generate_content_stream(
78
  model=model_name,
79
  config={'thinking_config': {'include_thoughts': True}},
80
  contents=[
 
83
  ]
84
  )
85
 
86
+ # Streaming de la réponse
87
+ stream_response(response_container, response)
 
 
 
 
 
 
88
 
89
  except Exception as e:
90
  st.error(f"Erreur lors de l'analyse: {e}")