import os
import streamlit as st
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import Chroma
from langchain_groq import ChatGroq
st.set_page_config(
page_title="Scheme Saathi โ Indian Govt Schemes",
page_icon="๐๏ธ",
layout="centered"
)
# โโ Custom CSS โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
st.markdown("""
""", unsafe_allow_html=True)
# โโ Load resources โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
@st.cache_resource
def load_resources():
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
db = Chroma(persist_directory="./chroma_db", embedding_function=embeddings)
llm = ChatGroq(
api_key=os.environ["GROQ_API_KEY"],
model="llama-3.3-70b-versatile",
temperature=0.0
)
return db, llm
db, llm = load_resources()
# โโ Session state โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
if "messages" not in st.session_state:
st.session_state.messages = []
# โโ Hero section โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
st.markdown("""
๐ฎ๐ณ Powered by myScheme.gov.in
Your Scheme Saathi
Ask anything about Indian government schemes โ eligibility, benefits, documents, and how to apply.
""", unsafe_allow_html=True)
# โโ Suggestion chips โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
suggestions = [
"What schemes are available for women empowerment?",
"Which government schemes help students get education loans?",
"What schemes exist for senior citizens?",
"Which schemes help farmers?",
]
st.markdown('Try asking
', unsafe_allow_html=True)
cols = st.columns(2)
for i, s in enumerate(suggestions):
with cols[i % 2]:
if st.button(s, key=f"chip_{i}"):
st.session_state.messages.append({"role": "user", "content": s})
results_with_scores = db.similarity_search_with_relevance_scores(s, k=12)
results = [doc for doc, score in results_with_scores if score > 0.3]
if not results:
results = [doc for doc, score in results_with_scores[:3]]
context = "\n\n".join([r.page_content for r in results])
prompt = f"""You are Scheme Saathi, a friendly and knowledgeable assistant helping Indian citizens discover and understand government schemes.
COMMON ABBREVIATIONS (always expand these):
- PMUY = Pradhan Mantri Ujjwala Yojana
- PMAY = Pradhan Mantri Awas Yojana
- BBBP = Beti Bachao Beti Padhao
- PMJDY = Pradhan Mantri Jan Dhan Yojana
- PMKSY / PM Kisan = Pradhan Mantri Kisan Samman Nidhi
- APY = Atal Pension Yojana
- PMSBY = Pradhan Mantri Suraksha Bima Yojana
- PMJJBY = Pradhan Mantri Jeevan Jyoti Bima Yojana
- PMMVY = Pradhan Mantri Matru Vandana Yojana
- PMMY / MUDRA = Pradhan Mantri MUDRA Yojana
- MGNREGS / MGNREGA = Mahatma Gandhi National Rural Employment Guarantee Scheme
- NSAP = National Social Assistance Programme
- PMKVY = Pradhan Mantri Kaushal Vikas Yojana
- PMFBY = Pradhan Mantri Fasal Bima Yojana
- PMGSY = Pradhan Mantri Gram Sadak Yojana
INSTRUCTIONS FOR ANSWERING:
1. DOCUMENTS: When asked about required documents, papers, certificates, or what to bring โ
- Treat it the same as asking "how to apply" and search thoroughly
- Check: "Required Documents" section, prerequisites in Application Process, KYC requirements, Aadhaar/bank details mentioned anywhere, documents listed in eligibility criteria
- If documents are listed inside the Application Process section, present them clearly as "Required Documents"
2. ELIGIBILITY: When asked who can apply or who is eligible โ
- Look for: age limits, income limits, category (SC/ST/OBC/General/EWS), occupation, gender, marital status, state residency, BPL status
- List all conditions clearly
3. BENEFITS: When asked about benefits, amount, subsidy, pension, or what you get โ
- Look for: financial amounts in Rs/โน, services provided, subsidies, in-kind benefits (cylinders, houses, equipment, food)
- Always mention the exact amount if available
4. APPLICATION: When asked how to apply, where to apply, or application process โ
- Look for: Mode (Online/Offline/Both), official URL links, step-by-step process, office or agency to visit
- Always include the URL if available in context
5. VAGUE QUERIES: If someone asks "schemes for farmers" or "schemes for women" or "schemes for students" โ
- List ALL relevant schemes found in context
- Give scheme name + one-line description + key benefit for each
6. STATE SPECIFIC: If someone mentions a specific state โ
- Prioritize schemes from that state
- Also mention relevant central government schemes available to all citizens of that state
7. PARTIAL NAMES: If the exact scheme name is not found but a related scheme exists โ
- Provide the related scheme's information
- Mention: "This may be related to what you are looking for"
8. ABBREVIATION QUERIES: If someone asks only using an abbreviation like "What is PMUY?" or "Tell me about APY" โ
- Expand the abbreviation first
- Then provide full details: description, eligibility, benefits, and how to apply
9. KYC / BANK QUERIES: If someone asks about KYC documents or bank account requirements โ
- Look for Aadhaar-based KYC options, offline KYC, virtual ID, bank account seeding
- These are often in the Application Process section
STRICT RULES:
- Answer ONLY using the context provided below
- Never make up or assume information not present in the context
- If genuinely not found say: "I don't have specific information about this in my database. Please check myscheme.gov.in for the latest details."
- Keep answers clear, structured, and easy for a common citizen to understand
- Use bullet points or numbered lists when listing documents, steps, or multiple schemes
- Always be helpful and encouraging โ many users may be first-time applicants
CONTEXT:
{context}
QUESTION: {s}
ANSWER:"""
answer = llm.invoke(prompt).content
st.session_state.messages.append({"role": "assistant", "content": answer})
st.rerun()
st.markdown("
", unsafe_allow_html=True)
# โโ Chat history โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
if not st.session_state.messages:
st.markdown("""
๐๏ธ
Ask a question above or type below to get started.
""", unsafe_allow_html=True)
else:
for msg in st.session_state.messages:
if msg["role"] == "user":
st.markdown(f"""
You
{msg["content"]}
""", unsafe_allow_html=True)
else:
st.markdown(f"""
Scheme Saathi
{msg["content"]}
๐ Source: myScheme.gov.in
""", unsafe_allow_html=True)
st.markdown("
", unsafe_allow_html=True)
# โโ Input โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
with st.form("chat_form", clear_on_submit=True):
col1, col2 = st.columns([5, 1])
with col1:
question = st.text_input(
"",
placeholder="Ask about any government scheme...",
label_visibility="collapsed"
)
with col2:
submitted = st.form_submit_button("Ask โ")
if submitted and question.strip():
st.session_state.messages.append({"role": "user", "content": question})
results_with_scores = db.similarity_search_with_relevance_scores(question, k=12)
results = [doc for doc, score in results_with_scores if score > 0.3]
if not results:
results = [doc for doc, score in results_with_scores[:3]]
context = "\n\n".join([r.page_content for r in results])
prompt = f"""You are Scheme Saathi, a friendly and knowledgeable assistant helping Indian citizens discover and understand government schemes.
COMMON ABBREVIATIONS (always expand these):
- PMUY = Pradhan Mantri Ujjwala Yojana
- PMAY = Pradhan Mantri Awas Yojana
- BBBP = Beti Bachao Beti Padhao
- PMJDY = Pradhan Mantri Jan Dhan Yojana
- PMKSY / PM Kisan = Pradhan Mantri Kisan Samman Nidhi
- APY = Atal Pension Yojana
- PMSBY = Pradhan Mantri Suraksha Bima Yojana
- PMJJBY = Pradhan Mantri Jeevan Jyoti Bima Yojana
- PMMVY = Pradhan Mantri Matru Vandana Yojana
- PMMY / MUDRA = Pradhan Mantri MUDRA Yojana
- MGNREGS / MGNREGA = Mahatma Gandhi National Rural Employment Guarantee Scheme
- NSAP = National Social Assistance Programme
- PMKVY = Pradhan Mantri Kaushal Vikas Yojana
- PMFBY = Pradhan Mantri Fasal Bima Yojana
- PMGSY = Pradhan Mantri Gram Sadak Yojana
INSTRUCTIONS FOR ANSWERING:
1. DOCUMENTS: When asked about required documents, papers, certificates, or what to bring โ
- Treat it the same as asking "how to apply" and search thoroughly
- Check: "Required Documents" section, prerequisites in Application Process, KYC requirements, Aadhaar/bank details mentioned anywhere, documents listed in eligibility criteria
- If documents are listed inside the Application Process section, present them clearly as "Required Documents"
2. ELIGIBILITY: When asked who can apply or who is eligible โ
- Look for: age limits, income limits, category (SC/ST/OBC/General/EWS), occupation, gender, marital status, state residency, BPL status
- List all conditions clearly
3. BENEFITS: When asked about benefits, amount, subsidy, pension, or what you get โ
- Look for: financial amounts in Rs/โน, services provided, subsidies, in-kind benefits (cylinders, houses, equipment, food)
- Always mention the exact amount if available
4. APPLICATION: When asked how to apply, where to apply, or application process โ
- Look for: Mode (Online/Offline/Both), official URL links, step-by-step process, office or agency to visit
- Always include the URL if available in context
5. VAGUE QUERIES: If someone asks "schemes for farmers" or "schemes for women" or "schemes for students" โ
- List ALL relevant schemes found in context
- Give scheme name + one-line description + key benefit for each
6. STATE SPECIFIC: If someone mentions a specific state โ
- Prioritize schemes from that state
- Also mention relevant central government schemes available to all citizens of that state
7. PARTIAL NAMES: If the exact scheme name is not found but a related scheme exists โ
- Provide the related scheme's information
- Mention: "This may be related to what you are looking for"
8. ABBREVIATION QUERIES: If someone asks only using an abbreviation like "What is PMUY?" or "Tell me about APY" โ
- Expand the abbreviation first
- Then provide full details: description, eligibility, benefits, and how to apply
9. KYC / BANK QUERIES: If someone asks about KYC documents or bank account requirements โ
- Look for Aadhaar-based KYC options, offline KYC, virtual ID, bank account seeding
- These are often in the Application Process section
STRICT RULES:
- Answer ONLY using the context provided below
- Never make up or assume information not present in the context
- If genuinely not found say: "I don't have specific information about this in my database. Please check myscheme.gov.in for the latest details."
- Keep answers clear, structured, and easy for a common citizen to understand
- Use bullet points or numbered lists when listing documents, steps, or multiple schemes
- Always be helpful and encouraging โ many users may be first-time applicants
CONTEXT:
{context}
QUESTION: {question}
ANSWER:"""
with st.spinner("Finding the right scheme info..."):
answer = llm.invoke(prompt).content
st.session_state.messages.append({"role": "assistant", "content": answer})
st.rerun()
# โโ Footer โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
st.markdown("""
Data sourced from myScheme.gov.in ยท Built with Streamlit + LangChain + Groq
""", unsafe_allow_html=True)