Docfile commited on
Commit
a512d67
·
verified ·
1 Parent(s): cb25963

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -54
app.py CHANGED
@@ -1,78 +1,78 @@
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
 
9
- # Configuration de la page
10
- st.set_page_config(page_title="Résolveur d'exercices")
11
 
12
- # Configuration de l'API Gemini
13
- GOOGLE_API_KEY = "AIzaSyC_zxN9IHjEAxIoshWPzMfgb9qwMsu5t5Y"
14
  client = genai.Client(
15
  api_key=GOOGLE_API_KEY,
16
  http_options={'api_version': 'v1alpha'},
17
  )
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  def main():
20
- st.title("Résolveur d'exercices")
21
 
22
- # Zone de téléchargement d'image
23
- uploaded_file = st.file_uploader("Téléchargez votre exercice", type=["png", "jpg", "jpeg"])
24
 
25
- if uploaded_file:
26
- # Affichage de l'image
27
  image = Image.open(uploaded_file)
28
  st.image(image, caption="Image téléchargée", use_column_width=True)
29
 
30
- # Bouton pour lancer l'analyse
31
  if st.button("Résoudre l'exercice"):
32
- # Conversion de l'image en base64
33
- buffered = io.BytesIO()
34
- image.save(buffered, format="PNG")
35
- img_str = base64.b64encode(buffered.getvalue()).decode()
36
-
37
- # Création de deux colonnes : une pour les pensées, une pour la réponse
38
- thoughts_col, answer_col = st.columns([1, 2])
39
-
40
- with thoughts_col:
41
- # Création d'un expander pour les pensées
42
- with st.expander("Pensées du modèle", expanded=True):
43
- thoughts_placeholder = st.empty()
44
-
45
- with answer_col:
46
- # Placeholder pour la réponse
47
- answer_placeholder = st.empty()
48
-
49
- try:
50
- # Génération du contenu avec streaming
51
- response = client.models.generate_content_stream(
52
- model="gemini-2.0-flash-thinking-exp-01-21",
53
- config={'thinking_config': {'include_thoughts': True}},
54
- contents=[
55
- {'inline_data': {'mime_type': 'image/png', 'data': img_str}},
56
- "Résous cet exercice. ça doit être bien présentable et espacé afin d'être facile à lire."
57
- ]
58
- )
59
-
60
- # Variables pour accumuler le contenu
61
- thoughts = []
62
- answer = []
63
 
64
- # Traitement du stream
65
- for chunk in response:
66
- for part in chunk.candidates[0].content.parts:
67
- if part.thought:
68
- thoughts.append(part.text)
69
- thoughts_placeholder.markdown("\n\n".join(thoughts))
70
- else:
71
- answer.append(part.text)
72
- answer_placeholder.markdown("\n\n".join(answer))
73
 
74
- except Exception as e:
75
- st.error(f"Une erreur s'est produite : {str(e)}")
 
 
76
 
77
  if __name__ == "__main__":
78
  main()
 
1
  import streamlit as st
 
 
2
  import os
3
+ from google import genai
4
  from PIL import Image
5
  import io
6
  import base64
7
 
8
+ # Page config
9
+ st.set_page_config(page_title="Exercise Solver", layout="wide")
10
 
11
+ # Initialize Gemini client
12
+ GOOGLE_API_KEY = os.environ.get("GEMINI_API_KEY")
13
  client = genai.Client(
14
  api_key=GOOGLE_API_KEY,
15
  http_options={'api_version': 'v1alpha'},
16
  )
17
 
18
+ def process_image(image):
19
+ # Convert image to base64
20
+ buffered = io.BytesIO()
21
+ image.save(buffered, format="PNG")
22
+ img_str = base64.b64encode(buffered.getvalue()).decode()
23
+
24
+ # Initialize containers for thoughts and answers
25
+ thoughts = []
26
+ answers = []
27
+
28
+ try:
29
+ response = client.models.generate_content_stream(
30
+ model="gemini-2.0-flash-thinking-exp-01-21",
31
+ config={'thinking_config': {'include_thoughts': True}},
32
+ contents=[
33
+ {'inline_data': {'mime_type': 'image/png', 'data': img_str}},
34
+ "Resous cette exercice. ça doit être bien présentable et espacé afin d'être facile à lire."
35
+ ]
36
+ )
37
+
38
+ for chunk in response:
39
+ for part in chunk.candidates[0].content.parts:
40
+ if part.thought:
41
+ thoughts.append(part.text)
42
+ else:
43
+ answers.append(part.text)
44
+
45
+ return thoughts, answers
46
+
47
+ except Exception as e:
48
+ st.error(f"Une erreur s'est produite: {str(e)}")
49
+ return [], []
50
+
51
  def main():
52
+ st.title("Résolution d'Exercices")
53
 
54
+ # File uploader
55
+ uploaded_file = st.file_uploader("Choisissez une image", type=['png', 'jpg', 'jpeg'])
56
 
57
+ if uploaded_file is not None:
58
+ # Display the uploaded image
59
  image = Image.open(uploaded_file)
60
  st.image(image, caption="Image téléchargée", use_column_width=True)
61
 
62
+ # Process button
63
  if st.button("Résoudre l'exercice"):
64
+ with st.spinner("Traitement en cours..."):
65
+ thoughts, answers = process_image(image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
+ # Display thoughts in expander
68
+ with st.expander("Voir le raisonnement"):
69
+ for i, thought in enumerate(thoughts, 1):
70
+ st.markdown(f"**Étape {i}:** {thought}")
 
 
 
 
 
71
 
72
+ # Display answer
73
+ st.markdown("### Solution:")
74
+ for answer in answers:
75
+ st.markdown(answer)
76
 
77
  if __name__ == "__main__":
78
  main()