Vamshi2277/AI-Powered-Human-Rights-Intelligence
0
1import streamlit as st2import os3import tempfile4 5from dotenv import load_dotenv6 7from google import genai8 9from rag_index_builder import build_index_from_pdf10from tools import retrieve_legal_context11import base6412 13 14load_dotenv()15 16GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")17 18client = genai.Client(api_key=GEMINI_API_KEY)19 20 21# STREAMLIT UI22 23st.set_page_config(24 page_title="AI_Powered_Human_Rights_Intelligence",25 layout="wide"26)27 28st.title("⚖️ AI_Powered_Human_Rights_Intelligence")29 30 31 32## background image 33def set_background(image_file):34 35 with open(image_file, "rb") as image:36 37 encoded = base64.b64encode(image.read()).decode()38 39 page_bg = f"""40 <style>41 42 /* MAIN APP BACKGROUND */43 44 .stApp {{45 background:46 linear-gradient(47 rgba(0,0,0,0.65),48 rgba(0,0,0,0.60)49 ),50 url("data:image/jpg;base64,{encoded}");51 52 background-size: cover;53 background-position: center;54 background-repeat: no-repeat;55 background-attachment: fixed;56 }}57 58 /* TEXT COLORS */59 60 h1, h2, h3, h4, h5, h6, p, label {{61 color: white !important;62 }}63 64 /* FILE UPLOADER */65 66 [data-testid="stFileUploader"] {{67 background-color: rgba(25,25,25,0.75);68 padding: 20px;69 border-radius: 15px;70 }}71 72 /* TEXT INPUT */73 74 .stTextInput > div > div > input {{75 background-color: rgba(30,30,30,0.85);76 color: white;77 border-radius: 10px;78 border: 1px solid rgba(255,255,255,0.1);79 }}80 81 /* SAMPLE QUESTION BOX */82 83 .sample-box {{84 background-color: rgba(35,35,35,0.80);85 color: white;86 padding: 14px;87 border-radius: 10px;88 margin-bottom: 12px;89 border: 1px solid rgba(255,255,255,0.1);90 font-size: 16px;91 }}92 93 /* ANSWER BOX */94 95 .answer-box {{96 background-color: rgba(15,15,15,0.92);97 color: white;98 padding: 25px;99 border-radius: 15px;100 font-size: 20px;101 line-height: 1.8;102 border: 1px solid rgba(255,255,255,0.1);103 margin-top: 20px;104 }}105 106 /* REMOVE WHITE HEADER */107 108 header {{109 background-color: transparent !important;110 }}111 112 /* SIDEBAR */113 114 section[data-testid="stSidebar"] {{115 background-color: rgba(20,20,20,0.85);116 }}117 118 </style>119 """120 121 st.markdown(page_bg, unsafe_allow_html=True)122 123 124# ======================================================125# SET BACKGROUND IMAGE126# ========================================================127 128set_background("download.jpg")129 130def generate_answer(query):131 132 context = retrieve_legal_context(query)133 134 prompt= f"""135 You are an expert constitutional law professor and legal research assistant.136 137 Your task is to answer questions strictly using retrieved legal context.138 139 The retrieved documents may contain:140 - constitutional provisions141 - legal explanations142 - comparative legal tables143 - mappings between Universal Declaration of Human Rights (UDHR)144 and Indian Constitutional Articles145 146 Instructions:147 148 1. If the retrieved context contains BOTH:149 - Universal Declaration article150 - Indian Constitutional article151 152 then explain BOTH clearly.153 154 2. When tables are present:155 - read the relationship between columns carefully156 - identify corresponding legal provisions157 - explain the connection professionally158 159 3. Structure answers like this:160 161 - Universal Declaration Provision162 - Indian Constitutional Provision163 - Legal Meaning164 - Constitutional Importance165 166 4. Use formal legal-academic language.167 168 5. Do NOT give one-line answers.169 170 6. Do NOT hallucinate legal provisions outside retrieved context.171 172 7. If the question refers to a legal right,173 identify corresponding articles from both:174 - UDHR175 - Indian Constitution176 177 Retrieved Context:178 {context}179 180 Question:181 {query}182 183 Answer:184 """185 response = client.models.generate_content(186 model="gemini-3.1-flash-lite-preview",187 contents=prompt188 )189 190 return response.text191 192 193 194 195st.markdown("""196Upload legal PDFs and ask legal questions.197The assistant answers using retrieved document context.198""")199 200 201uploaded_file = st.file_uploader(202 "Upload Legal PDF",203 type=["pdf"]204)205 206 207if uploaded_file is not None:208 209 with tempfile.NamedTemporaryFile(210 delete=False,211 suffix=".pdf"212 ) as tmp_file:213 214 tmp_file.write(uploaded_file.read())215 216 tmp_pdf_path = tmp_file.name217 218 st.success("PDF uploaded successfully.")219 220 with st.spinner("Building FAISS index..."):221 222 build_index_from_pdf(223 tmp_pdf_path,224 persist_dir="rag_faiss_store"225 )226 227 st.success("Index created successfully.")228 229 st.markdown("---")230 231 232st.subheader("📚 Sample Legal Questions")233 234sample_questions = [235 "What is Article 14 of the Indian Constitution?",236 "Explain Article 21 and protection of life and personal liberty.",237 "What is freedom of conscience and religion?",238 "Explain Article 32 constitutional remedies.",239 "What protections exist against forced labour?",240 "Compare UDHR and Indian constitutional rights.",241 "Explain the right to work and favourable conditions of work under UDHR and the Indian Constitution.",242 "What is the right to equal pay for equal work under human rights law?",243 "Explain the right to education under the Universal Declaration of Human Rights and the Constitution of India.",244 "What is the right against arbitrary arrest and detention?",245 "Explain freedom of speech and expression."246]247 248left_col, right_col = st.columns(2)249 250mid = len(sample_questions) // 2251 252left_questions = sample_questions[:mid]253right_questions = sample_questions[mid:]254 255 256with left_col:257 258 for question in left_questions:259 260 st.markdown(261 f"""262 <div class="sample-box">263 {question}264 </div>265 """,266 unsafe_allow_html=True267 )268 269 270with right_col:271 272 for question in right_questions:273 274 st.markdown(275 f"""276 <div class="sample-box">277 {question}278 </div>279 """,280 unsafe_allow_html=True281 )282 283# =========================================284# USER INPUT285# =========================================286 287query = st.text_input(288 "Ask your legal question",289 placeholder="Example: Explain Article 21 under Indian Constitution"290)291 292if query:293 294 with st.spinner("Retrieving legal context and generating answer..."):295 296 answer = generate_answer(query)297 298 st.markdown("## ⚖️ Legal Answer")299 300 st.markdown(301 f"""302 <div class="answer-box">303 {answer}304 </div>305 """,306 unsafe_allow_html=True307 )