AI / app.py
saherPervaiz's picture
Update app.py
2ceeaba verified
raw
history blame
6.42 kB
import streamlit as st
import requests
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from transformers import MarianMTModel, MarianTokenizer
# Set up the Streamlit page
st.title("AI Opportunity Finder for Youth")
st.write("Find Scholarships, Internships, Online Courses, and more!")
# Language Translation Function
def translate_text(text, target_lang='de'):
# Use Hugging Face's MarianMT for translation
model_name = f'Helsinki-NLP/opus-mt-en-{target_lang}'
model = MarianMTModel.from_pretrained(model_name)
tokenizer = MarianTokenizer.from_pretrained(model_name)
translated = model.generate(**tokenizer(text, return_tensors="pt", padding=True, truncation=True))
translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
return translated_text
# Mock function to get data from APIs (replace with actual API calls)
def get_scholarships(country, interests):
url = f"https://jsonplaceholder.typicode.com/posts" # Mock API (replace with real one)
# Simulate API response based on country
if country == "USA":
return [{"title": f"USA Scholarship {i+1}", "description": f"Description for scholarship {i+1} in USA.", "eligibility": "Any student from USA."} for i in range(5)]
elif country == "Germany":
return [{"title": f"Germany Scholarship {i+1}", "description": f"Description for scholarship {i+1} in Germany.", "eligibility": "Any student from Germany."} for i in range(5)]
else:
return [{"title": f"Scholarship {i+1}", "description": f"Description for scholarship {i+1} in {country}.", "eligibility": "Any student from any background."} for i in range(5)]
def get_internships(country):
url = f"https://jsonplaceholder.typicode.com/posts" # Mock API for testing
# Simulate internships data
if country == "USA":
return [{"jobtitle": f"Internship {i+1}", "company": "USA Company", "location": "USA", "snippet": "Description of internship in USA."} for i in range(5)]
elif country == "Germany":
return [{"jobtitle": f"Internship {i+1}", "company": "Germany Company", "location": "Germany", "snippet": "Description of internship in Germany."} for i in range(5)]
else:
return [{"jobtitle": f"Internship {i+1}", "company": "Sample Company", "location": "Remote", "snippet": "Description of internship."} for i in range(5)]
def recommend_opportunities(user_interests, user_skills, opportunities):
user_profile = [f"{user_interests} {user_skills}"]
opportunities_text = [f"{opportunity.get('description', 'No description available')} {opportunity.get('eligibility', 'No eligibility available')}" 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 top 5 recommendations
recommendations = cosine_sim[0].argsort()[-5:][::-1]
return [opportunities[i] for i in recommendations]
# Form to gather user profile and country selection
with st.form(key='user_form'):
st.sidebar.header("User Profile")
location = st.selectbox("Select your Country", ["USA", "Germany", "UK", "India", "Australia", "Pakistan"]) # You can add more countries here
skills = st.text_input("Skills (e.g., Python, Marketing)")
interests = st.text_input("Interests (e.g., Technology, Science)")
target_language = st.selectbox("Select target language", ['de', 'fr', 'es', 'it', 'pt']) # Available language codes for translation
submit_button = st.form_submit_button("Find Opportunities")
# Fetch data based on the user input
if submit_button:
# Fetch scholarships and internships based on the selected country and profile
scholarships = get_scholarships(location, interests)
internships = get_internships(location)
# Display Scholarships
if scholarships:
st.write("Scholarships found:")
for scholarship in scholarships:
title = translate_text(scholarship.get('title', 'No title available'), target_language)
description = translate_text(scholarship.get('description', 'No description available'), target_language)
eligibility = translate_text(scholarship.get('eligibility', 'No eligibility available'), target_language)
st.write(f"Title: {title}")
st.write(f"Description: {description}")
st.write(f"Eligibility: {eligibility}")
st.write("---")
else:
st.write("No scholarships found for the selected country.")
# Display Internships
if internships:
st.write("Internships found:")
for internship in internships:
title = translate_text(internship.get('jobtitle', 'No title available'), target_language)
company = translate_text(internship.get('company', 'No company available'), target_language)
location = translate_text(internship.get('location', 'No location available'), target_language)
snippet = translate_text(internship.get('snippet', 'No snippet available'), target_language)
st.write(f"Title: {title}")
st.write(f"Company: {company}")
st.write(f"Location: {location}")
st.write(f"Snippet: {snippet}")
st.write("---")
else:
st.write("No internships found for the selected country.")
# AI Recommendations based on interests and skills
all_opportunities = scholarships + internships
recommended_opportunities = recommend_opportunities(interests, skills, all_opportunities)
st.write("AI-based Recommended Opportunities based on your profile:")
for opportunity in recommended_opportunities:
title = translate_text(opportunity.get('title', 'No title available'), target_language)
description = translate_text(opportunity.get('description', 'No description available'), target_language)
eligibility = translate_text(opportunity.get('eligibility', 'Not available'), target_language)
st.write(f"Title: {title}")
st.write(f"Description: {description}")
st.write(f"Eligibility: {eligibility}")
st.write("---")