File size: 19,659 Bytes
655c38a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80084f9
dd632f4
 
b40aac8
 
dd632f4
80084f9
 
 
 
0c814bb
655c38a
 
 
 
 
 
 
 
 
0c814bb
 
80084f9
 
0c814bb
 
 
80084f9
0c814bb
 
 
 
 
655c38a
 
 
 
 
989b900
 
411ad61
 
 
 
 
 
 
 
67ea255
989b900
 
 
 
 
 
 
 
 
 
 
 
 
411ad61
 
 
 
989b900
 
411ad61
989b900
 
 
411ad61
67ea255
411ad61
655c38a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb0c48d
655c38a
eb0c48d
 
655c38a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a2672b
655c38a
 
8a2672b
655c38a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import requests
from sentence_transformers import SentenceTransformer
import numpy as np
import faiss
import json
import logging
import re
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
import os
# Import the scraper module
from scraper import scrape_job_description, is_url

# Set up cache directories in user's home directory
import os

# Define a container-writable cache directory in /tmp
cache_dir = os.path.join("/tmp", "shl_cache")
os.makedirs(cache_dir, exist_ok=True)

os.environ["TRANSFORMERS_CACHE"] = cache_dir
os.environ["HF_HOME"] = cache_dir
os.environ["SENTENCE_TRANSFORMERS_HOME"] = cache_dir

# Initialize models and caches as module-level singletons
_sentence_transformer = None
_llm = None
_llm_chain = None
_embedding_cache = {}

def get_sentence_transformer():
    global _sentence_transformer
    if _sentence_transformer is None:
        try:
            logging.info("Initializing SentenceTransformer model")
            model_name = 'sentence-transformers/all-MiniLM-L6-v2'
            _sentence_transformer = SentenceTransformer(model_name, cache_folder=os.environ["SENTENCE_TRANSFORMERS_HOME"])
            # Configure the model with required attributes
            if not hasattr(_sentence_transformer, 'config'):
                from transformers import AutoConfig
                config = AutoConfig.from_pretrained('bert-base-uncased', cache_dir=os.environ["TRANSFORMERS_CACHE"])
                config.model_type = 'bert'
                _sentence_transformer.config = config
        except Exception as e:
            logging.error(f"Error initializing SentenceTransformer: {str(e)}")
            raise
    return _sentence_transformer

def get_llm():
    global _llm, _llm_chain
    if _llm is None:
        try:
            logging.info("Initializing Gemma model")
            # Load API key from environment with explicit path to .env file
            from dotenv import load_dotenv
            load_dotenv(os.path.join(os.path.dirname(__file__), '.env'))
            
            api_key = os.getenv("GOOGLE_API_KEY")
            if not api_key:
                raise ValueError("GOOGLE_API_KEY not found in environment variables")
                
            _llm = ChatGoogleGenerativeAI(model="gemma-3-27b-it", google_api_key=api_key)
            prompt_template = ChatPromptTemplate.from_template(
                """
                You are a helpful assistant designed to analyze job descriptions and extract key information.
                Reply like you are the website and guiding users like a first person perspective.
                Based *only* on the following text content, please provide:
                1. A concise description of the particular assessment (2-4 sentences).
                2. Key features, benefits, or what it measures (up to 5 bullet points).
                Scraped Content:
                {context}
                Analysis:
                """
            )
            output_parser = StrOutputParser()
            _llm_chain = ({"context": RunnablePassthrough()} 
                         | prompt_template 
                         | _llm 
                         | output_parser)
        except Exception as e:
            logging.error(f"Error initializing Gemini API: {str(e)}")
            # More robust fallback that maintains chain structure
            def fallback_processor(text):
                return f"Analysis unavailable (API error). Original text: {text[:500]}"
            
            _llm_chain = ({"context": RunnablePassthrough()} 
                         | (lambda x: {"context": x["context"], "result": fallback_processor(x["context"])}) 
                         | (lambda x: x["result"]))
    return _llm_chain

