File size: 4,261 Bytes
a442f53
 
 
f70d963
4980e0d
a442f53
f70d963
 
4980e0d
f70d963
4980e0d
a442f53
f70d963
8b5a437
4980e0d
 
 
 
8b5a437
4980e0d
 
a442f53
4980e0d
 
 
 
 
 
 
 
 
a442f53
4980e0d
 
429dd66
a442f53
49bd39d
 
9c0039a
7c6c379
42effba
7c6c379
9c0039a
7c6c379
 
 
42effba
f70d963
a442f53
49bd39d
 
 
 
 
 
 
 
 
 
 
4980e0d
 
 
49bd39d
f70d963
4980e0d
49bd39d
 
 
 
 
 
 
 
 
 
 
4980e0d
49bd39d
8b5a437
f70d963
4980e0d
 
 
 
 
 
 
 
 
49bd39d
4980e0d
 
 
 
49bd39d
4980e0d
49bd39d
4980e0d
 
 
 
 
 
 
 
49bd39d
4980e0d
 
 
 
 
49bd39d
 
4980e0d
 
 
 
 
 
 
 
49bd39d
 
 
4980e0d
49bd39d
4980e0d
49bd39d
4980e0d
a442f53
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import os
import streamlit as st
import pandas as pd
import google.generativeai as genai
from google.generativeai.types import GenerationConfig

# Configuration
os.environ['GOOGLE_API_KEY'] = os.getenv('GOOGLE_API_KEY', 'your_key_here')
genai.configure(api_key=os.environ['GOOGLE_API_KEY'])

# Load and prepare data
@st.cache_data
def load_scholarships():
    try:
        df = pd.read_csv("scholarships_data.csv", quotechar='"', quoting=1)
        df = df.map(lambda x: x.strip() if isinstance(x, str) else x)
        df.rename(columns=lambda x: x.strip(), inplace=True)
        return df
    except Exception as e:
        st.error(f"Error loading data: {str(e)}")
        st.stop()

# Convert CSV to RAG context
def create_rag_context(df):
    context = "Scholarship Database:\n\n"
    for _, row in df.iterrows():
        context += f"""Scholarship Name: {row['Scholarship Name']}
Eligibility: {row['Eligibility']}
Deadline: {row['Deadline']}
Link: {row['Link']}\n\n"""
    return context

# Initialize Gemini model
def get_rag_model():
    return genai.GenerativeModel('gemini-2.0-flash-lite')

# Custom CSS for styling
def load_css():
    st.markdown("""
    <style>
    /* Custom color for results */
    .stMarkdown p, .stMarkdown ul, .stMarkdown ol {
        color: rgb(1, 27, 29); /* Custom color for results */
    }
    </style>
    """, unsafe_allow_html=True)

# Streamlit app
def main():
    # Load custom CSS
    load_css()

    # Hero Section
    st.markdown("""
    <div style="text-align: center; padding: 50px 0;">
        <h1 style="color: #2c3e50; font-size: 3rem;">πŸŽ“ AI Scholarship Advisor</h1>
        <p style="color: #34495e; font-size: 1.2rem;">Find the best scholarships tailored just for you!</p>
    </div>
    """, unsafe_allow_html=True)

    # Load data and create RAG context
    df = load_scholarships()
    rag_context = create_rag_context(df)

    # User input form
    with st.form("profile_form"):
        st.markdown("### πŸ“ Student Profile")
        col1, col2 = st.columns(2)
        with col1:
            age = st.number_input("Age", 16, 50, 20)
            citizenship = st.selectbox("Citizenship", ["India", "Other"])
            income = st.number_input("Annual Family Income (β‚Ή)", 0, 10000000, 300000)
        with col2:
            education = st.selectbox("Education Level", 
                                   ["High School", "Undergraduate", "Postgraduate", "PhD"])
            category = st.selectbox("Category", 
                                  ["General", "OBC", "SC", "ST", "EWS", "Minority"])
        
        submitted = st.form_submit_button("πŸš€ Get Recommendations")

    if submitted:
        # Create user profile
        user_profile = f"""
        Student Profile:
        - Age: {age}
        - Citizenship: {citizenship}
        - Annual Income: β‚Ή{income}
        - Education Level: {education}
        - Category: {category}
        """

        # Generate response using RAG
        model = get_rag_model()
        prompt = f"""
        {rag_context}

        {user_profile}

        Task: 
        1. Analyze the student profile against all scholarships
        2. Identify top 5 most relevant scholarships with priority order
        3. For each scholarship:
           - List matching eligibility criteria
           - Explain why it's a good match
           - Provide direct application link
        4. Format response with markdown headers and bullet points

        Important: 
        - Be specific about eligibility matches
        - Highlight deadlines if available
        - Never invent scholarships not in the database
        """

        with st.spinner("πŸ” Analyzing 50+ scholarships..."):
            response = model.generate_content(
                prompt,
                generation_config=GenerationConfig(
                    temperature=0.3,
                    top_p=0.95,
                    max_output_tokens=2000
                )
            )

        # Display recommendations
        st.markdown("### πŸŽ‰ Personalized Recommendations")
        st.markdown(response.text)

        # Show raw data for transparency
        with st.expander("πŸ“Š View Full Scholarship Database"):
            st.dataframe(df)

if __name__ == "__main__":
    main()