tahirsher commited on
Commit
49e3224
1 Parent(s): 8b747fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -106
app.py CHANGED
@@ -1,111 +1,92 @@
1
- import streamlit as st
 
2
  from transformers import pipeline
3
  from datasets import load_dataset
4
- import signal
5
-
6
- # Timeout and Exception Handling
7
- class TimeoutException(Exception):
8
- pass
9
-
10
- def timeout_handler(signum, frame):
11
- raise TimeoutException("Operation timed out!")
12
-
13
- # Set a timeout handler
14
- signal.signal(signal.SIGALRM, timeout_handler)
15
-
16
- # Caching function for the model to avoid reloading
17
- @st.cache_resource
18
- def load_qa_model():
19
- with st.spinner("Loading question-answering model..."):
20
- try:
21
- signal.alarm(30) # Set a 30-second timeout for model loading
22
- model = pipeline("question-answering", model="distilbert-base-uncased") # Smaller model for faster loading
23
- signal.alarm(0) # Cancel the alarm if loaded successfully
24
- st.success("Model loaded successfully!")
25
- return model
26
- except TimeoutException:
27
- st.error("Model loading timed out. Please try again later.")
28
- return None
29
-
30
- # Load QA Model
31
- qa_pipeline = load_qa_model()
32
-
33
- # Caching function for loading datasets
34
- @st.cache_data
35
- def load_job_dataset():
36
- with st.spinner("Loading job dataset..."):
37
- return load_dataset("lukebarousse/data_jobs", split="train[:100]") # Smaller sample for faster loading
38
-
39
- def load_course_dataset():
40
- with st.spinner("Loading course dataset..."):
41
- return load_dataset("azrai99/coursera-course-dataset", split="train[:50]") # Smaller sample for testing
42
 
43
  # Load datasets