def generate_embedding(text):
    # Use cache to avoid regenerating embeddings for identical text
    cache_key = hash(text)
    if cache_key in _embedding_cache:
        return _embedding_cache[cache_key]
    
    # Generate new embedding
    model = get_sentence_transformer()
    embedding = model.encode([text])
    
    # Cache the result
    _embedding_cache[cache_key] = embedding
    return embedding

# Function to process query and generate embedding
def process_query(input_data):
    try:
        # Check if input is a URL
        if is_url(input_data):
            # Scrape job description from URL
            text = scrape_job_description(input_data)
            
            # Check if scraping returned an error message
            if text.startswith("Unable to access") or text.startswith("No job description"):
                logging.warning(f"Scraping failed for URL: {input_data}")
                # Still try to process the error message to avoid breaking the flow
                processed_text = f"Query: {input_data}\n\nNote: {text}"
            else:
                try:
                    # Process the scraped content with Gemma to understand job requirements
                    llm_chain = get_llm()
                    job_analysis = llm_chain.invoke(text)
                    # Combine the original text with the analysis for better embedding
                    processed_text = f"Job Description: {text}\n\nAnalysis: {job_analysis}"
                except Exception as e:
                    logging.error(f"Error analyzing job description with LLM: {str(e)}")
                    # Fallback to just using the scraped text
                    processed_text = f"Job Description: {text}"
        else:
            # If not a URL, use the input text directly
            processed_text = input_data
            
        # Generate embedding from the processed text
        embedding = generate_embedding(processed_text)
        return embedding
    except Exception as e:
        logging.error(f"Error in process_query: {str(e)}")
        # Return a default embedding for the error message to avoid breaking the flow
        error_text = f"Error processing query: {str(e)}"
        return generate_embedding(error_text)

# Function to perform vector search
def vector_search(query_embedding):
    try:
        # Load the vector index
        index = faiss.read_index('shl_vector_index.idx')
        # Perform similarity search
        distances, indices = index.search(query_embedding, k=10)
        return distances, indices
    except Exception as e:
        logging.error(f"Error in vector search: {str(e)}")
        # Return empty results that won't break the flow
        # Create empty arrays with the right shape
        empty_indices = np.zeros((1, 10), dtype=np.int64)
        empty_distances = np.ones((1, 10), dtype=np.float32) * 999  # Large distance = low similarity
        return empty_distances, empty_indices

