Docfile commited on
Commit
17312e6
·
verified ·
1 Parent(s): 800e5c7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from google import genai
3
+ from google.genai import types
4
+ from PIL import Image
5
+ import io
6
+ import base64
7
+ import time
8
+ import json
9
+
10
+ # Configuration de l'API Gemini
11
+ GOOGLE_API_KEY = "YOUR_API_KEY" # Remplacez par votre clé API
12
+ genai.configure(api_key=GOOGLE_API_KEY)
13
+ client = genai.GenerativeModel('gemini-pro-vision')
14
+
15
+ # Fonction pour générer la réponse en streaming
16
+ def solve_math_problem(image_data):
17
+ img = Image.open(io.BytesIO(image_data))
18
+
19
+ # Convertir l'image en base64 pour l'envoyer à l'API Gemini
20
+ buffered = io.BytesIO()
21
+ img.save(buffered, format="PNG")
22
+ img_str = base64.b64encode(buffered.getvalue()).decode()
23
+
24
+ # Créer le contenu de la requête pour l'API Gemini
25
+ contents = [
26
+ {'parts': [{'mime_type': 'image/png', 'data': img_str}]},
27
+ {'parts': [{'text': "Résous ce problème?"}]},
28
+ ]
29
+
30
+ # Configuration pour inclure les pensées (si disponible)
31
+ config = {
32
+ 'thinking_config': {'include_thoughts': True}
33
+ }
34
+
35
+ response_stream = client.generate_content(
36
+ contents=contents,
37
+ model="gemini-2.0-flash-thinking-exp-01-21",
38
+ stream=True,
39
+ generation_config=config
40
+ )
41
+
42
+ for chunk in response_stream:
43
+ for part in chunk.parts:
44
+ if part.text:
45
+ yield part.text
46
+
47
+ # Interface Streamlit
48
+ st.set_page_config(page_title="Mariam M-0", page_icon="🧮", layout="centered")
49
+
50
+ st.title("Mariam M-0")
51
+ st.subheader("Solution Mathématique Intelligente")
52
+
53
+ uploaded_file = st.file_uploader("Déposez votre image ici", type=["jpg", "jpeg", "png"])
54
+
55
+ if uploaded_file is not None:
56
+ image_data = uploaded_file.getvalue()
57
+ st.image(image_data, caption="Image téléchargée.", use_column_width=True)
58
+
59
+ if st.button("Résoudre le problème"):
60
+ with st.spinner("Analyse en cours..."):
61
+ # Utilisation d'un conteneur pour mettre à jour le texte en streaming
62
+ thoughts_container = st.empty()
63
+ answer_container = st.empty()
64
+ full_thoughts = ""
65
+ full_answer = ""
66
+
67
+ for response_text in solve_math_problem(image_data):
68
+ try:
69
+ response_json = json.loads(response_text)
70
+ # Vérifier si la réponse contient une clé 'thoughts' ou 'answer'
71
+ if 'thoughts' in response_json:
72
+ thoughts_content = response_json['thoughts']
73
+ full_thoughts += thoughts_content + " \n"
74
+ thoughts_container.markdown(f"**Processus de Réflexion:**\n\n{full_thoughts}")
75
+ elif 'answer' in response_json:
76
+ answer_content = response_json['answer']
77
+ full_answer += answer_content + " \n"
78
+ answer_container.markdown(f"**Solution:**\n\n{full_answer}")
79
+ except json.JSONDecodeError:
80
+ print(f"Could not parse as JSON: {response_text}")
81
+ continue
82
+
83
+
84
+ st.success("Problème résolu!")