alessandro trinca tornidor commited on
Commit
5f896de
·
1 Parent(s): 06bd94e

feat: add request validations for lambdaGestSample and lambdaSpeechToScore backend modules

Browse files
aip_trainer/lambdas/lambdaGetSample.py CHANGED
@@ -7,6 +7,7 @@ import pandas as pd
7
 
8
  from aip_trainer import PROJECT_ROOT_FOLDER, app_logger
9
  from aip_trainer.models import RuleBasedModels
 
10
 
11
 
12
  class TextDataset:
@@ -53,19 +54,11 @@ lambda_ipa_converter['en'] = RuleBasedModels.EngPhonemConverter()
53
 
54
 
55
  def lambda_handler(event, context):
56
- body = json.loads(event['body'])
57
-
58
- try:
59
- category = int(body['category'])
60
- except KeyError:
61
- category = 0
62
- language = body['language']
63
- try:
64
- current_transcript = str(body["transcript"])
65
- except KeyError:
66
- current_transcript = get_random_selection(language, category, is_gradio_output=False)
67
- current_transcript = current_transcript if isinstance(current_transcript, str) else current_transcript[0]
68
- current_ipa = lambda_ipa_converter[language].convertToPhonem(current_transcript)
69
 
70
  app_logger.info(f"real_transcript='{current_transcript}', ipa_transcript='{current_ipa}'.")
71
  result = {
@@ -77,7 +70,9 @@ def lambda_handler(event, context):
77
  return json.dumps(result)
78
 
79
 
80
- def get_random_selection(language: str, category: int, is_gradio_output=True):
 
 
81
  lambda_df_lang = lambda_database[language]
82
  current_transcript = lambda_df_lang.get_random_sample_from_df(language, category)
83
  app_logger.info(f"category={category}, language={language}, current_transcript={current_transcript}.")
 
7
 
8
  from aip_trainer import PROJECT_ROOT_FOLDER, app_logger
9
  from aip_trainer.models import RuleBasedModels
10
+ from aip_trainer.utils.typing_hints import BodyGetSampleRequest
11
 
12
 
13
  class TextDataset:
 
54
 
55
 
56
  def lambda_handler(event, context):
57
+ event_body = event["body"]
58
+ body = BodyGetSampleRequest.model_validate_json(event_body)
59
+ current_transcript = get_random_selection(body.language, body.category, is_gradio_output=False, transcript=body.transcript)
60
+ current_transcript = current_transcript[0] if isinstance(current_transcript, list) else current_transcript
61
+ current_ipa = lambda_ipa_converter[body.language].convertToPhonem(current_transcript)
 
 
 
 
 
 
 
 
62
 
63
  app_logger.info(f"real_transcript='{current_transcript}', ipa_transcript='{current_ipa}'.")
64
  result = {
 
70
  return json.dumps(result)
71
 
72
 
73
+ def get_random_selection(language: str, category: int, is_gradio_output=True, transcript=None):
74
+ if transcript is not None and isinstance(transcript, str) and len(transcript) > 0:
75
+ return transcript
76
  lambda_df_lang = lambda_database[language]
77
  current_transcript = lambda_df_lang.get_random_sample_from_df(language, category)
78
  app_logger.info(f"category={category}, language={language}, current_transcript={current_transcript}.")
aip_trainer/lambdas/lambdaSpeechToScore.py CHANGED
@@ -12,6 +12,7 @@ from torchaudio.transforms import Resample
12
 
13
  from aip_trainer import WordMatching as wm, app_logger
14
  from aip_trainer import pronunciationTrainer, sample_rate_start
 
15
 
16
 
17
  trainer_SST_lambda = {
@@ -22,14 +23,14 @@ transform = Resample(orig_freq=sample_rate_start, new_freq=16000)
22
 
23
 
24
  def lambda_handler(event, context):
25
- body = event['body']
26
- data = json.loads(body)
27
 
28
- real_text = data['title']
29
- base64_audio = data["base64Audio"]
30
  app_logger.debug(f"base64Audio:{base64_audio} ...")
31
  file_bytes_or_audiotmpfile = base64.b64decode(base64_audio[22:].encode('utf-8'))
32
- language = data['language']
33
 
34
  if len(real_text) == 0:
35
  return {
 
12
 
13
  from aip_trainer import WordMatching as wm, app_logger
14
  from aip_trainer import pronunciationTrainer, sample_rate_start
15
+ from aip_trainer.utils.typing_hints import BodySpeechToScoreRequest
16
 
17
 
18
  trainer_SST_lambda = {
 
23
 
24
 
25
  def lambda_handler(event, context):
26
+ event_body = event['body']
27
+ data = BodySpeechToScoreRequest.model_validate_json(event_body)
28
 
29
+ real_text = data.title
30
+ base64_audio = data.base64Audio
31
  app_logger.debug(f"base64Audio:{base64_audio} ...")
32
  file_bytes_or_audiotmpfile = base64.b64decode(base64_audio[22:].encode('utf-8'))
33
+ language = data.language
34
 
35
  if len(real_text) == 0:
36
  return {
aip_trainer/utils/typing_hints.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Annotated, Optional
2
+ from pydantic import BaseModel
3
+
4
+ import annotated_types
5
+
6
+
7
+ type Category = Annotated[int, annotated_types.Ge(0), annotated_types.Le(4)]
8
+
9
+
10
+ class BodyGetSampleRequest(BaseModel):
11
+ category: Optional[Category] = 0
12
+ language: str
13
+ transcript: Optional[str] = ""
14
+
15
+
16
+ class BodySpeechToScoreRequest(BaseModel):
17
+ base64Audio: str
18
+ language: str
19
+ title: str