Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat add db nps #12

Merged
merged 14 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .replit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
modules = ["python-3.12"]
modules = ["python-3.12", "postgresql-16"]
run = "streamlit run --server.headless true main.py"

[nix]
Expand Down
49 changes: 49 additions & 0 deletions database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

import os
import psycopg2
from datetime import datetime

def init_db():
"""Initialize the database and create tables if needed"""
try:
conn = psycopg2.connect(os.environ['DATABASE_URL'])
cur = conn.cursor()

# Create feedback table
cur.execute('''
CREATE TABLE IF NOT EXISTS feedback (
id SERIAL PRIMARY KEY,
session_id TEXT,
search_term TEXT,
nps_score INTEGER,
email TEXT,
comments TEXT,
created_at TIMESTAMP
)
''')

conn.commit()
except Exception as e:
print(f"Database initialization error: {e}")
finally:
cur.close()
conn.close()

def save_feedback(session_id, search_term, nps_score=None, email=None, comments=None):
"""Save feedback to PostgreSQL database"""
try:
conn = psycopg2.connect(os.environ['DATABASE_URL'])
cur = conn.cursor()

cur.execute(
'''
INSERT INTO feedback (session_id, search_term, nps_score, email, comments, created_at)
VALUES (%s, %s, %s, %s, %s, %s)
''',
(session_id, search_term, nps_score, email, comments, datetime.now())
)

conn.commit()
finally:
cur.close()
conn.close()
45 changes: 29 additions & 16 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

"""
Situate Learning - A Streamlit app that generates higher-order thinking questions
using Groq's LLM API based on teacher input.
Expand Down Expand Up @@ -47,9 +46,13 @@ def generate_questions(lesson_text):
str: Generated questions from the LLM
"""
try:
system_msg = "You are an enthusiastic, curious teacher assistant creating thought-provoking questions."
user_msg = (f"Teacher: {lesson_text} Can you create some engaging, "
"higher-order thinking questions related to this topic? Include interdisciplinary questions.")
system_msg = ("You are an enthusiastic, curious teacher assistant "
"creating thought-provoking questions.")
user_msg = (
f"Teacher: {lesson_text} Can you create some engaging, "
"higher-order thinking questions related to this topic? "
"Include interdisciplinary questions."
)
messages = [
{"role": "system", "content": system_msg},
{"role": "user", "content": user_msg}
Expand Down Expand Up @@ -79,9 +82,7 @@ def copy_to_clipboard_script(response):
def main():
"""Main function to run the Streamlit app."""
initialize_session_state()

st.title("🌟 Situate Learning")

st.markdown("### What did you teach today?")
st.session_state.teacher_input = st.text_input(
"Enter today's lesson or topic:",
Expand All @@ -91,6 +92,10 @@ def main():

if st.button("Generate Questions"):
if st.session_state.teacher_input.strip():
# Log the search term immediately
from database import save_feedback
save_feedback(st.session_state.session_uuid, st.session_state.teacher_input)

st.session_state.ai_response = generate_questions(st.session_state.teacher_input)
st.markdown("### Higher-Order Thinking Questions:")
st.write(st.session_state.ai_response)
Expand All @@ -101,20 +106,28 @@ def main():
st_copy_to_clipboard(st.session_state.ai_response)
st.markdown("---")

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
)
from streamlit_star_rating import st_star_rating
st.markdown("### How helpful were these questions?")
stars = st_star_rating("", maxValue=5, defaultValue=5, key="rating")

if stars:
email = st.text_input("Email (optional)")
feedback = st.text_area("Additional comments (optional)")
if st.button("Submit"):
from database import save_feedback
save_feedback(st.session_state.session_uuid,
st.session_state.teacher_input,
stars,
email,
feedback)
st.success("Thanks for your feedback! 🌟")

st.markdown(
f"<div style='text-align: center; color: grey;'>Session ID: {st.session_state.session_uuid}</div>",
unsafe_allow_html=True
)

if __name__ == "__main__":
main()
from database import init_db
init_db()
main()
7 changes: 6 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
streamlit
groq
st-copy-to-clipboard
beautifulsoup4
beautifulsoup4
replit
replit
st-star-rating
psycopg2-binary
psycopg2-binary
12 changes: 9 additions & 3 deletions test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ def test_generate_questions(self, mock_groq):

# Verify Groq was called with correct parameters
expected_msg = [
{"role": "system", "content": "You are an enthusiastic, curious teacher assistant creating thought-provoking questions."},
{"role": "user", "content": "Teacher: test lesson Can you create some engaging, "
"higher-order thinking questions related to this topic? Include interdisciplinary questions."}
{
"role": "system",
"content": "You are an enthusiastic, curious teacher assistant creating thought-provoking questions."
},
{
"role": "user",
"content": "Teacher: test lesson Can you create some engaging, "
"higher-order thinking questions related to this topic? Include interdisciplinary questions."
}
]
mock_groq.return_value.chat.completions.create.assert_called_with(
model="llama-3.1-8b-instant",
Expand Down
Loading