Spaces:
Sleeping
Sleeping
File size: 3,759 Bytes
020ec8e | 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 | import streamlit as st
from pages.gemini import query_gemini
# Read Instructions
with open("pages/instructions/model_instructions.txt", "r") as f:
model_instructions = f.read()
# Model Configuration
model_config = {
"system_instruction": model_instructions,
}
# Intialize session state for history
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
if "chat_button_clicked" not in st.session_state:
st.session_state.chat_button_clicked = False
# App Header
st.markdown(
f"""
<div style="text-align: center;">
<h2 style="margin-top: 10px;">What’s on your mind today?</h2>
</div>
<br>
<br>
""",
unsafe_allow_html=True,
)
# Display chat messages from history on app rerun
for message in st.session_state.chat_history:
with st.chat_message(message["role"]):
st.markdown(message["content"])
example_prompts = [
"Find the time complexity of merge sort",
"Explain how binary search trees work",
"Write code to implement stacks using arrays",
"What is the difference between DFS and BFS",
"Optimize a solution to find largest in array",
"What are pros and cons of linked lists vs arrays",
]
example_prompts_help = [
"Look for the time complexity of sorting algorithms",
"Search for how binary search trees operate and behave",
"Implement stacks using arrays in a specific language",
"Compare DFS and BFS based on traversal techniques",
"Optimize an approach using algorithms for maximum value",
"Compare pros and cons of linked lists versus arrays",
]
# Display buttons only if no button was clicked before
if not st.session_state.chat_button_clicked:
button_cols = st.columns(3)
button_cols_2 = st.columns(3)
# Check if any example prompt button is pressed
if button_cols[0].button(example_prompts[0], help=example_prompts_help[0]):
st.session_state.chat_button_clicked = True
st.session_state.prompt = example_prompts[0]
if button_cols[1].button(example_prompts[1], help=example_prompts_help[1]):
st.session_state.chat_button_clicked = True
st.session_state.prompt = example_prompts[1]
if button_cols[2].button(example_prompts[2], help=example_prompts_help[2]):
st.session_state.chat_button_clicked = True
st.session_state.prompt = example_prompts[2]
if button_cols_2[0].button(example_prompts[3], help=example_prompts_help[3]):
st.session_state.chat_button_clicked = True
st.session_state.prompt = example_prompts[3]
if button_cols_2[1].button(example_prompts[4], help=example_prompts_help[4]):
st.session_state.chat_button_clicked = True
st.session_state.prompt = example_prompts[4]
if button_cols_2[2].button(example_prompts[5], help=example_prompts_help[5]):
st.session_state.chat_button_clicked = True
st.session_state.prompt = example_prompts[5]
# Get users input
prompt = st.chat_input("Ask SocratiQ AI...") or st.session_state.get("prompt", "")
if prompt:
# Whether the user clciks or not the suggestions should vanish
st.session_state.chat_button_clicked = True
# Add user message to chat history
st.session_state.chat_history.append({"role": "user", "content": prompt})
# Display user message in chat message container
with st.chat_message("user"):
st.markdown(prompt)
# Display assistant message in chat message container
with st.chat_message("assistant"):
response = st.write_stream(
query_gemini(prompt, model_config, st.session_state.chat_history)
)
# Add assistant message to chat history
st.session_state.chat_history.append({"role": "assistant", "content": response})
st.session_state.prompt = ""
|