with col_export:
if st.session_state.history:
# Prepare conversation text for export
export_text = ""
for idx, qa in enumerate(st.session_state.history, start=1):
export_text += f"Q{idx}: {qa['question']}\n"
export_text += f"A{idx}: {qa['answer']}\n\n"
bio = io.BytesIO()
bio.write(export_text.encode("utf-8"))
bio.seek(0)
st.download_button(
label="📥 Export Chat History",
data=bio,
file_name="AI_Teaching_Assistant_Conversation.txt",
mime="text/plain",
)
# User input
user_input = st.text_input("Enter your question here:")
if st.button("Ask"):
if user_input.strip():
with st.spinner("Generating AI response..."):
response = generate_response(user_input.strip())
st.session_state.history.append({"question": user_input.strip(), "answer": response})
else:
st.warning("⚠️ Please enter a question before clicking Ask.")
# Display conversation history in scrollable container
st.markdown("### Conversation History")
st.markdown(
"""
<style>
.history-box {
max-height: 400px;
overflow-y: auto;
border: 1px solid #ddd;
padding: 12px;
background-color: #f9f9f9;
border-radius: 6px;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.question {
font-weight: 600;
color: #0a6ebd;
margin-top: 12px;
margin-bottom: 4px;
}
.answer {
margin-bottom: 16px;
white-space: pre-wrap;
color: #333;
}
</style>
""",
unsafe_allow_html=True,
)
history_html = '<div class="history-box">'
for idx, qa in enumerate(st.session_state.history, start=1):
q = qa["question"]
a = qa["answer"]
history_html += f'<div class="question">Q{idx}: {q}</div>'
history_html += f'<div class="answer">A{idx}: {a}</div>'
history_html += '</div>'
st.markdown(history_html, unsafe_allow_html=True)
def main():
setup_ui()
if __name__ == "__main__":
main()
To embed this project on your website, copy the following code and paste it into your website's HTML: