# ╔══════════════════════════════════════════════════════════════════╗ # ║ IMPORTS & CONFIG ║ # ╚══════════════════════════════════════════════════════════════════╝ import streamlit as st import google.generativeai as genai GEMINI_API_KEY = "AIzaSyAsjqUR83MvJWrp32VUGCNxGX1FTtQpjVk" GEMINI_MODEL = "gemini-2.0-flash" st.set_page_config( page_title="AI Lawyer — Indian Law Assistant", page_icon="⚖️", layout="wide", initial_sidebar_state="expanded", ) # ╔══════════════════════════════════════════════════════════════════╗ # ║ BACKEND / MODEL LOGIC ║ # ╚══════════════════════════════════════════════════════════════════╝ genai.configure(api_key="AIzaSyAsjqUR83MvJWrp32VUGCNxGX1FTtQpjVk") _model = genai.GenerativeModel("gemini-2.5-flash-lite") SYSTEM_PROMPT = """You are an expert AI lawyer specializing in Indian law. You have deep knowledge of the Indian Penal Code (IPC), Code of Criminal Procedure (CrPC), Constitution of India, Civil Procedure Code (CPC), and all major Indian statutes. Rules: - Answer clearly and precisely with relevant section numbers where applicable. - If a question is outside Indian law, politely redirect. - Structure long answers with headings when needed. - Always end with a brief disclaimer: "This is for informational purposes only and not a substitute for professional legal advice." """ def ask_llm(chat_history: list) -> str: """Send the full conversation history to Gemini and return the reply.""" messages = [] for msg in chat_history: role = "user" if msg["role"] == "user" else "model" messages.append({"role": role, "parts": [msg["content"]]}) # Prepend system instruction as first user/model exchange full_messages = [ {"role": "user", "parts": [SYSTEM_PROMPT]}, {"role": "model", "parts": ["Understood. I am your AI Lawyer specializing in Indian law. How can I assist you today?"]}, ] + messages response = _model.generate_content(full_messages) return response.text # ╔══════════════════════════════════════════════════════════════════╗ # ║ UI / STREAMLIT ║ # ╚══════════════════════════════════════════════════════════════════╝ # ── Session State ───────────────────────────────────────────────── if "chat_history" not in st.session_state: st.session_state.chat_history = [] # ── CSS ─────────────────────────────────────────────────────────── st.markdown(""" """, unsafe_allow_html=True) # ── SIDEBAR ─────────────────────────────────────────────────────── with st.sidebar: st.markdown('', unsafe_allow_html=True) st.markdown('

Indian Law Assistant

', unsafe_allow_html=True) st.markdown('
', unsafe_allow_html=True) st.markdown('

About

', unsafe_allow_html=True) st.markdown("""
Ask any question related to Indian law — IPC, CrPC, Constitution, CPC, and more.

The AI will provide precise answers with relevant section references.
""", unsafe_allow_html=True) st.markdown('
', unsafe_allow_html=True) st.markdown('

Topics Covered

', unsafe_allow_html=True) topics = ["⚖️ Indian Penal Code", "📜 Constitution of India", "🔍 CrPC", "📋 Civil Procedure Code", "🏛️ Contract Act", "👨‍👩‍👧 Family Law", "🏘️ Property Law", "💼 Labour Law"] for t in topics: st.markdown(f'
{t}
', unsafe_allow_html=True) st.markdown('
', unsafe_allow_html=True) if st.session_state.chat_history: msg_count = len([m for m in st.session_state.chat_history if m["role"] == "user"]) st.markdown(f'
💬 {msg_count} question{"s" if msg_count!=1 else ""} asked
', unsafe_allow_html=True) st.markdown("") if st.button("🗑 Clear Chat", use_container_width=True): st.session_state.chat_history = [] st.rerun() st.markdown("""
Designed & Developed by
Yedeedya Injeti
Innomatics Research Labs
""", unsafe_allow_html=True) # ── HERO ────────────────────────────────────────────────────────── st.markdown("""

AI Lawyer

Your intelligent legal assistant for Indian law  •  IPC • CrPC • Constitution & more

Powered by RAG + LLM

""", unsafe_allow_html=True) # ── QUICK SUGGESTIONS (shown only when chat is empty) ───────────── suggestions = [ "Punishment for murder under IPC?", "What is Article 21 of the Constitution?", "Define culpable homicide.", "What are bailable offences?", "Right to self-defence in India?", "What is the limitation period for filing a civil suit?", ] if not st.session_state.chat_history: st.markdown('

Suggested Questions

', unsafe_allow_html=True) col1, col2, col3 = st.columns(3) chosen = None for i, sug in enumerate(suggestions): col = [col1, col2, col3][i % 3] with col: if st.button(sug, key=f"sug_{i}", use_container_width=True): chosen = sug st.markdown("") # ── CHAT WINDOW ─────────────────────────────────────────────────── if not st.session_state.chat_history: st.markdown("""
⚖️
How can I assist you today?
Ask me anything about Indian law — I'll provide accurate answers
with relevant section references and case principles.
""", unsafe_allow_html=True) else: bubbles_html = '
' for msg in st.session_state.chat_history: if msg["role"] == "user": bubbles_html += f"""
{msg["content"]}
""" else: # Convert newlines to
for HTML rendering content = msg["content"].replace("\n", "
") bubbles_html += f"""
⚖️
{content}
""" bubbles_html += "
" st.markdown(bubbles_html, unsafe_allow_html=True) # ── INPUT BAR ───────────────────────────────────────────────────── col_inp, col_send = st.columns([6, 1]) with col_inp: question = st.text_input( "input", label_visibility="collapsed", placeholder="Ask your legal question…", key="chat_input", ) with col_send: send_btn = st.button("Send ⚖️", use_container_width=True) # ── PROCESS ─────────────────────────────────────────────────────── final_q = None if "chosen" in dir() and chosen: final_q = chosen elif send_btn and question.strip(): final_q = question.strip() if final_q: st.session_state.chat_history.append({"role": "user", "content": final_q}) with st.spinner(""): answer = ask_llm(st.session_state.chat_history) st.session_state.chat_history.append({"role": "assistant", "content": answer}) st.rerun() # ── FOOTER ──────────────────────────────────────────────────────── st.markdown(""" """, unsafe_allow_html=True)