File size: 1,504 Bytes
2b2073c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3ea6f4
2b2073c
 
 
 
 
 
 
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
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)