Spaces:
Sleeping
Sleeping
Save
Browse files- app.py +107 -0
- requirements.txt +1 -0
- user_preferences.pkl +3 -0
app.py
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import pickle
|
4 |
+
|
5 |
+
# Define the file to store user preferences
|
6 |
+
PREFERENCES_FILE = "user_preferences.pkl"
|
7 |
+
|
8 |
+
def load_preferences():
|
9 |
+
"""Load preferences from the file."""
|
10 |
+
if os.path.exists(PREFERENCES_FILE):
|
11 |
+
with open(PREFERENCES_FILE, "rb") as f:
|
12 |
+
return pickle.load(f)
|
13 |
+
return {}
|
14 |
+
|
15 |
+
def save_preferences(preferences):
|
16 |
+
"""Save preferences to the file."""
|
17 |
+
with open(PREFERENCES_FILE, "wb") as f:
|
18 |
+
pickle.dump(preferences, f)
|
19 |
+
|
20 |
+
def save_user_preferences(username, topic, difficulty, num_items, preferences):
|
21 |
+
"""Save preferences for the user."""
|
22 |
+
preferences[username] = {
|
23 |
+
"topic": topic,
|
24 |
+
"difficulty": difficulty,
|
25 |
+
"num_items": num_items
|
26 |
+
}
|
27 |
+
save_preferences(preferences)
|
28 |
+
st.success("Preferences saved successfully!")
|
29 |
+
|
30 |
+
def go_to_quiz():
|
31 |
+
"""Callback to navigate to the quiz form."""
|
32 |
+
st.session_state["page"] = "quiz"
|
33 |
+
|
34 |
+
def go_to_result():
|
35 |
+
"""Callback to navigate to the result form."""
|
36 |
+
st.session_state["page"] = "result"
|
37 |
+
|
38 |
+
def go_to_main():
|
39 |
+
"""Callback to navigate to the main form."""
|
40 |
+
st.session_state["page"] = "main"
|
41 |
+
|
42 |
+
def display_username():
|
43 |
+
"""Display the username at the top of each page."""
|
44 |
+
if "username" in st.session_state:
|
45 |
+
st.sidebar.write(f"**User:** {st.session_state['username']}")
|
46 |
+
|
47 |
+
def main_form(preferences):
|
48 |
+
"""Main form where the user interacts with the AI."""
|
49 |
+
st.title("Main Form - Prompt AI")
|
50 |
+
|
51 |
+
# Ask for username
|
52 |
+
username = st.text_input("Enter your username:", placeholder="Username")
|
53 |
+
|
54 |
+
if username:
|
55 |
+
# Retrieve user-specific preferences if available
|
56 |
+
user_data = preferences.get(username, {
|
57 |
+
"topic": "",
|
58 |
+
"difficulty": "easy",
|
59 |
+
"num_items": 5
|
60 |
+
})
|
61 |
+
|
62 |
+
# Preference inputs
|
63 |
+
topic = st.text_input("Preferred Topic:", value=user_data["topic"], placeholder="Enter a topic")
|
64 |
+
difficulty = st.selectbox("Difficulty Level:", ["easy", "average", "hard"], index=["easy", "average", "hard"].index(user_data["difficulty"]))
|
65 |
+
num_items = st.selectbox("Number of Items:", [5, 10, 15], index=[5, 10, 15].index(user_data["num_items"]))
|
66 |
+
|
67 |
+
# Save and proceed button
|
68 |
+
st.button("Ask AI", on_click=lambda: save_user_preferences(username, topic, difficulty, num_items, preferences) or go_to_quiz())
|
69 |
+
|
70 |
+
def quiz_form():
|
71 |
+
"""Quiz form where the user takes the test."""
|
72 |
+
st.title("Quiz Form - Take the Test")
|
73 |
+
st.write("This is the quiz form. Add your quiz logic here.")
|
74 |
+
|
75 |
+
# Button to show results
|
76 |
+
st.button("Submit Quiz and Show Results", on_click=go_to_result)
|
77 |
+
|
78 |
+
def result_form():
|
79 |
+
"""Result form displayed after the quiz."""
|
80 |
+
st.title("Result Form - Quiz Results")
|
81 |
+
st.write("This is the result form. Add your result display logic here.")
|
82 |
+
|
83 |
+
# Button to return to main form
|
84 |
+
st.button("Back to Main Form", on_click=go_to_main)
|
85 |
+
|
86 |
+
def main():
|
87 |
+
"""Main app function."""
|
88 |
+
# Load preferences
|
89 |
+
preferences = load_preferences()
|
90 |
+
|
91 |
+
# Initialize session state
|
92 |
+
if "page" not in st.session_state:
|
93 |
+
st.session_state["page"] = "main"
|
94 |
+
|
95 |
+
# Display username
|
96 |
+
display_username()
|
97 |
+
|
98 |
+
# Render the appropriate page
|
99 |
+
if st.session_state["page"] == "main":
|
100 |
+
main_form(preferences)
|
101 |
+
elif st.session_state["page"] == "quiz":
|
102 |
+
quiz_form()
|
103 |
+
elif st.session_state["page"] == "result":
|
104 |
+
result_form()
|
105 |
+
|
106 |
+
if __name__ == "__main__":
|
107 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
streamlit
|
user_preferences.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8181ccbb2b01f5cadde2bb50f1ce16579f996e4e5746cec51534b1dfc1d69ed0
|
3 |
+
size 177
|