AI / app.py
saherPervaiz's picture
Update app.py
210d38b verified
raw
history blame
4.43 kB
import streamlit as st
import requests
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
# Set up the Streamlit page
st.title("AI Opportunity Finder for Youth")
st.write("Find Scholarships, Internships, Online Courses, and more!")
# Function to get scholarships data from a mock API
def get_scholarships(location, interests):
# Example: Using a mock API or replace it with a real API URL
url = "https://jsonplaceholder.typicode.com/posts" # Mock API
response = requests.get(url)
if response.status_code == 200:
# Convert the response to a list and limit to the first 5 items
posts = response.json()[:5]
# Construct scholarships list
return [{"title": f"Scholarship {i+1}", "description": post['body'], "eligibility": "Any student from any background."} for i, post in enumerate(posts)]
else:
return []
# Function to get internships data from a mock API
def get_internships():
# Example: Using a mock API for Internships (replace with a real API)
url = "https://jsonplaceholder.typicode.com/posts" # Mock API for testing
response = requests.get(url)
if response.status_code == 200:
# Return a list of mock internships
return [{"jobtitle": f"Internship {i+1}", "company": "Sample Company", "location": "Remote", "snippet": "Description of the internship."} for i in range(5)]
else:
return []
# Function to recommend opportunities based on user input
def recommend_opportunities(user_interests, user_skills, opportunities):
# Combine user profile into a single string
user_profile = [f"{user_interests} {user_skills}"]
# Create text data for opportunities based on description & eligibility
opportunities_text = [f"{opportunity['description']} {opportunity['eligibility']}" for opportunity in opportunities]
# Vectorize the text using TF-IDF
vectorizer = TfidfVectorizer(stop_words='english')
tfidf_matrix = vectorizer.fit_transform(opportunities_text + user_profile)
# Compute cosine similarity
cosine_sim = cosine_similarity(tfidf_matrix[-1], tfidf_matrix[:-1])
# Get the indices of the top 5 recommended opportunities
recommendations = cosine_sim[0].argsort()[-5:][::-1]
# Return recommended opportunities
return [opportunities[i] for i in recommendations]
# User input for profile
st.sidebar.header("User Profile")
location = st.sidebar.text_input("Location", "Pakistan") # Default to 'Pakistan'
skills = st.sidebar.text_input("Skills (e.g., Python, Marketing)")
interests = st.sidebar.text_input("Interests (e.g., Technology, Science)")
# Fetch scholarships based on user input
scholarships = get_scholarships(location, interests)
# Display scholarships if available
if scholarships:
st.write("Scholarships found:")
for scholarship in scholarships:
st.write(f"Title: {scholarship['title']}")
st.write(f"Description: {scholarship['description']}")
st.write(f"Eligibility: {scholarship['eligibility']}")
st.write("---")
else:
st.write("No scholarships found based on your criteria.")
# Fetch internships based on user input
internships = get_internships()
# Display internships if available
if internships:
st.write("Internships found:")
for internship in internships:
st.write(f"Title: {internship['jobtitle']}")
st.write(f"Company: {internship['company']}")
st.write(f"Location: {internship['location']}")
st.write(f"Snippet: {internship['snippet']}")
st.write("---")
else:
st.write("No internships found.")
# AI-based recommendations for opportunities
if st.sidebar.button("Get AI Recommendations"):
# Combine scholarships and internships for recommendations
all_opportunities = scholarships + internships
# Get AI recommendations based on user input
recommended_opportunities = recommend_opportunities(interests, skills, all_opportunities)
# Display recommended opportunities
st.write("Recommended Opportunities based on your profile:")
for opportunity in recommended_opportunities:
st.write(f"Title: {opportunity['title']}")
st.write(f"Description: {opportunity['description']}")
st.write(f"Eligibility: {opportunity.get('eligibility', 'Not available')}")
st.write("---")