CR7CAD commited on
Commit
b0dca97
·
verified ·
1 Parent(s): 36a6684

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -13
app.py CHANGED
@@ -1,12 +1,11 @@
1
  import os
2
  import tempfile
3
- import re
4
  import streamlit as st
5
  import docx
6
  import textract
7
- from sentence_transformers import SentenceTransformer, util
8
  from transformers import pipeline
9
  import threading
 
10
 
11
  #####################################
12
  # Load Models - Optimized with Threading
@@ -21,13 +20,15 @@ def load_models():
21
  def load_summarizer_thread():
22
  models['summarizer'] = pipeline("summarization", model="google/pegasus-xsum", device=0 if st.session_state.get('use_gpu', False) else -1)
23
 
24
- def load_sbert_thread():
25
- models['sbert'] = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2", device='cuda' if st.session_state.get('use_gpu', False) else 'cpu')
 
 
26
 
27
  # Start threads to load models in parallel
28
  threads = [
29
  threading.Thread(target=load_summarizer_thread),
30
- threading.Thread(target=load_sbert_thread)
31
  ]
32
 
33
  for thread in threads:
@@ -104,21 +105,23 @@ def summarize_resume_text(resume_text, models):
104
  return candidate_summary
105
 
106
  #####################################
107
- # Function: Compare Candidate Summary to Company Prompt - Optimized
108
  #####################################
109
  def compute_suitability(candidate_summary, company_prompt, models):
110
  """
111
- Compute the cosine similarity between candidate summary and company prompt embeddings.
112
  Returns a score in the range [0, 1].
113
  """
114
- sbert_model = models['sbert']
115
 
116
- # Encode texts in parallel (if supported by model)
117
- embeddings = sbert_model.encode([candidate_summary, company_prompt], convert_to_tensor=True)
118
- candidate_embed, company_embed = embeddings[0], embeddings[1]
 
 
119
 
120
- cosine_sim = util.cos_sim(candidate_embed, company_embed)
121
- score = float(cosine_sim.item())
122
  return score
123
 
124
  #####################################
 
1
  import os
2
  import tempfile
 
3
  import streamlit as st
4
  import docx
5
  import textract
 
6
  from transformers import pipeline
7
  import threading
8
+ import numpy as np
9
 
10
  #####################################
11
  # Load Models - Optimized with Threading
 
20
  def load_summarizer_thread():
21
  models['summarizer'] = pipeline("summarization", model="google/pegasus-xsum", device=0 if st.session_state.get('use_gpu', False) else -1)
22
 
23
+ def load_similarity_thread():
24
+ # Using sentence-similarity pipeline instead of SentenceTransformer
25
+ models['similarity'] = pipeline("sentence-similarity", model="sentence-transformers/all-MiniLM-L6-v2",
26
+ device=0 if st.session_state.get('use_gpu', False) else -1)
27
 
28
  # Start threads to load models in parallel
29
  threads = [
30
  threading.Thread(target=load_summarizer_thread),
31
+ threading.Thread(target=load_similarity_thread)
32
  ]
33
 
34
  for thread in threads:
 
105
  return candidate_summary
106
 
107
  #####################################
108
+ # Function: Compare Candidate Summary to Company Prompt - Using Pipeline
109
  #####################################
110
  def compute_suitability(candidate_summary, company_prompt, models):
111
  """
112
+ Compute the similarity between candidate summary and company prompt using the similarity pipeline.
113
  Returns a score in the range [0, 1].
114
  """
115
+ similarity_pipeline = models['similarity']
116
 
117
+ # The pipeline expects a document and a list of candidates to compare to
118
+ result = similarity_pipeline(
119
+ candidate_summary,
120
+ [company_prompt]
121
+ )
122
 
123
+ # Extract the similarity score from the result
124
+ score = result[0]['score']
125
  return score
126
 
127
  #####################################