Coco-18 commited on
Commit
a9978be
Β·
verified Β·
1 Parent(s): 95121a2

Update evaluate.py

Browse files
Files changed (1) hide show
  1. evaluate.py +11 -13
evaluate.py CHANGED
@@ -20,8 +20,8 @@ from translator import get_asr_model, get_asr_processor, LANGUAGE_CODES
20
  # Configure logging
21
  logger = logging.getLogger("speech_api")
22
 
23
- if not hasattr(handle_evaluation_request, 'cache'):
24
- handle_evaluation_request.cache = {}
25
 
26
  def calculate_similarity(text1, text2):
27
  """Calculate text similarity percentage."""
@@ -66,6 +66,7 @@ def setup_reference_patterns(reference_dir, sample_rate=16000):
66
  continue
67
 
68
  return created_dirs, 0 # Return 0 created files since we're not creating dummy files anymore
 
69
  def search_reference_directories():
70
  """Search for possible reference directories in various locations"""
71
  possible_locations = [
@@ -222,6 +223,7 @@ def init_reference_audio(reference_dir, output_dir):
222
  except:
223
  logger.critical("πŸ’₯ CRITICAL: Failed to create even a fallback directory")
224
  return reference_dir
 
225
  def handle_upload_reference(request, reference_dir, sample_rate):
226
  """Handle upload of reference audio files"""
227
  try:
@@ -349,10 +351,10 @@ def handle_evaluation_request(request, reference_dir, output_dir, sample_rate):
349
  audio_hash = hashlib.md5(audio_content).hexdigest()
350
  cache_key = f"{audio_hash}_{reference_locator}_{language}"
351
 
352
- # Check in-memory cache (define EVALUATION_CACHE at module level)
353
- if hasattr(handle_evaluation_request, 'cache') and cache_key in handle_evaluation_request.cache:
354
  logger.info(f"[{request_id}] βœ… Using cached evaluation result")
355
- return handle_evaluation_request.cache[cache_key]
356
 
357
  # Construct full reference directory path
358
  reference_dir_path = os.path.join(reference_dir, reference_locator)
@@ -571,16 +573,12 @@ def handle_evaluation_request(request, reference_dir, output_dir, sample_rate):
571
  "quick_evaluation": True
572
  })
573
 
574
- # OPTIMIZATION 6: Cache the result for future requests
575
- if not hasattr(handle_evaluation_request, 'cache'):
576
- handle_evaluation_request.cache = {}
577
-
578
- # Store in cache (limit cache size to avoid memory issues)
579
  MAX_CACHE_SIZE = 50
580
- handle_evaluation_request.cache[cache_key] = response
581
- if len(handle_evaluation_request.cache) > MAX_CACHE_SIZE:
582
  # Remove oldest entry (simplified approach)
583
- handle_evaluation_request.cache.pop(next(iter(handle_evaluation_request.cache)))
584
 
585
  return response
586
 
 
20
  # Configure logging
21
  logger = logging.getLogger("speech_api")
22
 
23
+ # Initialize cache at module level instead
24
+ EVALUATION_CACHE = {}
25
 
26
  def calculate_similarity(text1, text2):
27
  """Calculate text similarity percentage."""
 
66
  continue
67
 
68
  return created_dirs, 0 # Return 0 created files since we're not creating dummy files anymore
69
+
70
  def search_reference_directories():
71
  """Search for possible reference directories in various locations"""
72
  possible_locations = [
 
223
  except:
224
  logger.critical("πŸ’₯ CRITICAL: Failed to create even a fallback directory")
225
  return reference_dir
226
+
227
  def handle_upload_reference(request, reference_dir, sample_rate):
228
  """Handle upload of reference audio files"""
229
  try:
 
351
  audio_hash = hashlib.md5(audio_content).hexdigest()
352
  cache_key = f"{audio_hash}_{reference_locator}_{language}"
353
 
354
+ # Check in-memory cache using the module-level cache
355
+ if cache_key in EVALUATION_CACHE:
356
  logger.info(f"[{request_id}] βœ… Using cached evaluation result")
357
+ return EVALUATION_CACHE[cache_key]
358
 
359
  # Construct full reference directory path
360
  reference_dir_path = os.path.join(reference_dir, reference_locator)
 
573
  "quick_evaluation": True
574
  })
575
 
576
+ # OPTIMIZATION 6: Cache the result for future requests using module-level cache
 
 
 
 
577
  MAX_CACHE_SIZE = 50
578
+ EVALUATION_CACHE[cache_key] = response
579
+ if len(EVALUATION_CACHE) > MAX_CACHE_SIZE:
580
  # Remove oldest entry (simplified approach)
581
+ EVALUATION_CACHE.pop(next(iter(EVALUATION_CACHE)))
582
 
583
  return response
584