Spaces:
Sleeping
Sleeping
Update app.py
Browse files28 unmodified lines2930313233343536373839404142434445464748495051525354555657581 unmodified line60616263646566676869707118 unmodified lines909192939495969798991001011021031041051061071081091101111121131141151161171188 unmodified lines12712812913013113213313413513613720 unmodified lines15815916016116216316416511 unmodified lines17717817918018118218328 unmodified linesst.error(f"Error loading data: {str(e)}")return Nonedef create_job_clusters(df, num_clusters=10):"""Create job clusters using TF-IDF and K-means."""# Create TF-IDF vectors from combined textvectorizer = TfidfVectorizer(stop_words='english',max_features=1000,ngram_range=(1, 2))# Fit and transform the text datatfidf_matrix = vectorizer.fit_transform(df['combined_text'])# Perform K-means clusteringkmeans = KMeans(n_clusters=num_clusters,random_state=42,n_init=10)clusters = kmeans.fit_predict(tfidf_matrix)return vectorizer, kmeans, clustersdef find_matching_jobs(df, job_title, location, min_salary, keywords, vectorizer, kmeans, debug=True):"""Find matching jobs using K-means clustering."""matches = df.copy()# Debug information1 unmodified linest.write("### Debug Information")st.write(f"Initial number of jobs: {len(matches)}")# Apply title filter if providedif job_title:matches = matches[matches['Title'].str.contains(job_title, case=False, na=False)]if debug:st.write(f"After title filter: {len(matches)} jobs")# Apply location filter if providedif location:matches = matches[matches['Location'].str.contains(location, case=False, na=False)]18 unmodified linesif debug:st.write(f"After keyword '{keyword}' filter: {len(matches)} jobs")# Only use K-means if we have search termsif job_title or keywords:search_query = (job_title + ' ' + (keywords if keywords else '')).lower()query_vector = vectorizer.transform([search_query])query_cluster = kmeans.predict(query_vector)[0]# Get cluster assignment for all remaining jobsjob_vectors = vectorizer.transform(matches['combined_text'])matches['cluster'] = kmeans.predict(job_vectors)# Filter by clustermatches = matches[matches['cluster'] == query_cluster]if debug:st.write(f"After cluster filtering: {len(matches)} jobs")st.write(f"Query assigned to cluster: {query_cluster}")return matchesdef main():st.set_page_config(page_title="K-means Job Clustering Search", layout="wide")st.title("Job Search with K-means Clustering")st.write("Find jobs using K-means clustering algorithm!")# Load dataurl = "https://huggingface.co/datasets/burcuonel/datajob_indeed_usa/raw/main/datajob_indeed_usa.csv"8 unmodified linesif 'filtered_jobs' not in st.session_state:st.session_state.filtered_jobs = pd.DataFrame()# Create clustersnum_clusters = st.sidebar.slider("Number of Clusters (K):", 5, 20, 10)vectorizer, kmeans, clusters = create_job_clusters(jobs_df, num_clusters)jobs_df['cluster'] = clusters# Sidebar filterswith st.sidebar:st.header("Search Filters")20 unmodified linesif search_button:st.session_state.filtered_jobs = find_matching_jobs(jobs_df, job_title, location, min_salary, keywords,vectorizer, kmeans, debug=debug_mode)st.session_state.search_performed = True11 unmodified linesfor index, row in filtered_jobs.iterrows():with st.expander(f"{row['Title']} - {row['Location']}"):st.write(f"**Salary:** {row['Salary']}")st.write(f"**Cluster:** {row['cluster']}")st.write("**Description:**")description = row['Description']28 unmodified lines29303132333435361 unmodified line38394041424318 unmodified lines62636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091108 unmodified lines11912012112212312420 unmodified lines14514614714814915015111 unmodified lines16316416516616716816917028 unmodified linesst.error(f"Error loading data: {str(e)}")return Nonedef find_matching_jobs(df, job_title, location, min_salary, keywords, debug=True):"""Find matching jobs using simple filtering first."""matches = df.copy()# Debug information1 unmodified linest.write("### Debug Information")st.write(f"Initial number of jobs: {len(matches)}")# Apply location filter if providedif location:matches = matches[matches['Location'].str.contains(location, case=False, na=False)]18 unmodified linesif debug:st.write(f"After keyword '{keyword}' filter: {len(matches)} jobs")# Check if we have any matches before applying K-meansif len(matches) == 0:if debug:st.warning("No jobs match the basic filters. Try relaxing your search criteria.")return matches# Only apply title and K-means clustering if we have matchesif job_title:# Create TF-IDF vectorsvectorizer = TfidfVectorizer(stop_words='english',max_features=1000,ngram_range=(1, 2))# Fit vectorizer on the remaining matchestfidf_matrix = vectorizer.fit_transform(matches['combined_text'])# Create clustersnum_clusters = min(5, len(matches)) # Adjust number of clusters based on matcheskmeans = KMeans(n_clusters=num_clusters, random_state=42, n_init=10)clusters = kmeans.fit_predict(tfidf_matrix)# Transform search querysearch_query = job_title.lower()query_vector = vectorizer.transform([search_query])query_cluster = kmeans.predict(query_vector)[0]# Assign clusters and filtermatches['cluster'] = clustersmatches = matches[matches['cluster'] == query_cluster]if debug:st.write(f"After clustering filter: {len(matches)} jobs")st.write(f"Query assigned to cluster: {query_cluster}")return matchesdef main():st.set_page_config(page_title="Job Search Application", layout="wide")st.title("Job Search Application")st.write("Find jobs that match your preferences!")# Load dataurl = "https://huggingface.co/datasets/burcuonel/datajob_indeed_usa/raw/main/datajob_indeed_usa.csv"8 unmodified linesif 'filtered_jobs' not in st.session_state:st.session_state.filtered_jobs = pd.DataFrame()# Sidebar filterswith st.sidebar:st.header("Search Filters")20 unmodified linesif search_button:st.session_state.filtered_jobs = find_matching_jobs(jobs_df, job_title, location, min_salary, keywords, debug=debug_mode)st.session_state.search_performed = True11 unmodified linesfor index, row in filtered_jobs.iterrows():with st.expander(f"{row['Title']} - {row['Location']}"):st.write(f"**Salary:** {row['Salary']}")if 'cluster' in row:st.write(f"**Cluster:** {row['cluster']}")st.write("**Description:**")description = row['Description']
app.py+37-50