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