Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import google.generativeai as genai | |
| import os | |
| MODEL_ID = "gemini-2.0-flash-exp" | |
| try: | |
| api_key = os.getenv("GEMINI_API_KEY") | |
| model_id = MODEL_ID | |
| genai.configure(api_key=api_key) | |
| except Exception as e: | |
| st.error(f"Error: {e}") | |
| st.stop() | |
| # Initialize conversation history in Streamlit session state | |
| if "conversation_history" not in st.session_state: | |
| st.session_state.conversation_history = [] | |
| # Initialize chat model outside the function to maintain state | |
| if "chat_model" not in st.session_state: | |
| st.session_state.chat_model = genai.GenerativeModel(MODEL_ID).start_chat(history=st.session_state.conversation_history) | |
| def basic_prompt(text_prompt): | |
| try: | |
| chat = st.session_state.chat_model #use the saved chat model. | |
| response = chat.send_message(text_prompt) | |
| # Update conversation history | |
| st.session_state.conversation_history.append({"role": "user", "parts": [text_prompt]}) #gemini uses parts now. | |
| st.session_state.conversation_history.append({"role": "model", "parts": [response.text]}) | |
| # Update the chat model's history | |
| st.session_state.chat_model = genai.GenerativeModel(MODEL_ID).start_chat(history=st.session_state.conversation_history) | |
| return response.text | |
| except Exception as e: | |
| return f"An error occurred: {e}" | |
| # App title | |
| st.title("Gemini Tutor: One-way ANOVA and two-way ANOVA") | |
| # About this App section | |
| with st.expander("About this App"): | |
| st.markdown(""" | |
| **Gemini AI-enabled Teaching Assistant** is an innovative application designed to unlock concepts in various subjects using the power of Gemini AI. | |
| Created by Louie F. Cervantes, M. Eng. (Information Engineering) (c) 2025. | |
| """) | |
| # Create two tabs | |
| st.write("Click the tabs to display more information.") | |
| tab1, tab2 = st.tabs(["Concept", "Application"]) | |
| # Concept tab | |
| with tab1: | |
| prompt = """Create a detailed explainer on ANOVA statistic and the difference between one-way and two-way ANOVA. | |
| The context is for senior high school students enrolled in a statistics course. | |
| Use brief and concise phrasing and do not add text like 'Okay let's start' or 'Here is the explanation'.""" | |
| if st.button("Explain"): | |
| with st.spinner("Gemini is thinking..."): | |
| response_text = basic_prompt(prompt) | |
| st.markdown(response_text) | |
| # Application tab | |
| with tab2: | |
| st.write("Click the button to generate a new example.") | |
| if st.button("Illustrate with an example"): | |
| with st.spinner("Gemini is thinking..."): | |
| prompt = """Create a real world example that uses either one-way or two-way ANOVA. | |
| Output only the text of the example do not add any other text. | |
| The problem should be different from the example you created in the previous step.""" | |
| problem = basic_prompt(prompt) | |
| st.markdown(problem) | |
| st.subheader("Explanation") | |
| prompt = """Discuss why one-way ANOVA or two-way ANOVA is applicable and not the other.""" | |
| solution = basic_prompt(prompt) | |
| st.markdown(solution) | |