Spaces:
Sleeping
Sleeping
Update app.py
Browse files22 unmodified lines23242526262728294 unmodified lines343536373837383940414243442 unmodified lines47484948495051525354555657585950516152536354556566565768695859606162717263646566676874757677697071727374757677787980818283848586868788899091929394959697989910010110210310410510610710810911016 unmodified lines1271281291091301311111121131141151161171181191201321331341351364 unmodified lines1411421431311441331451461471481491501511521531541553 unmodified lines1591601611471621631641659 unmodified lines1751761771631781791801661671811821837 unmodified lines19119219318119419519619719819920020120218418518618718818920320420522 unmodified lines df['clean_salary'] = df['Salary'].apply(clean_salary) # Combine title and description for better clustering df['combined_text'] = df['Title'] + ' ' + df['Description'] df['combined_text'] = df['Title'].str.lower() + ' ' + df['Description'].str.lower() return df except Exception as e: st.error(f"Error loading data: {str(e)}")4 unmodified lines # Create TF-IDF vectors from combined text vectorizer = TfidfVectorizer( stop_words='english', max_features=1000, # Limit features to most common terms ngram_range=(1, 2) # Include both unigrams and bigrams max_features=1000, ngram_range=(1, 2) ) # Fit and transform the text data tfidf_matrix = vectorizer.fit_transform(df['combined_text']) # Perform K-means clustering2 unmodified lines random_state=42, n_init=10 ) df['cluster'] = kmeans.fit_predict(tfidf_matrix) # Get the most common terms in each cluster feature_names = vectorizer.get_feature_names_out() cluster_centers = kmeans.cluster_centers_ # Get top terms for each cluster cluster_terms = {} for i in range(num_clusters): top_indices = cluster_centers[i].argsort()[-5:][::-1] # Get top 5 terms top_terms = [feature_names[j] for j in top_indices] cluster_terms[i] = top_terms clusters = kmeans.fit_predict(tfidf_matrix) return vectorizer, kmeans, cluster_terms return vectorizer, kmeans, clusters
def find_matching_jobs(df, job_title, location, min_salary, keywords, vectorizer, kmeans):def find_matching_jobs(df, job_title, location, min_salary, keywords, vectorizer, kmeans, debug=True): """Find matching jobs using K-means clustering.""" # Prepare search query search_query = job_title + ' ' + (keywords if keywords else '') matches = df.copy() # Transform search query using the same vectorizer query_vector = vectorizer.transform([search_query]) # Debug information if debug: st.write("### Debug Information") st.write(f"Initial number of jobs: {len(matches)}") # Predict cluster for the search query query_cluster = kmeans.predict(query_vector)[0] # Apply title filter if provided if 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") # Get jobs from the same cluster matches = df[df['cluster'] == query_cluster].copy() # Apply additional filters # Apply location filter if provided if location: matches = matches[matches['Location'].str.contains(location, case=False, na=False)] if debug: st.write(f"After location filter: {len(matches)} jobs") # Apply salary filter if min_salary > 0: matches = matches[matches['clean_salary'] >= min_salary] if debug: st.write(f"After salary filter: {len(matches)} jobs") # Apply keyword filter if provided if keywords: for keyword in keywords.split(','): keyword = keyword.strip() if keyword: matches = matches[matches['Description'].str.contains(keyword, case=False, na=False)] matches = matches[ matches['Description'].str.contains(keyword, case=False, na=False) | matches['Title'].str.contains(keyword, case=False, na=False) ] if debug: st.write(f"After keyword '{keyword}' filter: {len(matches)} jobs") # Only use K-means if we have search terms if 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 jobs job_vectors = vectorizer.transform(matches['combined_text']) matches['cluster'] = kmeans.predict(job_vectors) # Filter by cluster matches = 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 matches
16 unmodified lines if 'filtered_jobs' not in st.session_state: st.session_state.filtered_jobs = pd.DataFrame() # Create clusters and store cluster information # Create clusters num_clusters = st.sidebar.slider("Number of Clusters (K):", 5, 20, 10) vectorizer, kmeans, cluster_terms = create_job_clusters(jobs_df, num_clusters) # Show cluster information if st.checkbox("Show Cluster Information"): st.subheader("Cluster Analysis") for cluster_id, terms in cluster_terms.items(): st.write(f"Cluster {cluster_id} top terms: {', '.join(terms)}") cluster_size = len(jobs_df[jobs_df['cluster'] == cluster_id]) st.write(f"Number of jobs in cluster: {cluster_size}") st.write("---") vectorizer, kmeans, clusters = create_job_clusters(jobs_df, num_clusters) jobs_df['cluster'] = clusters # Sidebar filters with st.sidebar:4 unmodified lines location = st.text_input("Location:", "") min_salary = st.slider("Minimum Salary:", 0, 300000, 50000, step=5000) st.subheader("Keywords") keywords = st.text_area( "Enter keywords (comma-separated):", "Keywords (comma-separated):", placeholder="e.g., Python, SQL, machine learning", help="Enter keywords to find in job descriptions" ) # Add debug mode checkbox debug_mode = st.checkbox("Show debug information", value=True) search_button = st.form_submit_button( "Search Jobs", use_container_width=True,3 unmodified lines if search_button: st.session_state.filtered_jobs = find_matching_jobs( jobs_df, job_title, location, min_salary, keywords, vectorizer, kmeans vectorizer, kmeans, debug=debug_mode ) st.session_state.search_performed = True 9 unmodified lines if not filtered_jobs.empty: for index, row in filtered_jobs.iterrows(): with st.expander(f"{row['Title']} - {row['Location']} (Cluster {row['cluster']})"): with st.expander(f"{row['Title']} - {row['Location']}"): st.write(f"**Salary:** {row['Salary']}") st.write(f"**Cluster:** {row['cluster']}") st.write("**Top terms in this cluster:**", ', '.join(cluster_terms[row['cluster']])) st.write("**Description:**") description = row['Description'] 7 unmodified lines st.write(description[:500] + "..." if len(description) > 500 else description) else: st.info("No matching jobs found. Try adjusting your filters.") st.warning(""" No matching jobs found. Try: 1. Using fewer filters 2. Using more general keywords 3. Reducing the minimum salary 4. Checking different locations """) else: st.info("Use the filters in the sidebar and click 'Search Jobs' to find matching positions.") # Statistics with st.sidebar: st.header("Statistics") st.write(f"Total jobs in database: {len(jobs_df)}") st.write(f"Average salary: ${int(jobs_df['clean_salary'].mean()):,}")
if __name__ == "__main__": main()app.py+65-52