import streamlit as st import os from google import genai from PIL import Image import io import base64 # Page config st.set_page_config(page_title="Exercise Solver", layout="wide") # Initialize Gemini client GOOGLE_API_KEY = os.environ.get("GEMINI_API_KEY") client = genai.Client( api_key=GOOGLE_API_KEY, http_options={'api_version': 'v1alpha'}, ) def process_image(image): # Convert image to base64 buffered = io.BytesIO() image.save(buffered, format="PNG") img_str = base64.b64encode(buffered.getvalue()).decode() # Initialize containers for thoughts and answers thoughts = [] answers = [] try: response = client.models.generate_content_stream( model="gemini-2.0-flash-thinking-exp-01-21", config={'thinking_config': {'include_thoughts': True}}, contents=[ {'inline_data': {'mime_type': 'image/png', 'data': img_str}}, "Resous cette exercice. ça doit être bien présentable et espacé afin d'être facile à lire." ] ) for chunk in response: for part in chunk.candidates[0].content.parts: if part.thought: thoughts.append(part.text) else: answers.append(part.text) return thoughts, answers except Exception as e: st.error(f"Une erreur s'est produite: {str(e)}") return [], [] def main(): st.title("Résolution d'Exercices") # File uploader uploaded_file = st.file_uploader("Choisissez une image", type=['png', 'jpg', 'jpeg']) if uploaded_file is not None: # Display the uploaded image image = Image.open(uploaded_file) st.image(image, caption="Image téléchargée", use_column_width=True) # Process button if st.button("Résoudre l'exercice"): with st.spinner("Traitement en cours..."): thoughts, answers = process_image(image) # Display thoughts in expander with st.expander("Voir le raisonnement"): for i, thought in enumerate(thoughts, 1): st.markdown(f"**Étape {i}:** {thought}") # Display answer st.markdown("### Solution:") for answer in answers: st.markdown(answer) if __name__ == "__main__": main()