Update app.py
Browse files
app.py
CHANGED
@@ -1,57 +1,69 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
3 |
-
|
4 |
-
import json
|
5 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
# Page config
|
8 |
-
st.set_page_config(page_title="Gemini API Physics Questions", layout="wide")
|
9 |
-
|
10 |
-
# Title
|
11 |
-
st.title("AP Physics C Practice Question Generator")
|
12 |
-
|
13 |
-
# Initialize the API key input
|
14 |
-
api_key = st.text_input("Enter your Google API Key", type="password")
|
15 |
-
|
16 |
-
if api_key:
|
17 |
-
# Initialize the client
|
18 |
-
client = genai.Client(
|
19 |
-
api_key=api_key,
|
20 |
-
http_options={'api_version': 'v1alpha'},
|
21 |
-
)
|
22 |
-
|
23 |
-
model_name = "gemini-2.0-flash-thinking-exp-01-21"
|
24 |
-
|
25 |
-
if st.button("Generate New Question"):
|
26 |
-
# Create containers for thinking and answer
|
27 |
-
thinking_container = st.container()
|
28 |
-
answer_container = st.container()
|
29 |
-
|
30 |
-
# Generate content
|
31 |
-
response = client.models.generate_content_stream(
|
32 |
-
model=model_name,
|
33 |
-
config={'thinking_config': {'include_thoughts': True}},
|
34 |
-
contents="Give me a practice question I can use for the AP Physics C exam?"
|
35 |
-
)
|
36 |
-
|
37 |
mode = 'starting'
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import google.generativeai as genai
|
3 |
+
import os
|
|
|
4 |
from PIL import Image
|
5 |
+
import io
|
6 |
+
import base64
|
7 |
+
import json
|
8 |
+
|
9 |
+
GOOGLE_API_KEY = os.environ.get("GEMINI_API_KEY")
|
10 |
+
|
11 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
12 |
+
client = genai.GenerativeModel("gemini-2.0-flash-thinking-exp-01-21")
|
13 |
+
client.http_options = {'api_version': 'v1alpha'}
|
14 |
+
|
15 |
+
|
16 |
+
st.title("Résolution d'exercice avec Gemini")
|
17 |
+
|
18 |
+
uploaded_file = st.file_uploader("Télécharger une image de l'exercice", type=["png", "jpg", "jpeg"])
|
19 |
+
|
20 |
+
if uploaded_file is not None:
|
21 |
+
image_data = uploaded_file.read()
|
22 |
+
img = Image.open(io.BytesIO(image_data))
|
23 |
+
|
24 |
+
buffered = io.BytesIO()
|
25 |
+
img.save(buffered, format="PNG")
|
26 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
27 |
+
|
28 |
+
if st.button("Résoudre l'exercice"):
|
29 |
+
response_area = st.empty() # Placeholder pour la réponse
|
30 |
+
mode_area = st.empty() # Placeholder pour le mode (thinking/answering)
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
mode = 'starting'
|
33 |
+
try:
|
34 |
+
responses = client.generate_content(
|
35 |
+
model="gemini-2.0-flash-thinking-exp-01-21",
|
36 |
+
generation_config=genai.types.GenerationConfig(
|
37 |
+
include_thoughts=True
|
38 |
+
),
|
39 |
+
contents=[
|
40 |
+
{'inline_data': {'mime_type': 'image/png', 'data': img_str}},
|
41 |
+
"Résous cet exercice. ça doit être bien présenté et espacé afin d'être facile à lire."
|
42 |
+
],
|
43 |
+
stream=True # Activer le streaming
|
44 |
+
)
|
45 |
+
|
46 |
+
full_response = "" # Pour accumuler toute la réponse pour un affichage final propre
|
47 |
+
|
48 |
+
for response in responses:
|
49 |
+
for part in response.parts:
|
50 |
+
if hasattr(part, 'thought') and part.thought: # Vérifier si la partie a un attribut 'thought' et s'il est True
|
51 |
+
if mode != "thinking":
|
52 |
+
mode_area.markdown("**Gemini réfléchit... 🤔**") # Afficher "thinking" dans le placeholder
|
53 |
+
mode = "thinking"
|
54 |
+
elif hasattr(part, 'text'): # Vérifier si la partie a un attribut 'text' (pour la réponse)
|
55 |
+
if mode != "answering":
|
56 |
+
mode_area.markdown("**Réponse de Gemini :**") # Afficher "answering" dans le placeholder
|
57 |
+
mode = "answering"
|
58 |
+
|
59 |
+
text_chunk = part.text or "" # Gérer le cas où part.text pourrait être None
|
60 |
+
full_response += text_chunk
|
61 |
+
response_area.markdown(full_response) # Mettre à jour la réponse en continu
|
62 |
+
|
63 |
+
mode_area.empty() # Effacer le message "thinking/answering" une fois terminé
|
64 |
+
st.success("Exercice résolu !") # Message de succès
|
65 |
+
|
66 |
+
except Exception as e:
|
67 |
+
print(f"Erreur lors de la génération: {e}")
|
68 |
+
mode_area.empty() # Effacer les messages de mode en cas d'erreur
|
69 |
+
response_area.error(f"Erreur: {e}") # Afficher l'erreur dans le placeholder de réponse
|