Docfile commited on
Commit
7c881b7
·
verified ·
1 Parent(s): a512d67

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -61
app.py CHANGED
@@ -1,78 +1,57 @@
1
  import streamlit as st
2
- import os
3
  from google import genai
 
 
4
  from PIL import Image
5
- import io
6
- import base64
7
 
8
  # Page config
9
- st.set_page_config(page_title="Exercise Solver", layout="wide")
10
 
11
- # Initialize Gemini client
12
- GOOGLE_API_KEY = os.environ.get("GEMINI_API_KEY")
13
- client = genai.Client(
14
- api_key=GOOGLE_API_KEY,
15
- http_options={'api_version': 'v1alpha'},
16
- )
17
 
18
- def process_image(image):
19
- # Convert image to base64
20
- buffered = io.BytesIO()
21
- image.save(buffered, format="PNG")
22
- img_str = base64.b64encode(buffered.getvalue()).decode()
 
 
 
 
23
 
24
- # Initialize containers for thoughts and answers
25
- thoughts = []
26
- answers = []
27
 
28
- try:
 
 
 
 
 
29
  response = client.models.generate_content_stream(
30
- model="gemini-2.0-flash-thinking-exp-01-21",
31
  config={'thinking_config': {'include_thoughts': True}},
32
- contents=[
33
- {'inline_data': {'mime_type': 'image/png', 'data': img_str}},
34
- "Resous cette exercice. ça doit être bien présentable et espacé afin d'être facile à lire."
35
- ]
36
  )
37
 
 
 
 
38
  for chunk in response:
39
  for part in chunk.candidates[0].content.parts:
40
  if part.thought:
41
- thoughts.append(part.text)
 
 
 
 
 
42
  else:
43
- answers.append(part.text)
44
-
45
- return thoughts, answers
46
-
47
- except Exception as e:
48
- st.error(f"Une erreur s'est produite: {str(e)}")
49
- return [], []
50
-
51
- def main():
52
- st.title("Résolution d'Exercices")
53
-
54
- # File uploader
55
- uploaded_file = st.file_uploader("Choisissez une image", type=['png', 'jpg', 'jpeg'])
56
-
57
- if uploaded_file is not None:
58
- # Display the uploaded image
59
- image = Image.open(uploaded_file)
60
- st.image(image, caption="Image téléchargée", use_column_width=True)
61
-
62
- # Process button
63
- if st.button("Résoudre l'exercice"):
64
- with st.spinner("Traitement en cours..."):
65
- thoughts, answers = process_image(image)
66
-
67
- # Display thoughts in expander
68
- with st.expander("Voir le raisonnement"):
69
- for i, thought in enumerate(thoughts, 1):
70
- st.markdown(f"**Étape {i}:** {thought}")
71
-
72
- # Display answer
73
- st.markdown("### Solution:")
74
- for answer in answers:
75
- st.markdown(answer)
76
-
77
- if __name__ == "__main__":
78
- main()
 
1
  import streamlit as st
 
2
  from google import genai
3
+ from google.genai import types
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
+ # Process the response
40
  for chunk in response:
41
  for part in chunk.candidates[0].content.parts:
42
  if part.thought:
43
+ if mode != "thinking":
44
+ with thinking_container:
45
+ st.subheader("Thinking Process")
46
+ mode = "thinking"
47
+ with thinking_container:
48
+ st.write(part.text)
49
  else:
50
+ if mode != "answering":
51
+ with answer_container:
52
+ st.subheader("Generated Question")
53
+ mode = "answering"
54
+ with answer_container:
55
+ st.write(part.text)
56
+ else:
57
+ st.warning("Please enter your Google API key to start generating questions.")