FYP-2 / app.py
Saim-11's picture
Update app.py
e3ea6f4 verified
import streamlit as st
from constitution_py import get_legal_response # βœ… importing from constitution.py
# Title
st.markdown("<h1 style='text-align: center; color: green;'>PakLegalAI</h1>", unsafe_allow_html=True)
st.markdown("<hr>", unsafe_allow_html=True)
# Chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Show chat history
for msg in st.session_state.messages:
with st.chat_message(msg["role"]):
st.markdown(msg["content"])
# Automatically scroll to bottom with custom JavaScript
st.markdown(
"""
<script>
const messages = window.parent.document.querySelector('div.stApp').querySelectorAll('.chat-message');
messages[messages.length - 1].scrollIntoView({ behavior: 'smooth' });
</script>
""", unsafe_allow_html=True)
# Chat input
user_input = st.chat_input("Ask a legal question about Pakistan's constitution...")
if user_input:
st.session_state.messages.append({"role": "user", "content": user_input})
with st.chat_message("user"):
st.markdown(user_input)
# βœ… Real response from your logic
try:
response = get_legal_response(user_input)
response = '\n'.join([response['title'], response['article_number'], response['answer']])
except Exception as e:
response = f"Sorry, I encountered an error: {str(e)}"
st.session_state.messages.append({"role": "assistant", "content": response})
with st.chat_message("assistant"):
st.markdown(response)