# Function to extract attributes from top results using Gemma
def extract_attributes(distances, indices):
    try:
        # Load and cache the processed data
        if not hasattr(extract_attributes, 'processed_data'):
            try:
                with open('shl_processed_analysis_specific.json', 'r', encoding='utf-8') as f:
                    extract_attributes.processed_data = json.load(f)
            except Exception as e:
                logging.error(f"Error loading processed data: {str(e)}")
                # Return empty results if data can't be loaded
                return [{
                    'Assessment Name': 'Error',
                    'URL': 'N/A',
                    'description': f"Error loading assessment data: {str(e)}",
                    'Key Features': [],
                    'Duration': '',
                    'Remote Testing': False,
                    'Raw Analysis': '',
                    'Similarity Score': 0
                }]
        processed_data = extract_attributes.processed_data
        results = []
        
        for i, idx in enumerate(indices[0]):
            try:
                # Handle index out of bounds
                if idx >= len(processed_data):
                    logging.warning(f"Index {idx} out of bounds for processed_data with length {len(processed_data)}")
                    continue
                    
                item = processed_data[idx]
                similarity_score = 1 / (0.5 + distances[0][i])  # Adjusted formula to boost similarity scores
                
                # Filter to only include assessment-specific URLs containing '/view/'
                if '/view/' not in item.get('url', ''):
                    continue
                extracted_text = item.get('extracted_text', '')
                
                if not extracted_text:
                    logging.warning(f"Empty extracted text for index {idx}")
                    continue
                
                try:
                    # Use Gemma to analyze the assessment details with a structured prompt
                    llm_chain = get_llm()
                    assessment_content = extracted_text.split('\n\n')[0] 
                    analysis = llm_chain.invoke(
                        f"""Assessment Data:
                        {assessment_content}
                        
                        Please analyze this specific assessment and provide a focused, assessment-specific output with these exact section headers. Avoid general company information.
                        
                        ## description:
                        [Provide a 1 sentence description that specifically describes what this assessment measures, its primary purpose, and its target audience. Focus only on this specific assessment's unique characteristics.]
                        
                        ## Key Features:
                        - [List 3-5 specific features or capabilities of this assessment]
                        - [Focus on what skills/abilities it measures]
                        - [Include technical aspects like adaptive testing if applicable]
                        
                        ## Duration:
                        [Specify exact duration in minutes if available, or provide estimated time range]
                        
                        ## Remote Testing:
                        [Yes/No - Include any specific remote proctoring details if available]
                        
                        ## Target Role/Level:
                        [Specify the job roles, levels, or industries this assessment is designed for]
                        """
                    )
                except Exception as e:
                    logging.error(f"Error analyzing assessment with LLM: {str(e)}")
                    # Use a placeholder analysis if LLM fails
                    analysis = f"Assessment information. Unable to analyze details: {str(e)}"
                
                # Process the structured analysis output
                analysis_lines = analysis.split('\n')
                description = ''
                features = []
                assessment_name = item.get('title', '') or 'SHL Assessment'
                duration = ''
                remote_testing = False
                
                # Parse the structured response sections
                current_section = ''
                for line in analysis_lines:
                    line = line.strip()
                    if line.startswith('##'):
                        current_section = line.replace('#', '').strip().lower()
                    elif line and current_section == 'description:':
                        description = line.strip('[]')
                    elif line.startswith('-') and current_section == 'key features:':
                        feature = line.strip('- []')
                        if feature:
                            features.append(feature)
                    elif current_section == 'duration:':
                        if line and not line.startswith('['):
                            duration = line.strip('[]')
                    elif current_section == 'remote testing:':
                        remote_testing = 'yes' in line.lower() or 'available' in line.lower() or 'supported' in line.lower()
                # Parse the structured response sections
                current_section = ''
                for line in analysis_lines:
                    line = line.strip()
                    if line.startswith('##'):
                        current_section = line.replace('#', '').strip().lower()
                    elif line and current_section == 'description:':
                        # Extract clean description without brackets
                        if '[' in line and ']' in line:
                            description = line[line.find('[')+1:line.find(']')]
                        else:
                            description = line
                    elif line.startswith('-') and current_section == 'key features:':
                        feature = line.strip('- []')
                        if feature:
                            features.append(feature)
                    elif current_section == 'duration:':
                        if line and not line.startswith('['):
                            duration = line.strip('[]')
                    elif current_section == 'remote testing:':
                        remote_testing = 'yes' in line.lower() or 'available' in line.lower() or 'supported' in line.lower()
                
                # Clean up and validate the description
                if not description or len(description.strip()) < 10:
                    # Fallback to a basic description if the LLM output is insufficient
                    description = f"Assessment measuring key competencies and skills for {assessment_name}."
                
                # Ensure features list is not empty
                if not features:
                    features = ["Measures relevant job competencies", "Provides standardized assessment"]
                
                # Clean up duration string
                if duration:
                    # Extract numbers from duration string
                    duration_numbers = re.findall(r'\d+', duration)
                    if duration_numbers:
                        duration = duration_numbers[0]  # Take the first number found
                        
                # Fallback duration extraction if not found in analysis
                if not duration and 'approximate completion time' in extracted_text.lower():
                    time_match = re.search(r'Approximate Completion Time in minutes = (\d+)', extracted_text, re.IGNORECASE)
                    if time_match:
                        duration = f"{time_match.group(1)} minutes"
                url=item.get('url', 'N/A')
                result = {
                    'Assessment_Name': assessment_name,
                    'URL': url,
                    'description': description,
                    'Key_Features': features,
                    'Duration': duration,
                    'Remote_Testing': remote_testing,
                    'Raw_Analysis': analysis,
                    'Similarity_Score': similarity_score
                }
                results.append(result)
            
            except Exception as e:
                logging.error(f"Error processing result at index {i}: {str(e)}")
                # Add an error result instead of failing completely
                results.append({
                    'Assessment_Name': 'Error',
                    'URL': 'N/A',
                    'description': f"Error processing assessment: {str(e)}",
                    'Key_Features': [],
                    'Duration': '',
                    'Remote_Testing': False,
                    'Raw_Analysis': '',
                    'Similarity_Score': 0
                })
        
        # If no results were found or all processing failed, return a helpful message
        if not results:
            results.append({
                'Assessment_Name': 'No Results',
                'URL': 'N/A',
                'description': "No matching assessments found for your query.",
                'Key_Features': ["Try a different search term", "Be more specific about the job role or skills"],
                'Duration': '',
                'Remote_Testing': False,
                'Raw_Analysis': '',
                'Similarity_Score': 0
            })
            
        return results
    except Exception as e:
        logging.error(f"Unexpected error in extract_attributes: {str(e)}")
        # Return a single error result
        return [{
            'Assessment Name': 'Error',
            'URL': 'N/A',
            'description': f"An unexpected error occurred: {str(e)}",
            'Key Features': ["Please try again later"],
            'Duration': '',
            'Remote Testing': False,
            'Raw Analysis': '',
            'Similarity Score': 0
        }]

