louiecerv
Save
ff2934d
import streamlit as st
import os
import pickle
# Define the file to store user preferences
PREFERENCES_FILE = "user_preferences.pkl"
def load_preferences():
"""Load preferences from the file."""
if os.path.exists(PREFERENCES_FILE):
with open(PREFERENCES_FILE, "rb") as f:
return pickle.load(f)
return {}
def save_preferences(preferences):
"""Save preferences to the file."""
with open(PREFERENCES_FILE, "wb") as f:
pickle.dump(preferences, f)
def save_user_preferences(username, topic, difficulty, num_items, preferences):
"""Save preferences for the user."""
preferences[username] = {
"topic": topic,
"difficulty": difficulty,
"num_items": num_items
}
save_preferences(preferences)
st.success("Preferences saved successfully!")
def go_to_quiz():
"""Callback to navigate to the quiz form."""
st.session_state["page"] = "quiz"
def go_to_result():
"""Callback to navigate to the result form."""
st.session_state["page"] = "result"
def go_to_main():
"""Callback to navigate to the main form."""
st.session_state["page"] = "main"
def display_username():
"""Display the username at the top of each page."""
if "username" in st.session_state:
st.sidebar.write(f"**User:** {st.session_state['username']}")
def main_form(preferences):
"""Main form where the user interacts with the AI."""
st.title("Main Form - Prompt AI")
# Ask for username
username = st.text_input("Enter your username:", placeholder="Username")
if username:
# Retrieve user-specific preferences if available
user_data = preferences.get(username, {
"topic": "",
"difficulty": "easy",
"num_items": 5
})
# Preference inputs
topic = st.text_input("Preferred Topic:", value=user_data["topic"], placeholder="Enter a topic")
difficulty = st.selectbox("Difficulty Level:", ["easy", "average", "hard"], index=["easy", "average", "hard"].index(user_data["difficulty"]))
num_items = st.selectbox("Number of Items:", [5, 10, 15], index=[5, 10, 15].index(user_data["num_items"]))
# Save and proceed button
st.button("Ask AI", on_click=lambda: save_user_preferences(username, topic, difficulty, num_items, preferences) or go_to_quiz())
def quiz_form():
"""Quiz form where the user takes the test."""
st.title("Quiz Form - Take the Test")
st.write("This is the quiz form. Add your quiz logic here.")
# Button to show results
st.button("Submit Quiz and Show Results", on_click=go_to_result)
def result_form():
"""Result form displayed after the quiz."""
st.title("Result Form - Quiz Results")
st.write("This is the result form. Add your result display logic here.")
# Button to return to main form
st.button("Back to Main Form", on_click=go_to_main)
def main():
"""Main app function."""
# Load preferences
preferences = load_preferences()
# Initialize session state
if "page" not in st.session_state:
st.session_state["page"] = "main"
# Display username
display_username()
# Render the appropriate page
if st.session_state["page"] == "main":
main_form(preferences)
elif st.session_state["page"] == "quiz":
quiz_form()
elif st.session_state["page"] == "result":
result_form()
if __name__ == "__main__":
main()