Spaces:
Sleeping
Sleeping
| from sentence_transformers import SentenceTransformer | |
| from sklearn.metrics.pairwise import cosine_similarity | |
| import numpy as np | |
| import streamlit as st | |
| # Use a singleton pattern to load the model only once. | |
| def get_sentence_transformer_model(): | |
| """Loads and caches the SentenceTransformer model.""" | |
| try: | |
| model = SentenceTransformer('all-MiniLM-L6-v2') | |
| return model | |
| except Exception as e: | |
| st.error(f"Error loading embedding model: {e}") | |
| return None | |
| def get_embedding(text: str, api_key: str = None, model_name: str = None): | |
| """ | |
| Generates embeddings for a given text using a local SentenceTransformer model. | |
| The api_key and model_name arguments are kept for compatibility but are no longer used. | |
| Args: | |
| text (str): The input text to embed. | |
| Returns: | |
| np.ndarray: A numpy array representing the embedding. | |
| """ | |
| model = get_sentence_transformer_model() | |
| if model is None: | |
| return None | |
| try: | |
| # The model directly outputs a numpy array. | |
| embedding = model.encode(text) | |
| return embedding | |
| except Exception as e: | |
| print(f"Error generating embedding: {e}") | |
| raise | |
| def calculate_match_score(resume_embedding: np.ndarray, job_embedding: np.ndarray) -> float: | |
| """ | |
| Calculates the cosine similarity between two embeddings. | |
| Args: | |
| resume_embedding (np.ndarray): The embedding of the resume. | |
| job_embedding (np.ndarray): The embedding of the job description. | |
| Returns: | |
| float: The cosine similarity score (0.0 to 1.0). | |
| """ | |
| if resume_embedding is None or job_embedding is None: | |
| return 0.0 | |
| # Reshape arrays to be 2D for cosine_similarity function | |
| resume_embedding = resume_embedding.reshape(1, -1) | |
| job_embedding = job_embedding.reshape(1, -1) | |
| # Calculate cosine similarity | |
| score = cosine_similarity(resume_embedding, job_embedding)[0][0] | |
| # Ensure the score is within the 0-1 range | |
| return max(0.0, min(1.0, score)) |