|
import streamlit as st |
|
from google import genai |
|
from google.genai import types |
|
import json |
|
from PIL import Image |
|
|
|
|
|
st.set_page_config(page_title="Gemini API Physics Questions", layout="wide") |
|
|
|
|
|
st.title("AP Physics C Practice Question Generator") |
|
|
|
|
|
api_key = st.text_input("Enter your Google API Key", type="password") |
|
|
|
if api_key: |
|
|
|
client = genai.Client( |
|
api_key=api_key, |
|
http_options={'api_version': 'v1alpha'}, |
|
) |
|
|
|
model_name = "gemini-2.0-flash-thinking-exp-01-21" |
|
|
|
if st.button("Generate New Question"): |
|
|
|
thinking_container = st.container() |
|
answer_container = st.container() |
|
|
|
|
|
response = client.models.generate_content_stream( |
|
model=model_name, |
|
config={'thinking_config': {'include_thoughts': True}}, |
|
contents="Give me a practice question I can use for the AP Physics C exam?" |
|
) |
|
|
|
mode = 'starting' |
|
|
|
|
|
for chunk in response: |
|
for part in chunk.candidates[0].content.parts: |
|
if part.thought: |
|
if mode != "thinking": |
|
with thinking_container: |
|
st.subheader("Thinking Process") |
|
mode = "thinking" |
|
with thinking_container: |
|
st.write(part.text) |
|
else: |
|
if mode != "answering": |
|
with answer_container: |
|
st.subheader("Generated Question") |
|
mode = "answering" |
|
with answer_container: |
|
st.write(part.text) |
|
else: |
|
st.warning("Please enter your Google API key to start generating questions.") |