File size: 2,492 Bytes
17312e6
cb25963
a512d67
17312e6
 
 
 
a512d67
 
17312e6
a512d67
 
cb25963
 
 
 
17312e6
a512d67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb25963
a512d67
cb25963
a512d67
 
cb25963
a512d67
 
cb25963
 
 
a512d67
cb25963
a512d67
 
cb25963
a512d67
 
 
 
cb25963
a512d67
 
 
 
cb25963
 
 
1
2
3
4
5
6
7
8
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
34
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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()