import streamlit as st import google.generativeai as genai import os # Page configuration st.set_page_config( page_title="Government Services Assistant", page_icon="🏛️", layout="centered" ) # Custom CSS for better styling st.markdown(""" """, unsafe_allow_html=True) # Initialize session state if 'messages' not in st.session_state: st.session_state.messages = [] if 'chat_session' not in st.session_state: st.session_state.chat_session = None if 'api_key_configured' not in st.session_state: st.session_state.api_key_configured = False # Hardcoded responses IDENTITY_GREETING = """I am a Government Services Assistant for the KP Government. My role is to provide official information about: • Government services and procedures • Required documents for applications • Office timings and locations • General service-related guidance ⚠️ Important: I do not provide legal or medical advice. How may I assist you today?""" EMERGENCY_RESPONSE = """⚠️ For emergencies, please contact official emergency services: • Police: 15 • Rescue: 1122 • Edhi: 115 This chatbot does not handle emergency situations.""" LEGAL_DISCLAIMER = """⚖️ I cannot provide legal advice or legal interpretations. Please consult: • A qualified legal professional • The relevant government authority • Legal aid services I can only provide procedural information about government services.""" MEDICAL_DISCLAIMER = """🏥 I am not authorized to provide medical advice. Please consult a licensed medical professional or healthcare provider. I can provide information about: • How to obtain medical certificates (procedure only) • Hospital locations and timings""" POLITICAL_RESPONSE = """I focus only on government service procedures and official information. I do not provide opinions on political matters.""" def initialize_chat_session(api_key): """Initialize the Gemini chat session with pre-seeded persona""" try: genai.configure(api_key=api_key) # Create model model = genai.GenerativeModel('gemini-2.5-flash') # Pre-seeded persona history initial_history = [ { "role": "user", "parts": [{ "text": """You are a Government Services Assistant for KP Government, Pakistan. Your behavior rules: 1. Use friendly, professional, and official tone 2. Focus ONLY on Pakistan/KP government procedures 3. Provide step-by-step procedural guidance 4. Always list required documents clearly 5. Use clear, simple language (no technical jargon) 6. When giving processing times or fees, add: "Please confirm with the concerned office as these may vary by district." 7. Never interpret laws or provide legal opinions 8. Never guess - if unsure, say "Please contact the relevant government office for accurate information." 9. Be polite and respectful in all interactions You assist with services like: - Domicile certificates - CNIC procedures - Birth certificates - Complaint procedures - Office timings and locations - General procedural guidance Respond in the same language as the user (English/Urdu/Pashto).""" }] }, { "role": "model", "parts": [{ "text": "Persona confirmed. I am the Government Services Assistant for KP Government. I am ready to assist citizens with official service information and procedures." }] } ] # Start chat session with history chat_session = model.start_chat(history=initial_history) return chat_session, None except Exception as e: return None, str(e) def check_hardcoded_responses(user_input): """Check if user input matches hardcoded response patterns""" user_lower = user_input.lower() # Identity queries if user_lower in ["who are you?", "who are you", "what can you do?", "what can you do", "introduce yourself", "what is your purpose?", "what is your purpose"]: return IDENTITY_GREETING # Emergency queries if any(word in user_lower for word in ["emergency", "urgent help", "police", "ambulance", "rescue"]): return EMERGENCY_RESPONSE # Legal advice queries if any(word in user_lower for word in ["legal advice", "can i sue", "is this legal", "lawyer", "court case", "legal opinion"]): return LEGAL_DISCLAIMER # Medical advice queries if any(word in user_lower for word in ["medical advice", "health advice", "diagnosis", "medicine", "treatment", "doctor recommendation"]): return MEDICAL_DISCLAIMER # Political queries if any(word in user_lower for word in ["which party", "political opinion", "vote for", "is this policy good", "government failure"]): return POLITICAL_RESPONSE return None # Header st.markdown("""
خوش آمدید • ښه راغلاست • Welcome
Developed for KP Government Training 2026 • AI-based Chatbots Workshop
Trainer: Engr. Sumayyea Salahuddin