44
- job_dataset = load_job_dataset()
45
- course_dataset = load_course_dataset()
46
-
47
- # Helper function for question generation
48
- def generate_questions(profile_data):
49
- questions = [
50
- f"What skills do you have in {profile_data.get('tech_skills', 'technology')}?",
51
- "What are your preferred working conditions?",
52
- "What motivates you in a job?",
53
- "Are you interested in work-from-home opportunities?",
54
- "Do you have a preference for job location?",
55
- "Are you open to roles requiring a degree?",
56
- "Do you have a preferred organization size?",
57
- "What are your expected salary requirements?",
58
- "What challenges do you enjoy solving?",
59
- "Do you have experience with any specific programming languages or tools?"
60
- ]
61
- return questions
62
-
63
- # Streamlit Interface
64
- st.title("Career and Course Recommendations App")
65
-
66
- # Profile Setup Section
67
- st.header("Profile Setup")
68
- profile_data = {}
69
- profile_data["name"] = st.text_input("Your Name")
70
- profile_data["tech_skills"] = st.text_input("Technical Skills (comma-separated)")
71
- profile_data["preferred_location"] = st.text_input("Preferred Location")
72
- profile_data["work_preference"] = st.selectbox("Work Preference", ["On-site", "Remote", "Hybrid"])
73
-
74
- # Intelligent Q&A Session
75
- st.header("Intelligent Q&A Session")
76
- if st.button("Start Q&A Session"):
77
- questions = generate_questions(profile_data)
78
- answers = []
79
- for i, question in enumerate(questions):
80
- answer = st.text_input(f"Q{i+1}: {question}")
81
- answers.append(answer)
82
-
83
- profile_data["answers"] = answers
84
- st.success("Q&A Session Completed!")
85
-
86
- # Career Recommendations Based on Profile
87
- st.header("Career Recommendations")
88
- if qa_pipeline and "tech_skills" in profile_data:
89
- job_recommendations = [
90
- job["job_title"]
91
- for job in job_dataset
92
- if "job_skills" in job and any(skill.lower() in job["job_skills"].lower() for skill in profile_data["tech_skills"].split(","))
93
- ]
94
- st.write("Recommended Jobs:")
95
- for job in job_recommendations[:5]: # Limit to 5 recommendations
96
- st.write(f"- {job}")
97
-
98
- # Course Recommendations Based on Profile
99
- st.header("Course Recommendations")
100
- if qa_pipeline and "tech_skills" in profile_data:
101
- course_recommendations = [
102
- course["Title"]
103
- for course in course_dataset
104
- if "Skills" in course and any(skill.lower() in course["Skills"].lower() for skill in profile_data["tech_skills"].split(","))
105
- ]
106
- st.write("Recommended Courses:")
107
- for course in course_recommendations[:5]: # Limit to 5 recommendations
108
- st.write(f"- {course}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
- # Debugging: Log Profile Data
111
- st.write("Profile Data:", profile_data)
 
1
+ import threading
2
+ import time # Simulate a long task for demonstration
3
  from transformers import pipeline
4
  from datasets import load_dataset
5
+ import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  # Load datasets
8
+ jobs_dataset = load_dataset("lukebarousse/data_jobs")["train"]
9
+ universities_url = "https://www.4icu.org/top-universities-world/"
10
+ courses_dataset = load_dataset("azrai99/coursera-course-dataset")["train"]
11
+
12
+ # Function to handle long-running tasks with timeout
13
+ def run_with_timeout(target_func, timeout, *args, **kwargs):
14
+ result = [None]
15
+ exception = [None]
16
+
17
+ def wrapper():
18
+ try:
19
+ result[0] = target_func(*args, **kwargs)
20
+ except Exception as e:
21
+ exception[0] = e
22
+
23
+ thread = threading.Thread(target=wrapper)
24
+ thread.start()
25
+ thread.join(timeout=timeout)
26
+
27
+ if thread.is_alive():
28
+ st.warning("The operation timed out. Please try again.")
29
+ return None
30
+ if exception[0]:
31
+ raise exception[0]
32
+ return result[0]
33
+
34
+ # Example function to simulate a long task (you can replace it with your actual task)
35
+ def example_long_task():
36
+ time.sleep(10) # Simulating a task that takes 10 seconds
37
+ return "Task completed successfully."
38
+
39
+ # Load QA pipeline for Q&A session
40
+ qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad")
41
+
42
+ # Streamlit UI
43
+ st.title("Intelligent Career & Course Recommendation System")
44
+
45
+ # Profile setup
46
+ st.subheader("Profile Setup")
47
+ profile_data = {
48
+ "name": st.text_input("Enter your name"),
49
+ "interests": st.text_input("List your interests (comma-separated)"),
50
+ "tech_skills": st.text_input("List your technical skills (comma-separated)"),
51
+ }
52
+
53
+ if st.button("Save Profile"):
54
+ if profile_data["name"] and profile_data["interests"] and profile_data["tech_skills"]:
55
+ st.session_state.profile_data = profile_data
56
+ st.success("Profile saved successfully!")
57
+ else:
58
+ st.warning("Please fill in all fields.")
59
+
60
+ # Q&A session after profile setup
61
+ if "profile_data" in st.session_state:
62
+ st.subheader("Q&A Session")
63
+ question = st.text_input("Ask a question about your career or courses:")
64
+
65
+ if st.button("Submit Question"):
66
+ if question:
67
+ answer = run_with_timeout(qa_pipeline, timeout=5, question=question, context=str(jobs_dataset)) # Using jobs dataset as context
68
+ if answer:
69
+ st.write(f"Answer: {answer['answer']}")
70
+ else:
71
+ st.warning("Please enter a question.")
72
+
73
+ # Job and course recommendations based on interests and skills
74
+ if "profile_data" in st.session_state:
75
+ st.subheader("Career and Course Recommendations")
76
+ interests = [interest.strip().lower() for interest in st.session_state.profile_data["interests"].split(",")]
77
+ tech_skills = [skill.strip().lower() for skill in st.session_state.profile_data["tech_skills"].split(",")]
78
+
79
+ # Job Recommendations
80
+ st.write("### Job Recommendations:")
81
+ for job in jobs_dataset:
82
+ job_skills = [skill.lower() for skill in job["job_skills"]]
83
+ if any(skill in job_skills for skill in tech_skills):
84
+ st.write(f"- **{job['job_title']}** at {job['company_name']}, Location: {job['job_location']}")
85
+
86
+ # Course Recommendations
87
+ st.write("### Course Recommendations:")
88
+ for course in courses_dataset:
89
+ course_skills = [skill.lower() for skill in course["Skills"]]
90
+ if any(interest in course["Title"].lower() for interest in interests):
91
+ st.write(f"- **{course['Title']}** by {course['Organization']}. [Link to course]({course['course_url']})")
92