Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,47 +3,41 @@ import requests
|
|
3 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
4 |
from sklearn.metrics.pairwise import cosine_similarity
|
5 |
from transformers import MarianMTModel, MarianTokenizer
|
|
|
6 |
|
7 |
# Set up the Streamlit page
|
8 |
st.title("AI Opportunity Finder for Youth")
|
9 |
st.write("Find Scholarships, Internships, Online Courses, and more!")
|
10 |
|
11 |
-
#
|
12 |
-
def
|
13 |
-
|
14 |
-
|
15 |
-
model = MarianMTModel.from_pretrained(model_name)
|
16 |
-
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
17 |
-
|
18 |
-
translated = model.generate(**tokenizer(text, return_tensors="pt", padding=True, truncation=True))
|
19 |
-
translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
|
20 |
-
return translated_text
|
21 |
-
|
22 |
-
# Mock function to get data from APIs (replace with actual API calls)
|
23 |
-
def get_scholarships(country, interests):
|
24 |
-
url = f"https://jsonplaceholder.typicode.com/posts" # Mock API (replace with real one)
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
return [{"title": f"
|
29 |
-
elif country == "Germany":
|
30 |
-
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)]
|
31 |
else:
|
32 |
-
return [
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
#
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
41 |
else:
|
42 |
-
return [
|
43 |
|
|
|
44 |
def recommend_opportunities(user_interests, user_skills, opportunities):
|
|
|
45 |
user_profile = [f"{user_interests} {user_skills}"]
|
46 |
-
|
|
|
|
|
47 |
|
48 |
# Vectorize the text using TF-IDF
|
49 |
vectorizer = TfidfVectorizer(stop_words='english')
|
@@ -52,70 +46,105 @@ def recommend_opportunities(user_interests, user_skills, opportunities):
|
|
52 |
# Compute cosine similarity
|
53 |
cosine_sim = cosine_similarity(tfidf_matrix[-1], tfidf_matrix[:-1])
|
54 |
|
55 |
-
# Get the top 5
|
56 |
recommendations = cosine_sim[0].argsort()[-5:][::-1]
|
57 |
|
|
|
58 |
return [opportunities[i] for i in recommendations]
|
59 |
|
60 |
-
#
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
target_language = st.selectbox("Select target language", ['de', 'fr', 'es', 'it', 'pt']) # Available language codes for translation
|
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 |
-
# AI Recommendations based on interests and skills
|
109 |
all_opportunities = scholarships + internships
|
|
|
|
|
110 |
recommended_opportunities = recommend_opportunities(interests, skills, all_opportunities)
|
111 |
|
112 |
-
|
|
|
113 |
for opportunity in recommended_opportunities:
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
st.write(f"Title: {title}")
|
119 |
-
st.write(f"Description: {description}")
|
120 |
-
st.write(f"Eligibility: {eligibility}")
|
121 |
st.write("---")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
from sklearn.feature_extraction.text import TfidfVectorizer
|
4 |
from sklearn.metrics.pairwise import cosine_similarity
|
5 |
from transformers import MarianMTModel, MarianTokenizer
|
6 |
+
import sentencepiece
|
7 |
|
8 |
# Set up the Streamlit page
|
9 |
st.title("AI Opportunity Finder for Youth")
|
10 |
st.write("Find Scholarships, Internships, Online Courses, and more!")
|
11 |
|
12 |
+
# Function to get scholarships data from a mock API
|
13 |
+
def get_scholarships(location, interests):
|
14 |
+
url = "https://jsonplaceholder.typicode.com/posts" # Mock API for testing
|
15 |
+
response = requests.get(url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
if response.status_code == 200:
|
18 |
+
# Return a list of mock scholarships
|
19 |
+
return [{"title": f"Scholarship {i+1}", "description": post['body'], "eligibility": "Any student from any background."} for i, post in enumerate(response.json())[:5]]
|
|
|
|
|
20 |
else:
|
21 |
+
return []
|
22 |
|
23 |
+
# Function to get internships data from a mock API
|
24 |
+
def get_internships():
|
25 |
+
url = "https://jsonplaceholder.typicode.com/posts" # Mock API for testing
|
26 |
+
response = requests.get(url)
|
27 |
+
|
28 |
+
if response.status_code == 200:
|
29 |
+
# Return a list of mock internships
|
30 |
+
return [{"jobtitle": f"Internship {i+1}", "company": "Sample Company", "location": "Remote", "snippet": "Description of the internship."} for i in range(5)]
|
31 |
else:
|
32 |
+
return []
|
33 |
|
34 |
+
# Function to recommend opportunities based on user input
|
35 |
def recommend_opportunities(user_interests, user_skills, opportunities):
|
36 |
+
# Combine user profile into a single string
|
37 |
user_profile = [f"{user_interests} {user_skills}"]
|
38 |
+
|
39 |
+
# Create text data for opportunities based on description & eligibility
|
40 |
+
opportunities_text = [f"{opportunity['description']} {opportunity['eligibility']}" for opportunity in opportunities]
|
41 |
|
42 |
# Vectorize the text using TF-IDF
|
43 |
vectorizer = TfidfVectorizer(stop_words='english')
|
|
|
46 |
# Compute cosine similarity
|
47 |
cosine_sim = cosine_similarity(tfidf_matrix[-1], tfidf_matrix[:-1])
|
48 |
|
49 |
+
# Get the indices of the top 5 recommended opportunities
|
50 |
recommendations = cosine_sim[0].argsort()[-5:][::-1]
|
51 |
|
52 |
+
# Return recommended opportunities
|
53 |
return [opportunities[i] for i in recommendations]
|
54 |
|
55 |
+
# Function to load MarianMT translation model
|
56 |
+
def load_translation_model(target_language):
|
57 |
+
model_name = f'Helsinki-NLP/opus-mt-en-{target_language}'
|
58 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
59 |
+
model = MarianMTModel.from_pretrained(model_name)
|
60 |
+
return model, tokenizer
|
|
|
61 |
|
62 |
+
# Function to translate text using MarianMT
|
63 |
+
def translate_text(text, target_language):
|
64 |
+
try:
|
65 |
+
model, tokenizer = load_translation_model(target_language)
|
66 |
+
|
67 |
+
# Tokenize and translate text
|
68 |
+
tokens = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
69 |
+
translated = model.generate(**tokens)
|
70 |
+
translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
|
71 |
+
|
72 |
+
return translated_text
|
73 |
+
except Exception as e:
|
74 |
+
return f"Error during translation: {str(e)}"
|
75 |
|
76 |
+
# User input for profile
|
77 |
+
st.sidebar.header("User Profile")
|
78 |
+
location = st.sidebar.text_input("Location", "Pakistan") # Default to 'Pakistan'
|
79 |
+
skills = st.sidebar.text_input("Skills (e.g., Python, Marketing)")
|
80 |
+
interests = st.sidebar.text_input("Interests (e.g., Technology, Science)")
|
81 |
+
|
82 |
+
# Fetch scholarships based on user input
|
83 |
+
scholarships = get_scholarships(location, interests)
|
84 |
+
|
85 |
+
# Display scholarships if available
|
86 |
+
if scholarships:
|
87 |
+
st.write("Scholarships found:")
|
88 |
+
for scholarship in scholarships:
|
89 |
+
st.write(f"Title: {scholarship['title']}")
|
90 |
+
st.write(f"Description: {scholarship['description']}")
|
91 |
+
st.write(f"Eligibility: {scholarship['eligibility']}")
|
92 |
+
st.write("---")
|
93 |
+
else:
|
94 |
+
st.write("No scholarships found based on your criteria.")
|
95 |
+
|
96 |
+
# Fetch internships based on user input
|
97 |
+
internships = get_internships()
|
98 |
+
|
99 |
+
# Display internships if available
|
100 |
+
if internships:
|
101 |
+
st.write("Internships found:")
|
102 |
+
for internship in internships:
|
103 |
+
st.write(f"Title: {internship['jobtitle']}")
|
104 |
+
st.write(f"Company: {internship['company']}")
|
105 |
+
st.write(f"Location: {internship['location']}")
|
106 |
+
st.write(f"Snippet: {internship['snippet']}")
|
107 |
+
st.write("---")
|
108 |
+
else:
|
109 |
+
st.write("No internships found.")
|
110 |
+
|
111 |
+
# AI-based recommendations for opportunities
|
112 |
+
if st.sidebar.button("Get AI Recommendations"):
|
113 |
+
# Combine scholarships and internships for recommendations
|
|
|
114 |
all_opportunities = scholarships + internships
|
115 |
+
|
116 |
+
# Get AI recommendations based on user input
|
117 |
recommended_opportunities = recommend_opportunities(interests, skills, all_opportunities)
|
118 |
|
119 |
+
# Display recommended opportunities
|
120 |
+
st.write("Recommended Opportunities based on your profile:")
|
121 |
for opportunity in recommended_opportunities:
|
122 |
+
st.write(f"Title: {opportunity['title']}")
|
123 |
+
st.write(f"Description: {opportunity['description']}")
|
124 |
+
st.write(f"Eligibility: {opportunity.get('eligibility', 'Not available')}")
|
|
|
|
|
|
|
|
|
125 |
st.write("---")
|
126 |
+
|
127 |
+
# Language selection
|
128 |
+
languages = {
|
129 |
+
'English': 'english',
|
130 |
+
'German': 'deutch',
|
131 |
+
'French': 'french',
|
132 |
+
'Spanish': 'spanish',
|
133 |
+
'Italian': 'italian',
|
134 |
+
'Portuguese': 'portugese',
|
135 |
+
'Chinese': 'chinese',
|
136 |
+
'Arabic': 'arabic',
|
137 |
+
'Russian': 'russian',
|
138 |
+
'Japanese': 'japanese',
|
139 |
+
'Korean': 'korean',
|
140 |
+
'Urdu': 'urdu'
|
141 |
+
}
|
142 |
+
|
143 |
+
# Dropdown for language selection
|
144 |
+
selected_language = st.selectbox("Select Language", list(languages.keys()))
|
145 |
+
|
146 |
+
# Translate the opportunity description based on the selected language
|
147 |
+
if selected_language != 'English':
|
148 |
+
# Translate the title of the app or a sample text
|
149 |
+
translated_text = translate_text("Hello, welcome to AI Opportunity Finder!", languages[selected_language])
|
150 |
+
st.write(f"Translated Text ({selected_language}): {translated_text}")
|