Spaces:
Sleeping
Sleeping
| import os | |
| import streamlit as st | |
| import pandas as pd | |
| import google.generativeai as genai | |
| # Configuration | |
| os.environ['GOOGLE_API_KEY'] = os.getenv('GOOGLE_API_KEY', 'your_key_here') | |
| # Load scholarships data | |
| def load_scholarships(): | |
| scholarships = pd.read_csv("scholarships_data.csv") | |
| scholarships.rename(columns=lambda x: x.strip(), inplace=True) # Clean column names | |
| required_columns = ['Scholarship Name', 'Eligibility', 'Link'] | |
| if not all(col in scholarships.columns for col in required_columns): | |
| st.error(f"Missing required columns in the CSV file: {required_columns}") | |
| st.stop() | |
| return scholarships | |
| # Initialize Generative AI | |
| def get_genai_client(): | |
| try: | |
| genai.configure(api_key=os.environ['GOOGLE_API_KEY']) | |
| return genai.GenerativeModel('gemini-pro') | |
| except Exception as e: | |
| st.error(f"AI Initialization Error: {str(e)}") | |
| return None | |
| # Recommendation engine | |
| def recommend_scholarships(user_data, scholarships): | |
| matches = [] | |
| for _, row in scholarships.iterrows(): | |
| eligibility = row['Eligibility'].lower() | |
| if ( | |
| (user_data['citizenship'] in eligibility) and | |
| (user_data['age'] <= parse_age(eligibility)) and | |
| (user_data['income'] <= parse_income(eligibility)) and | |
| (user_data['education'] in eligibility) and | |
| (user_data['category'] in eligibility) | |
| ): | |
| matches.append(row) | |
| return pd.DataFrame(matches) | |
| # Helper functions for eligibility parsing | |
| def parse_age(eligibility): | |
| if 'below 35' in eligibility: | |
| return 35 | |
| return 100 # Default if no age restriction | |
| def parse_income(eligibility): | |
| if '₹2,50,000' in eligibility: | |
| return 250000 | |
| return float('inf') # Default if no income restriction | |
| # Streamlit app | |
| def main(): | |
| st.title("AI-Powered Scholarship Advisor") | |
| # User input form | |
| with st.form("scholarship_form"): | |
| st.header("Student Profile") | |
| citizenship = st.selectbox("Citizenship", ["India", "Other"]).lower() | |
| age = st.number_input("Age", 1, 100, 25) | |
| income = st.number_input("Annual Family Income (₹)", 0, 10000000, 500000) | |
| education = st.selectbox( | |
| "Education Level", | |
| ["Class 10", "Class 12", "Undergraduate", | |
| "Postgraduate", "PhD"] | |
| ).lower() | |
| category = st.selectbox( | |
| "Category", | |
| ["General", "OBC", "SC", "ST", "EWS", "Minority"] | |
| ).lower() | |
| submitted = st.form_submit_button("Find Scholarships") # Form submission button | |
| # Process form submission | |
| if submitted: | |
| # Validate inputs | |
| if not os.environ.get('GOOGLE_API_KEY'): | |
| st.warning("Please set GOOGLE_API_KEY environment variable") | |
| return | |
| # Load data and generate recommendations | |
| scholarships = load_scholarships() | |
| user_data = { | |
| 'citizenship': citizenship, | |
| 'age': age, | |
| 'income': income, | |
| 'education': education, | |
| 'category': category | |
| } | |
| results = recommend_scholarships(user_data, scholarships) | |
| # Display results | |
| if not results.empty: | |
| st.subheader(f"Found {len(results)} Matching Scholarships") | |
| for _, row in results.iterrows(): | |
| st.markdown(f""" | |
| **{row['Scholarship Name']}** | |
| **Eligibility:** {row['Eligibility']} | |
| [Apply Here]({row['Link']}) | |
| """) | |
| st.divider() | |
| else: | |
| st.warning("No scholarships found matching your criteria.") | |
| # Generate AI-powered advice | |
| model = get_genai_client() | |
| if model: | |
| prompt = f""" | |
| Act as an experienced education counselor. | |
| For a {age}-year-old {citizenship} citizen with: | |
| - Annual income: ₹{income} | |
| - Education level: {education} | |
| - Category: {category} | |
| Provide personalized advice about these scholarships: | |
| {results['Scholarship Name'].tolist()} | |
| """ | |
| response = model.generate_content(prompt) | |
| st.subheader("AI Advisor Recommendations") | |
| st.write(response.text) | |
| if __name__ == "__main__": | |
| main() |