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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -94
app.py CHANGED
@@ -1,150 +1,144 @@
1
  import streamlit as st
2
  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
- 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')
44
- tfidf_matrix = vectorizer.fit_transform(opportunities_text + user_profile)
 
45
 
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}")
 
1
  import streamlit as st
2
  import requests
 
 
 
 
3
 
4
  # Set up the Streamlit page
5
  st.title("AI Opportunity Finder for Youth")
6
  st.write("Find Scholarships, Internships, Online Courses, and more!")
7
 
8
+ # Groq API URL and Authentication
9
+ GROQ_API_URL = "https://api.groq.ai"
10
+ API_KEY = "YOUR_GROQ_API_KEY" # Replace with your actual Groq API Key
11
+
12
+ # Function to fetch data from Groq API for scholarships
13
  def get_scholarships(location, interests):
14
+ url = f"{GROQ_API_URL}/scholarships"
15
+ headers = {
16
+ 'Authorization': f'Bearer {API_KEY}',
17
+ 'Content-Type': 'application/json'
18
+ }
19
+ params = {"location": location, "interests": interests}
20
+
21
+ response = requests.get(url, headers=headers, params=params)
22
 
23
  if response.status_code == 200:
24
+ scholarships = response.json()
25
+ return [{"title": scholarship['title'], "description": scholarship['description'], "eligibility": scholarship['eligibility']} for scholarship in scholarships]
26
  else:
27
  return []
28
 
29
+ # Function to fetch data from Groq API for internships
30
  def get_internships():
31
+ url = f"{GROQ_API_URL}/internships"
32
+ headers = {
33
+ 'Authorization': f'Bearer {API_KEY}',
34
+ 'Content-Type': 'application/json'
35
+ }
36
+
37
+ response = requests.get(url, headers=headers)
38
 
39
  if response.status_code == 200:
40
+ internships = response.json()
41
+ return [{"jobtitle": internship['jobtitle'], "company": internship['company'], "location": internship['location'], "snippet": internship['description']} for internship in internships]
42
  else:
43
  return []
44
 
45
+ # Function to recommend opportunities using Groq's recommendation system
46
+ def recommend_opportunities(user_interests, user_skills, scholarships, internships):
47
+ # Combine user interests and skills into a profile for recommendation
48
+ user_profile = f"{user_interests} {user_skills}"
49
 
50
+ # API request to get recommendations (assumed Groq API endpoint)
51
+ url = f"{GROQ_API_URL}/recommendations"
52
+ headers = {
53
+ 'Authorization': f'Bearer {API_KEY}',
54
+ 'Content-Type': 'application/json'
55
+ }
56
 
57
+ data = {
58
+ "profile": user_profile,
59
+ "opportunities": scholarships + internships
60
+ }
61
 
62
+ response = requests.post(url, json=data, headers=headers)
 
63
 
64
+ if response.status_code == 200:
65
+ recommended_opportunities = response.json()
66
+ return recommended_opportunities
67
+ else:
68
+ return []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
+ # Streamlit Sidebar: User Profile Input
71
  st.sidebar.header("User Profile")
72
  location = st.sidebar.text_input("Location", "Pakistan") # Default to 'Pakistan'
73
  skills = st.sidebar.text_input("Skills (e.g., Python, Marketing)")
74
  interests = st.sidebar.text_input("Interests (e.g., Technology, Science)")
75
 
76
+ # Fetch Scholarships and Internships
77
  scholarships = get_scholarships(location, interests)
78
+ internships = get_internships()
79
 
80
+ # Display Scholarships if available
81
  if scholarships:
82
  st.write("Scholarships found:")
83
  for scholarship in scholarships:
84
+ st.write(f"**Title:** {scholarship['title']}")
85
+ st.write(f"**Description:** {scholarship['description']}")
86
+ st.write(f"**Eligibility:** {scholarship['eligibility']}")
87
  st.write("---")
88
  else:
89
  st.write("No scholarships found based on your criteria.")
90
 
91
+ # Display Internships if available
 
 
 
92
  if internships:
93
  st.write("Internships found:")
94
  for internship in internships:
95
+ st.write(f"**Title:** {internship['jobtitle']}")
96
+ st.write(f"**Company:** {internship['company']}")
97
+ st.write(f"**Location:** {internship['location']}")
98
+ st.write(f"**Snippet:** {internship['snippet']}")
99
  st.write("---")
100
  else:
101
+ st.write("No internships found based on your criteria.")
102
 
103
+ # AI-based Recommendations for Opportunities
104
  if st.sidebar.button("Get AI Recommendations"):
105
+ recommended_opportunities = recommend_opportunities(interests, skills, scholarships, internships)
 
 
 
 
106
 
107
+ if recommended_opportunities:
108
+ st.write("Recommended Opportunities based on your profile:")
109
+ for opportunity in recommended_opportunities:
110
+ st.write(f"**Title:** {opportunity['title']}")
111
+ st.write(f"**Description:** {opportunity['description']}")
112
+ st.write(f"**Eligibility:** {opportunity.get('eligibility', 'Not available')}")
113
+ st.write("---")
114
+ else:
115
+ st.write("No AI recommendations found.")
116
 
117
+ # Language selection for translation (optional)
118
  languages = {
119
+ 'English': 'English',
120
+ 'German': 'German',
121
+ 'French': 'French',
122
+ 'Spanish': 'Spanish',
123
+ 'Italian': 'Italian',
124
+ 'Portuguese': 'Portuguese',
125
+ 'Chinese': 'Chinese',
126
+ 'Arabic': 'Arabic',
127
+ 'Russian': 'Russian',
128
+ 'Japanese': 'Japanese',
129
+ 'Korean': 'Korean',
130
+ 'Urdu': 'Urdu'
131
  }
132
 
133
  # Dropdown for language selection
134
  selected_language = st.selectbox("Select Language", list(languages.keys()))
135
 
136
+ # Translate text (Optional)
137
+ def translate_text(text, target_language):
138
+ # Example translation logic (replace with actual API call if necessary)
139
+ return f"Translated {text} to {target_language}"
140
+
141
+ # Display the translated text (if selected language is not English)
142
  if selected_language != 'English':
 
143
  translated_text = translate_text("Hello, welcome to AI Opportunity Finder!", languages[selected_language])
144
  st.write(f"Translated Text ({selected_language}): {translated_text}")