Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,106 +1,111 @@
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
import pandas as pd
|
4 |
-
import
|
5 |
|
6 |
-
#
|
|
|
|
|
|
|
7 |
@st.cache_data
|
8 |
-
def
|
9 |
return pd.read_csv("scholarships_data.csv")
|
10 |
|
11 |
-
#
|
12 |
-
def
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
if (
|
17 |
-
(
|
18 |
-
(
|
19 |
-
(
|
20 |
-
(
|
21 |
-
(
|
22 |
):
|
23 |
-
|
24 |
-
return pd.DataFrame(
|
25 |
|
26 |
-
#
|
27 |
-
def
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
return None
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
}
|
38 |
-
payload = {"prompt": prompt}
|
39 |
-
response = requests.post(url, headers=headers, json=payload)
|
40 |
-
if response.status_code == 200:
|
41 |
-
return response.json().get("response", "")
|
42 |
-
else:
|
43 |
-
return "Error: Unable to fetch response from Gemini API."
|
44 |
|
45 |
-
# Streamlit
|
46 |
def main():
|
47 |
-
st.title("Scholarship
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
age = st.number_input("Age", min_value=1, max_value=100)
|
55 |
-
income = st.number_input("Annual Family Income (in ₹)", min_value=0, max_value=10000000)
|
56 |
-
education_level = st.selectbox(
|
57 |
-
"Education Level",
|
58 |
-
["Class 10", "Class 12", "Undergraduate", "Postgraduate", "PhD"]
|
59 |
-
)
|
60 |
-
category = st.selectbox(
|
61 |
-
"Category",
|
62 |
-
["General", "OBC", "SC", "ST", "EWS", "Minority"]
|
63 |
-
)
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
user_details = {
|
74 |
-
"citizenship": citizenship.lower(),
|
75 |
-
"age": age,
|
76 |
-
"income": income,
|
77 |
-
"education_level": education_level.lower(),
|
78 |
-
"category": category.lower(),
|
79 |
}
|
80 |
-
|
81 |
-
# Filter scholarships
|
82 |
-
recommended_scholarships = recommend_scholarships(scholarships_data, user_details)
|
83 |
|
84 |
# Display results
|
85 |
-
st.subheader("
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
st.warning("No scholarships found matching your criteria.")
|
94 |
|
95 |
-
#
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
if __name__ == "__main__":
|
106 |
main()
|
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
import pandas as pd
|
4 |
+
import google.generativeai as genai
|
5 |
|
6 |
+
# Configuration
|
7 |
+
os.environ['GOOGLE_API_KEY'] = os.getenv('GOOGLE_API_KEY', 'your_key_here')
|
8 |
+
|
9 |
+
# Load scholarships data
|
10 |
@st.cache_data
|
11 |
+
def load_scholarships():
|
12 |
return pd.read_csv("scholarships_data.csv")
|
13 |
|
14 |
+
# Initialize Generative AI
|
15 |
+
def get_genai_client():
|
16 |
+
try:
|
17 |
+
genai.configure(api_key=os.environ['GOOGLE_API_KEY'])
|
18 |
+
return genai.GenerativeModel('gemini-pro')
|
19 |
+
except Exception as e:
|
20 |
+
st.error(f"AI Initialization Error: {str(e)}")
|
21 |
+
return None
|
22 |
+
|
23 |
+
# Recommendation engine
|
24 |
+
def recommend_scholarships(user_data, scholarships):
|
25 |
+
matches = []
|
26 |
+
for _, row in scholarships.iterrows():
|
27 |
+
eligibility = row['Eligibility'].lower()
|
28 |
if (
|
29 |
+
(user_data['citizenship'] in eligibility) and
|
30 |
+
(user_data['age'] <= parse_age(eligibility)) and
|
31 |
+
(user_data['income'] <= parse_income(eligibility)) and
|
32 |
+
(user_data['education'] in eligibility) and
|
33 |
+
(user_data['category'] in eligibility)
|
34 |
):
|
35 |
+
matches.append(row)
|
36 |
+
return pd.DataFrame(matches)
|
37 |
|
38 |
+
# Helper functions for eligibility parsing
|
39 |
+
def parse_age(eligibility):
|
40 |
+
if 'below 35' in eligibility:
|
41 |
+
return 35
|
42 |
+
return 100 # Default if no age restriction
|
|
|
43 |
|
44 |
+
def parse_income(eligibility):
|
45 |
+
if '₹2,50,000' in eligibility:
|
46 |
+
return 250000
|
47 |
+
return float('inf') # Default if no income restriction
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
+
# Streamlit app
|
50 |
def main():
|
51 |
+
st.title("AI-Powered Scholarship Advisor")
|
52 |
+
|
53 |
+
# User input form
|
54 |
+
with st.form("scholarship_form"):
|
55 |
+
st.header("Student Profile")
|
56 |
+
citizenship = st.selectbox("Citizenship", ["India", "Other"]).lower()
|
57 |
+
age = st.number_input("Age", 1, 100, 25)
|
58 |
+
income = st.number_input("Annual Family Income (₹)", 0, 10000000, 500000)
|
59 |
+
education = st.selectbox("Education Level",
|
60 |
+
["Class 10", "Class 12", "Undergraduate",
|
61 |
+
"Postgraduate", "PhD"]).lower()
|
62 |
+
category = st.selectbox("Category",
|
63 |
+
["General", "OBC", "SC", "ST", "EWS", "Minority"]).lower()
|
64 |
+
|
65 |
+
submitted = st.form_submit_button("Find Scholarships")
|
66 |
|
67 |
+
if submitted:
|
68 |
+
# Validate inputs
|
69 |
+
if not os.environ.get('GOOGLE_API_KEY'):
|
70 |
+
st.warning("Please set GOOGLE_API_KEY environment variable")
|
71 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
+
# Load data and generate recommendations
|
74 |
+
scholarships = load_scholarships()
|
75 |
+
user_data = {
|
76 |
+
'citizenship': citizenship,
|
77 |
+
'age': age,
|
78 |
+
'income': income,
|
79 |
+
'education': education,
|
80 |
+
'category': category
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
}
|
82 |
+
results = recommend_scholarships(user_data, scholarships)
|
|
|
|
|
83 |
|
84 |
# Display results
|
85 |
+
st.subheader(f"Found {len(results)} Matching Scholarships")
|
86 |
+
for _, row in results.iterrows():
|
87 |
+
st.markdown(f"""
|
88 |
+
**{row['Scholarship Name']}**
|
89 |
+
**Eligibility:** {row['Eligibility']}
|
90 |
+
[Apply Here]({row['Link']})
|
91 |
+
""")
|
92 |
+
st.divider()
|
|
|
93 |
|
94 |
+
# Generate AI-powered advice
|
95 |
+
model = get_genai_client()
|
96 |
+
if model:
|
97 |
+
prompt = f"""
|
98 |
+
Act as an experienced education counselor.
|
99 |
+
For a {age}-year-old {citizenship} citizen with:
|
100 |
+
- Annual income: ₹{income}
|
101 |
+
- Education level: {education}
|
102 |
+
- Category: {category}
|
103 |
+
Provide personalized advice about these scholarships:
|
104 |
+
{results['Scholarship Name'].tolist()}
|
105 |
+
"""
|
106 |
+
response = model.generate_content(prompt)
|
107 |
+
st.subheader("AI Advisor Recommendations")
|
108 |
+
st.write(response.text)
|
109 |
|
110 |
if __name__ == "__main__":
|
111 |
main()
|