|
import streamlit as st |
|
from google import genai |
|
from google.genai import types |
|
import os |
|
from PIL import Image |
|
import io |
|
import base64 |
|
|
|
|
|
st.set_page_config(page_title="Résolveur d'exercices") |
|
|
|
|
|
GOOGLE_API_KEY = "AIzaSyC_zxN9IHjEAxIoshWPzMfgb9qwMsu5t5Y" |
|
client = genai.Client( |
|
api_key=GOOGLE_API_KEY, |
|
http_options={'api_version': 'v1alpha'}, |
|
) |
|
|
|
def main(): |
|
st.title("Résolveur d'exercices") |
|
|
|
|
|
uploaded_file = st.file_uploader("Téléchargez votre exercice", type=["png", "jpg", "jpeg"]) |
|
|
|
if uploaded_file: |
|
|
|
image = Image.open(uploaded_file) |
|
st.image(image, caption="Image téléchargée", use_column_width=True) |
|
|
|
|
|
if st.button("Résoudre l'exercice"): |
|
|
|
buffered = io.BytesIO() |
|
image.save(buffered, format="PNG") |
|
img_str = base64.b64encode(buffered.getvalue()).decode() |
|
|
|
|
|
thoughts_col, answer_col = st.columns([1, 2]) |
|
|
|
with thoughts_col: |
|
|
|
with st.expander("Pensées du modèle", expanded=True): |
|
thoughts_placeholder = st.empty() |
|
|
|
with answer_col: |
|
|
|
answer_placeholder = st.empty() |
|
|
|
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}}, |
|
"Résous cet exercice. ça doit être bien présentable et espacé afin d'être facile à lire." |
|
] |
|
) |
|
|
|
|
|
thoughts = [] |
|
answer = [] |
|
|
|
|
|
for chunk in response: |
|
for part in chunk.candidates[0].content.parts: |
|
if part.thought: |
|
thoughts.append(part.text) |
|
thoughts_placeholder.markdown("\n\n".join(thoughts)) |
|
else: |
|
answer.append(part.text) |
|
answer_placeholder.markdown("\n\n".join(answer)) |
|
|
|
except Exception as e: |
|
st.error(f"Une erreur s'est produite : {str(e)}") |
|
|
|
if __name__ == "__main__": |
|
main() |