Spaces:
Sleeping
Sleeping
feat: add main.py
Browse files
app.py
ADDED
@@ -0,0 +1,177 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from groq import Groq
|
3 |
+
import streamlit.components.v1 as components
|
4 |
+
from st_copy_to_clipboard import st_copy_to_clipboard
|
5 |
+
import urllib.parse
|
6 |
+
import os
|
7 |
+
import uuid
|
8 |
+
import html
|
9 |
+
|
10 |
+
# Streamlit page configuration
|
11 |
+
st.set_page_config(
|
12 |
+
page_title="Situate Learning",
|
13 |
+
page_icon="π",
|
14 |
+
layout="centered"
|
15 |
+
)
|
16 |
+
|
17 |
+
|
18 |
+
# Function to inject Google Analytics using st.components.v1.html
|
19 |
+
def inject_ga():
|
20 |
+
try:
|
21 |
+
GA_MEASUREMENT_ID = st.secrets["google_analytics"]["measurement_id"]
|
22 |
+
|
23 |
+
# Define the Google Analytics script
|
24 |
+
GA_SCRIPT = f"""
|
25 |
+
<!-- Google tag (gtag.js) -->
|
26 |
+
<script async src="https://www.googletagmanager.com/gtag/js?id={GA_MEASUREMENT_ID}"></script>
|
27 |
+
<script>
|
28 |
+
window.dataLayer = window.dataLayer || [];
|
29 |
+
function gtag(){{dataLayer.push(arguments);}}
|
30 |
+
gtag('js', new Date());
|
31 |
+
gtag('config', '{GA_MEASUREMENT_ID}');
|
32 |
+
</script>
|
33 |
+
"""
|
34 |
+
|
35 |
+
# Inject the script into the app
|
36 |
+
components.html(GA_SCRIPT, height=0)
|
37 |
+
except KeyError:
|
38 |
+
st.error("Google Analytics measurement ID not found in secrets.")
|
39 |
+
|
40 |
+
|
41 |
+
# Inject Google Analytics dynamically
|
42 |
+
inject_ga()
|
43 |
+
|
44 |
+
# Initialize the Groq client
|
45 |
+
GROQ_API_KEY = st.secrets["GROQ_API_KEY"]
|
46 |
+
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
|
47 |
+
client = Groq()
|
48 |
+
|
49 |
+
if "session_uuid" not in st.session_state:
|
50 |
+
st.session_state.update({
|
51 |
+
"session_uuid": str(uuid.uuid4()),
|
52 |
+
"teacher_input": "",
|
53 |
+
"ai_response": "",
|
54 |
+
"ga_initialized": False
|
55 |
+
})
|
56 |
+
|
57 |
+
|
58 |
+
def log_event_to_ga(event_name, event_label="", value=""):
|
59 |
+
event_script = f"""
|
60 |
+
<script>
|
61 |
+
gtag('event', '{event_name}', {{
|
62 |
+
'event_category': 'User Interaction',
|
63 |
+
'event_label': '{html.escape(event_label)}',
|
64 |
+
'value': '{html.escape(value)}'
|
65 |
+
}});
|
66 |
+
</script>
|
67 |
+
"""
|
68 |
+
if not st.session_state.get("ga_event_logged", False):
|
69 |
+
st.markdown(event_script, unsafe_allow_html=True)
|
70 |
+
st.session_state.ga_event_logged = True
|
71 |
+
|
72 |
+
|
73 |
+
# Function to display the help modal
|
74 |
+
# @st.dialog("Get Started", width="small")
|
75 |
+
# def help_modal():
|
76 |
+
# st.write("Not braining today? Type in a simple concept to trigger a list of questions to better frame or close the lesson.")
|
77 |
+
# st.markdown("For example:")
|
78 |
+
# st.markdown("- *photosynthesis*")
|
79 |
+
# st.markdown("- *algebra basics*")
|
80 |
+
# st.markdown("- *moments (physics)*")
|
81 |
+
# st.markdown("- *acids, salts and bases (chemistry)*")
|
82 |
+
# st.markdown("- *price elasticity of demand*")
|
83 |
+
# if st.button("Close Help"):
|
84 |
+
# st.rerun() # Close the modal by triggering a script rerun
|
85 |
+
|
86 |
+
# Streamlit Page Title
|
87 |
+
st.title("π Situate Learning")
|
88 |
+
|
89 |
+
# # Help/Get Started Button
|
90 |
+
# if st.button("Help / Get Started"):
|
91 |
+
# help_modal() # Open the help modal
|
92 |
+
|
93 |
+
# Teacher input field
|
94 |
+
st.markdown("### What did you teach today?")
|
95 |
+
st.session_state.teacher_input = st.text_input(
|
96 |
+
"Enter today's lesson or topic:",
|
97 |
+
value=st.session_state.teacher_input,
|
98 |
+
placeholder="e.g. photosynthesis or quadratic equations"
|
99 |
+
)
|
100 |
+
|
101 |
+
# Function to generate higher-order thinking questions based on the lesson
|
102 |
+
def generate_questions(lesson_text):
|
103 |
+
"""Generate higher-order thinking questions using Groq and Llama."""
|
104 |
+
try:
|
105 |
+
messages = [
|
106 |
+
{"role": "system", "content": "You are an enthusiastic, curious teacher assistant creating thought-provoking questions."},
|
107 |
+
{"role": "user", "content": f"Teacher: {lesson_text} Can you create some engaging, higher-order thinking questions related to this topic? Include interdisciplinary questions."}
|
108 |
+
]
|
109 |
+
response = client.chat.completions.create(
|
110 |
+
model="llama-3.1-8b-instant",
|
111 |
+
messages=messages
|
112 |
+
)
|
113 |
+
ai_response = response.choices[0].message.content.strip()
|
114 |
+
return ai_response
|
115 |
+
except Exception as e:
|
116 |
+
st.error(f"An error occurred: {e}")
|
117 |
+
return "Sorry, we couldn't generate questions. Please try again later."
|
118 |
+
|
119 |
+
|
120 |
+
# Button to generate questions
|
121 |
+
if st.button("Generate Questions"):
|
122 |
+
if st.session_state.teacher_input.strip():
|
123 |
+
# Log the user input to GA
|
124 |
+
log_event_to_ga(st.session_state.teacher_input)
|
125 |
+
|
126 |
+
# Generate higher-order thinking questions based on teacher's input
|
127 |
+
st.session_state.ai_response = generate_questions(st.session_state.teacher_input)
|
128 |
+
st.markdown(f"### Higher-Order Thinking Questions:")
|
129 |
+
st.write(st.session_state.ai_response)
|
130 |
+
else:
|
131 |
+
st.warning("Please provide a topic or lesson before submitting.")
|
132 |
+
|
133 |
+
# Function to copy response to clipboard
|
134 |
+
def copy_to_clipboard_script(response):
|
135 |
+
sanitized_response = html.escape(response).replace("\n", "\\n").replace("\r", "\\r")
|
136 |
+
return f"""
|
137 |
+
<script>
|
138 |
+
function copyToClipboard() {{
|
139 |
+
navigator.clipboard.writeText("{sanitized_response}")
|
140 |
+
.then(() => {{
|
141 |
+
alert('Copied to clipboard!');
|
142 |
+
}})
|
143 |
+
.catch(err => {{
|
144 |
+
console.error('Failed to copy: ', err);
|
145 |
+
}});
|
146 |
+
}}
|
147 |
+
</script>
|
148 |
+
"""
|
149 |
+
|
150 |
+
|
151 |
+
# Footer and Feedback Section
|
152 |
+
|
153 |
+
# Check if AI response exists
|
154 |
+
if st.session_state.ai_response:
|
155 |
+
# Add the Copy to Clipboard button
|
156 |
+
st_copy_to_clipboard(
|
157 |
+
st.session_state.ai_response, # Text to copy
|
158 |
+
)
|
159 |
+
st.markdown("---")
|
160 |
+
|
161 |
+
|
162 |
+
# Feedback Link
|
163 |
+
st.markdown(
|
164 |
+
"""
|
165 |
+
<div style='text-align: center;'>
|
166 |
+
<a href="https://leekahhow.notion.site/14ac34bc89df803fbb5fc9b2922a62ea?pvs=105" target="_blank">
|
167 |
+
Provide Feedback
|
168 |
+
</a>
|
169 |
+
</div>
|
170 |
+
""", unsafe_allow_html=True
|
171 |
+
)
|
172 |
+
|
173 |
+
# Footer with Session ID
|
174 |
+
st.markdown(
|
175 |
+
f"<div style='text-align: center; color: grey;'>Session ID: {st.session_state.session_uuid}</div>",
|
176 |
+
unsafe_allow_html=True
|
177 |
+
)
|