MansoorSarookh's picture
Update app.py
93f8224 verified
import streamlit as st
from datasets import load_dataset
import pandas as pd
from transformers import pipeline
import time
import os
# Constants
universities_url = "https://www.4icu.org/top-universities-world/"
# Load datasets with caching to optimize performance
@st.cache_resource
def load_datasets():
# Load datasets from Hugging Face
try:
ds_jobs = load_dataset("lukebarousse/data_jobs")
except ValueError as e:
st.error("Error loading 'lukebarousse/data_jobs'. Please ensure the dataset exists and is accessible.")
ds_jobs = None
try:
ds_courses = load_dataset("azrai99/coursera-course-dataset")
except ValueError as e:
st.error("Error loading 'azrai99/coursera-course-dataset'. Please ensure the dataset exists and is accessible.")
ds_courses = None
# Load local CSV files
csv_files = {
"ds_custom_courses": "final_cleaned_merged_coursera_courses.csv",
"ds_custom_jobs": "merged_data_science_jobs.csv",
"ds_custom_universities": "merged_university_data_cleaned (1).csv"
}
datasets = {}
for name, path in csv_files.items():
if os.path.isfile(path):
datasets[name] = pd.read_csv(path)
else:
st.warning(f"File '{path}' not found. Please check if it is available in the app directory.")
datasets[name] = None
return ds_jobs, ds_courses, datasets.get("ds_custom_courses"), datasets.get("ds_custom_jobs"), datasets.get("ds_custom_universities")
# Load datasets and handle None cases if they don't load
ds_jobs, ds_courses, ds_custom_courses, ds_custom_jobs, ds_custom_universities = load_datasets()
# Initialize the pipeline with caching
@st.cache_resource
def load_pipeline():
return pipeline("text2text-generation", model="google/flan-t5-large")
qa_pipeline = load_pipeline()
# Streamlit App Interface
st.title("Career Counseling Application")
st.subheader("Build Your Profile and Discover Tailored Career Recommendations")
# Sidebar for Profile Setup
st.sidebar.header("Profile Setup")
educational_background = st.sidebar.text_input("Educational Background (e.g., Degree, Major)")
interests = st.sidebar.text_input("Interests (e.g., AI, Data Science, Engineering)")
tech_skills = st.sidebar.text_area("Technical Skills (e.g., Python, SQL, Machine Learning)")
soft_skills = st.sidebar.text_area("Soft Skills (e.g., Communication, Teamwork)")
# Save profile data for session-based recommendations
if st.sidebar.button("Save Profile"):
with st.spinner('Saving your profile...'):
time.sleep(2) # Simulate processing time
st.session_state.profile_data = {
"educational_background": educational_background,
"interests": interests,
"tech_skills": tech_skills,
"soft_skills": soft_skills
}
st.sidebar.success("Profile saved successfully!")
st.session_state.show_questions = True # Show questions after profile save
# Check if the profile has been saved
if "profile_data" in st.session_state:
# Show question section if profile is saved
if "show_questions" in st.session_state and st.session_state.show_questions:
st.header("Questionnaire")
st.write("Please answer these questions to help us make more accurate recommendations.")
# List of 10 questions
questions = [
"What do you see yourself achieving in the next five years?",
"Which skills would you like to develop further? (e.g., leadership, technical expertise, communication)",
"Do you prefer a structured routine or a more flexible, varied work environment?",
"What’s most important to you in a job? (e.g., work-life balance, job stability, opportunities for growth, impact on society)",
"What types of projects or tasks energize you? (e.g., solving complex problems, helping others, creating something new)",
"Are you comfortable with roles that may involve public speaking or presenting ideas?",
"How do you handle stress or pressure in a work setting? (Select options: I thrive under pressure, I manage well, I prefer lower-stress environments)",
"Would you be open to relocation or travel for your job?",
"Do you prioritize high salary potential or job satisfaction when considering a career?",
"What kind of work culture are you drawn to? (e.g., collaborative, competitive, mission-driven, innovative)"
]
# Collect responses
answers = []
for i, question in enumerate(questions):
answers.append(st.text_input(f"Q{i+1}: {question}", key=f"question_{i}"))
# Submit questions
if st.button("Submit Questionnaire"):
st.session_state.answers = answers
st.session_state.show_questions = False # Hide questions after submission
st.success("Thank you for submitting your answers!")
# Proceed to recommendation sections if questions are answered
if "answers" in st.session_state:
# Intelligent Q&A Section
st.header("Intelligent Q&A")
question = st.text_input("Ask a career-related question:")
if question:
with st.spinner('Processing your question...'):
answer = qa_pipeline(question)[0]["generated_text"]
time.sleep(2) # Simulate processing time
st.write("Answer:", answer)
# Career and Job Recommendations Section
st.header("Job Recommendations")
with st.spinner('Generating job recommendations...'):
time.sleep(2) # Simulate processing time
job_recommendations = []
# Find jobs from ds_jobs if available
if ds_jobs:
for job in ds_jobs["train"]:
job_title = job.get("job_title_short", "Unknown Job Title")
job_skills = job.get("job_skills", "") or ""
if any(skill.lower() in job_skills.lower() for skill in st.session_state.profile_data["tech_skills"].split(",")):
job_recommendations.append(job_title)
# Find jobs from ds_custom_jobs if available
if ds_custom_jobs is not None:
for _, job in ds_custom_jobs.iterrows():
job_title = job.get("job_title", "Unknown Job Title")
job_skills = job.get("skills", "") or ""
if any(skill.lower() in job_skills.lower() for skill in st.session_state.profile_data["tech_skills"].split(",")):
job_recommendations.append(job_title)
# Remove duplicates
job_recommendations = list(set(job_recommendations))
if job_recommendations:
st.subheader("Based on your profile, here are some potential job roles:")
for job in job_recommendations[:5]: # Limit to top 5 job recommendations
st.write("- ", job)
else:
st.write("No specific job recommendations found matching your profile.")
# Course Suggestions Section
st.header("Recommended Courses")
with st.spinner('Finding courses related to your profile...'):
time.sleep(2)
course_recommendations = []
# Find relevant courses in ds_courses if available
if ds_courses:
for course in ds_courses["train"]:
if any(interest.lower() in course.get("Course Name", "").lower() for interest in st.session_state.profile_data["interests"].split(",")):
course_recommendations.append({
"name": course.get("Course Name", "Unknown Course Title"),
"url": course.get("Links", "#")
})
# Find relevant courses in ds_custom_courses if available
if ds_custom_courses is not None:
for _, row in ds_custom_courses.iterrows():
if any(interest.lower() in row["Course Name"].lower() for interest in st.session_state.profile_data["interests"].split(",")):
course_recommendations.append({
"name": row["Course Name"],
"url": row.get("Links", "#")
})
# Remove duplicates
course_recommendations = list({(course["name"], course["url"]) for course in course_recommendations})
if course_recommendations:
st.write("Here are the top 5 courses related to your interests:")
for course in course_recommendations[:5]:
st.write(f"- [{course[0]}]({course[1]})")
# University Recommendations Section
st.header("Top Universities")
st.write("For further education, you can explore the top universities worldwide:")
st.write(f"[View Top Universities Rankings]({universities_url})")
# Conclusion
st.write("Thank you for using the Career Counseling Application!")