saherPervaiz commited on
Commit
5a4ed29
·
verified ·
1 Parent(s): f2485a8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -85
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
- # Language Translation Function
12
- def translate_text(text, target_lang='de'):
13
- # Use Hugging Face's MarianMT for translation
14
- model_name = f'Helsinki-NLP/opus-mt-en-{target_lang}'
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
- # Simulate API response based on country
27
- if country == "USA":
28
- 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)]
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 [{"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)]
33
 
34
- def get_internships(country):
35
- url = f"https://jsonplaceholder.typicode.com/posts" # Mock API for testing
36
- # Simulate internships data
37
- if country == "USA":
38
- return [{"jobtitle": f"Internship {i+1}", "company": "USA Company", "location": "USA", "snippet": "Description of internship in USA."} for i in range(5)]
39
- elif country == "Germany":
40
- return [{"jobtitle": f"Internship {i+1}", "company": "Germany Company", "location": "Germany", "snippet": "Description of internship in Germany."} for i in range(5)]
 
41
  else:
42
- return [{"jobtitle": f"Internship {i+1}", "company": "Sample Company", "location": "Remote", "snippet": "Description of internship."} for i in range(5)]
43
 
 
44
  def recommend_opportunities(user_interests, user_skills, opportunities):
 
45
  user_profile = [f"{user_interests} {user_skills}"]
46
- opportunities_text = [f"{opportunity.get('description', 'No description available')} {opportunity.get('eligibility', 'No eligibility available')}" for opportunity in opportunities]
 
 
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 recommendations
56
  recommendations = cosine_sim[0].argsort()[-5:][::-1]
57
 
 
58
  return [opportunities[i] for i in recommendations]
59
 
60
- # Form to gather user profile and country selection
61
- with st.form(key='user_form'):
62
- st.sidebar.header("User Profile")
63
- location = st.selectbox("Select your Country", ["USA", "Germany", "UK", "India", "Australia", "Pakistan"]) # You can add more countries here
64
- skills = st.text_input("Skills (e.g., Python, Marketing)")
65
- interests = st.text_input("Interests (e.g., Technology, Science)")
66
- target_language = st.selectbox("Select target language", ['de', 'fr', 'es', 'it', 'pt']) # Available language codes for translation
67
 
68
- submit_button = st.form_submit_button("Find Opportunities")
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
- # Fetch data based on the user input
71
- if submit_button:
72
- # Fetch scholarships and internships based on the selected country and profile
73
- scholarships = get_scholarships(location, interests)
74
- internships = get_internships(location)
75
-
76
- # Display Scholarships
77
- if scholarships:
78
- st.write("Scholarships found:")
79
- for scholarship in scholarships:
80
- title = translate_text(scholarship.get('title', 'No title available'), target_language)
81
- description = translate_text(scholarship.get('description', 'No description available'), target_language)
82
- eligibility = translate_text(scholarship.get('eligibility', 'No eligibility available'), target_language)
83
-
84
- st.write(f"Title: {title}")
85
- st.write(f"Description: {description}")
86
- st.write(f"Eligibility: {eligibility}")
87
- st.write("---")
88
- else:
89
- st.write("No scholarships found for the selected country.")
90
-
91
- # Display Internships
92
- if internships:
93
- st.write("Internships found:")
94
- for internship in internships:
95
- title = translate_text(internship.get('jobtitle', 'No title available'), target_language)
96
- company = translate_text(internship.get('company', 'No company available'), target_language)
97
- location = translate_text(internship.get('location', 'No location available'), target_language)
98
- snippet = translate_text(internship.get('snippet', 'No snippet available'), target_language)
99
-
100
- st.write(f"Title: {title}")
101
- st.write(f"Company: {company}")
102
- st.write(f"Location: {location}")
103
- st.write(f"Snippet: {snippet}")
104
- st.write("---")
105
- else:
106
- st.write("No internships found for the selected country.")
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
- st.write("AI-based Recommended Opportunities based on your profile:")
 
113
  for opportunity in recommended_opportunities:
114
- title = translate_text(opportunity.get('title', 'No title available'), target_language)
115
- description = translate_text(opportunity.get('description', 'No description available'), target_language)
116
- eligibility = translate_text(opportunity.get('eligibility', 'Not available'), target_language)
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}")