Update app.py
Browse files
app.py
CHANGED
@@ -1,111 +1,92 @@
|
|
1 |
-
import
|
|
|
2 |
from transformers import pipeline
|
3 |
from datasets import load_dataset
|
4 |
-
import
|
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 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
st.
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
st.
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
st.
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|