# Example usage
def calculate_metrics(results, relevant_assessments, k=3):
    """Calculate Mean Recall@K and MAP@K metrics.
    
    Args:
        results: List of retrieved assessment results
        relevant_assessments: List of relevant assessment IDs/names
        k: Number of top results to consider (default: 3)
    
    Returns:
        tuple: (recall@k, map@k)
    """
    if not results or not relevant_assessments:
        return 0.0, 0.0
    
    # Get top K results
    top_k = results[:k]
    retrieved_assessments = [r['Assessment_Name'] for r in top_k]
    
    # Calculate Recall@K
    relevant_retrieved = sum(1 for r in retrieved_assessments if r in relevant_assessments)
    recall_k = relevant_retrieved / len(relevant_assessments) if relevant_assessments else 0.0
    
    # Calculate MAP@K
    precision_sum = 0.0
    relevant_count = 0
    
    for i, assessment in enumerate(retrieved_assessments, 1):
        if assessment in relevant_assessments:
            relevant_count += 1
            precision_at_i = relevant_count / i
            precision_sum += precision_at_i
    
    map_k = precision_sum / min(k, len(relevant_assessments)) if relevant_assessments else 0.0
    
    return recall_k, map_k

def main():
    try:
        input_query = "Your input query or URL here"
        query_embedding = process_query(input_query)
        distances, indices = vector_search(query_embedding)
        # Reshape indices and distances to match expected format
        if len(indices.shape) == 1:
            indices = indices.reshape(1, -1)
            distances = distances.reshape(1, -1)
        results = extract_attributes(distances=distances, indices=indices)
        
        # Example usage of metrics calculation
        # In a real scenario, relevant_assessments would come from ground truth data
        relevant_assessments = ["Example Assessment 1", "Example Assessment 2"]
        recall_k, map_k = calculate_metrics(results, relevant_assessments)
        logging.info(f"Mean Recall@3: {recall_k:.3f}")
        logging.info(f"MAP@3: {map_k:.3f}")
        
        return results
    except Exception as e:
        logging.error(f"Error in main function: {str(e)}")
        return [{
            'Assessment Name': 'Error',
            'URL': 'N/A',
            'description': f"An error occurred while processing your query: {str(e)}",
            'Key Features': ["Please try again later"],
            'Duration': '',
            'Remote Testing': False,
            'Raw Analysis': '',
            'Similarity Score': 0
        }]

if __name__ == "__main__":
    results = main()
    print(results)