import streamlit as st from docxtpl import DocxTemplate, RichText import io from datetime import date import os import subprocess # --- CONFIGURATION --- TEMPLATE_FILE = "template.docx" # Make sure your uploaded file is named this! DEFAULT_BANK = "Vastu Housing Finance Corporation Ltd." st.set_page_config(page_title="Smart LSR Generator", layout="wide") # --- SESSION STATE INITIALIZATION --- if 'properties' not in st.session_state: st.session_state.properties = [{ 'cat': 'Residential', 'address': '', 'area': '', 'unit': 'Sq. ft', 'east': '', 'west': '', 'north': '', 'south': '' }] if 'annex_1' not in st.session_state: st.session_state.annex_1 = [] if 'annex_2' not in st.session_state: st.session_state.annex_2 = [] if 'submitted_docs' not in st.session_state: st.session_state.submitted_docs = [] if 'co_applicants' not in st.session_state: st.session_state.co_applicants = [] def to_title_case(text): return text.title() if text else "" # --- SIDEBAR: FILE SETUP --- with st.sidebar: st.header("⚙️ System Status") if os.path.exists(TEMPLATE_FILE): st.success(f"Template '{TEMPLATE_FILE}' found!") else: st.error(f"Template '{TEMPLATE_FILE}' NOT found. Please upload it.") # --- MAIN UI --- st.title("⚖️ Smart Legal Scrutiny Report") # 1. BASIC INFORMATION with st.expander("1. Basic Information", expanded=True): col1, col2 = st.columns(2) with col1: loan_no = st.text_input("Loan A/C No") report_date = st.date_input("Report Date", value=date.today()) bank_name = st.text_input("Bank Name", value=DEFAULT_BANK) with col2: app_name = st.text_input("Applicant Name") current_owner = st.text_input("Current Owner Name") trace_years = st.text_input("Trace Years", value="13") # Co-Applicants st.subheader("Co-Applicants") c_col1, c_col2 = st.columns([3, 1]) with c_col1: new_co = st.text_input("Add Co-Applicant Name") with c_col2: if st.button("Add Co-App"): if new_co: st.session_state.co_applicants.append(to_title_case(new_co)) if st.session_state.co_applicants: st.write(f"**Added:** {', '.join(st.session_state.co_applicants)}") if st.button("Clear Co-Applicants"): st.session_state.co_applicants = [] # 2. PROPERTY DETAILS (VERTICAL STACK) with st.expander("2. Property Details", expanded=True): for idx, p in enumerate(st.session_state.properties): st.info(f"🏡 Property {idx + 1}") # Category & Address pc1, pc2 = st.columns([1, 3]) p['cat'] = pc1.selectbox(f"Category #{idx+1}", ["Residential", "Commercial", "Agricultural", "Plot"], key=f"cat_{idx}") p['address'] = pc2.text_area(f"Full Address #{idx+1}", value=p['address'], placeholder="e.g. Khasra No 85/1, Village Kolipura...", key=f"addr_{idx}") # Area & Boundaries pc3, pc4 = st.columns(2) with pc3: ac1, ac2 = st.columns([2, 1]) p['area'] = ac1.text_input(f"Area #{idx+1}", value=p['area'], key=f"area_{idx}") p['unit'] = ac2.selectbox(f"Unit #{idx+1}", ["Sq. ft", "Hectares", "Acres"], key=f"unit_{idx}") with pc4: st.caption("Boundaries") bc1, bc2 = st.columns(2) p['east'] = bc1.text_input("East", value=p['east'], key=f"e_{idx}") p['west'] = bc2.text_input("West", value=p['west'], key=f"w_{idx}") p['north'] = bc1.text_input("North", value=p['north'], key=f"n_{idx}") p['south'] = bc2.text_input("South", value=p['south'], key=f"s_{idx}") if st.button("+ Add Another Property"): st.session_state.properties.append({'cat': 'Residential', 'address': '', 'area': '', 'unit': 'Sq. ft', 'east': '', 'west': '', 'north': '', 'south': ''}) st.rerun() # 3. SMART NARRATIVE with st.expander("3. History of Title (Narrative)", expanded=True): col_n1, col_n2 = st.columns([3, 1]) with col_n2: if st.button("✨ Auto-Draft Narrative"): # Logic to generate text from Property 1 if st.session_state.properties: p1 = st.session_state.properties[0] draft = ( f"All the piece and parcel of land situated at {to_title_case(p1['address'])} " f"(total area {p1['area']} {p1['unit']}) appears to be originally owned by {to_title_case(current_owner)}, " f"as evidenced by the revenue records.\n\n" f"On perusal of Registered Sale Deed, it appears that the owner sold the said land " f"to {to_title_case(app_name)}.\n\n" f"Therefore, based on documents produced, the flow of title appears to be clear and in order, " f"vesting with {to_title_case(app_name)}." ) st.session_state.auto_draft = draft narrative_val = st.session_state.get('auto_draft', "") narrative_input = st.text_area("Flow of Title", value=narrative_val, height=250) # 4. DOCUMENTS & ANNEXURES with st.expander("4. Documents Management"): # Helper to manage lists def list_manager(title, key_name): st.subheader(title) new_item = st.text_input(f"Add to {title}", key=f"input_{key_name}") if st.button(f"Add Item", key=f"btn_{key_name}"): if new_item: st.session_state[key_name].append(new_item) # Display list with remove option for i, item in enumerate(st.session_state[key_name]): c1, c2 = st.columns([4, 1]) c1.write(f"{i+1}. {item}") if c2.button("❌", key=f"del_{key_name}_{i}"): st.session_state[key_name].pop(i) st.rerun() list_manager("Submitted Documents (Part-II)", "submitted_docs") st.divider() c1, c2 = st.columns(2) with c1: list_manager("Annexure I (Standard)", "annex_1") with c2: list_manager("Annexure II (Post-Disbursal)", "annex_2") # 5. LEGAL AUDIT (FIXED QUESTIONS) with st.expander("5. Legal Audit Checklist"): questions = [ "Whether the provisions of Urban Land Ceiling Act are applicable?", "Whether property to be given as security is subject to any minor’s claim/share?", "Whether the property is affected by any revenue and tenancy Regulations?", "Whether the user land has been converted under Land Revenue Law?", "Whether all original chain documents of the property have been Scrutinized?", "Whether required documents are available for creating mortgage?", "Whether all previous owners had the right/competency to transfer the Property?", "Whether proposed mortgage by deposit of title deeds is possible?", "Whether the property is lease land? If yes, then what is the tenure of the Lease?", "Whether the land/property is joint family property?", "Is the land/property/flat subject to any reservation/acquisition/requisition?", "Whether SARFAESI ACT is applicable or not?" ] # Default answers: No for most, Yes for positive ones (indices 4, 5, 6, 7, 11) default_indices = [1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0] # 0=Yes, 1=No audit_results = [] for i, q in enumerate(questions): idx = default_indices[i] ans = st.radio(f"Q{i+1}: {q}", ["Yes", "No", "N/A"], index=idx, horizontal=True) audit_results.append({'provision_text': q, 'remark': ans}) # --- GENERATION LOGIC --- st.divider() if st.button("Generate Report (Word & PDF)", type="primary"): if not os.path.exists(TEMPLATE_FILE): st.error("Template file missing! Please upload 'template.docx'.") else: doc = DocxTemplate(TEMPLATE_FILE) # Create Context context = { 'LoanNo': loan_no, 'ReportDate': str(report_date), 'BankName': bank_name, 'ApplicantName': to_title_case(app_name), 'co_applicants': st.session_state.co_applicants, 'CurrentOwner': to_title_case(current_owner), 'TraceYears': trace_years, 'TitleNarrative': RichText(narrative_input), # Preserves newlines 'properties': st.session_state.properties, 'submitted_docs': st.session_state.submitted_docs, 'legal_questions': audit_results, 'annexure_1_list': st.session_state.annex_1, 'annexure_2_list': st.session_state.annex_2 } # Render doc.render(context) # Save to Memory bio = io.BytesIO() doc.save(bio) # Download Button filename = f"{to_title_case(app_name)}_{loan_no}".replace(" ", "_") st.success("✅ Report Generated!") st.download_button( label="Download Word Doc", data=bio.getvalue(), file_name=f"{filename}.docx", mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document" )