tahirsher's picture
Update app.py
884344e verified
raw
history blame
4.1 kB
import threading
import time # Simulate a long task for demonstration
from transformers import pipeline
from datasets import load_dataset
import streamlit as st
# Load datasets
jobs_dataset = load_dataset("lukebarousse/data_jobs")["train"]
universities_url = "https://www.4icu.org/top-universities-world/"
courses_dataset = load_dataset("azrai99/coursera-course-dataset")["train"]
# Function to handle long-running tasks with timeout
def run_with_timeout(target_func, timeout, *args, **kwargs):
result = [None]
exception = [None]
def wrapper():
try:
result[0] = target_func(*args, **kwargs)
except Exception as e:
exception[0] = e
thread = threading.Thread(target=wrapper)
thread.start()
thread.join(timeout=timeout)
if thread.is_alive():
st.warning("The operation timed out. Please try again.")
return None
if exception[0]:
raise exception[0]
return result[0]
# Load QA pipeline for Q&A session
qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
# Streamlit UI
st.title("Intelligent Career & Course Recommendation System")
# Profile setup
st.subheader("Profile Setup")
profile_data = {
"name": st.text_input("Enter your name"),
"interests": st.text_input("List your interests (comma-separated)"),
"tech_skills": st.text_input("List your technical skills (comma-separated)"),
}
if st.button("Save Profile"):
if profile_data["name"] and profile_data["interests"] and profile_data["tech_skills"]:
st.session_state.profile_data = profile_data
st.success("Profile saved successfully!")
else:
st.warning("Please fill in all fields.")
# Q&A session after profile setup
if "profile_data" in st.session_state:
st.subheader("Q&A Session")
question = st.text_input("Ask a question about your career or courses:")
if st.button("Submit Question"):
if question:
# Filter context based on interests or skills
relevant_jobs = [job for job in jobs_dataset if any(interest in job["job_title"].lower() for interest in [i.strip().lower() for i in st.session_state.profile_data["interests"].split(",")])]
context = " ".join([job["job_description"] for job in relevant_jobs if "job_description" in job]) # Creating a context from relevant job descriptions
if context:
answer = run_with_timeout(qa_pipeline, timeout=10, question=question, context=context) # Using a longer timeout
if answer:
st.write(f"Answer: {answer['answer']}")
else:
st.warning("No relevant jobs found to answer your question.")
else:
st.warning("Please enter a question.")
# Job and course recommendations based on interests and skills
if "profile_data" in st.session_state:
st.subheader("Career and Course Recommendations")
interests = [interest.strip().lower() for interest in st.session_state.profile_data["interests"].split(",")]
tech_skills = [skill.strip().lower() for skill in st.session_state.profile_data["tech_skills"].split(",")]
# Job Recommendations
st.write("### Job Recommendations:")
for job in jobs_dataset:
job_skills = job.get("job_skills", [])
if job_skills is not None: # Check if job_skills is not None
job_skills = [skill.lower() for skill in job_skills] # Lowercase the skills
if any(skill in job_skills for skill in tech_skills):
st.write(f"- **{job['job_title']}** at {job['company_name']}, Location: {job['job_location']}")
# Course Recommendations
st.write("### Course Recommendations:")
for course in courses_dataset:
course_skills = [skill.lower() for skill in course["Skills"]] if course["Skills"] is not None else []
if any(interest in course["Title"].lower() for interest in interests):
st.write(f"- **{course['Title']}** by {course['Organization']}. [Link to course]({course['course_url']})")