srd4 commited on
Commit
cf9a3bd
·
verified ·
1 Parent(s): 12879c9

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +16 -21
handler.py CHANGED
@@ -3,31 +3,26 @@ from faster_whisper import WhisperModel
3
 
4
  class EndpointHandler:
5
  def __init__(self):
6
- # Initialize WhisperModel
7
- self.model = WhisperModel("large-v2")
8
 
9
  def __call__(self, data: Dict) -> Dict:
10
- # Get the audio file bytes from the request data
11
  audio_bytes = data["inputs"]
12
 
13
- # Perform transcription
14
- results = []
15
  segments, info = self.model.transcribe(audio_bytes)
16
- for segment in segments:
17
- result = {
18
- "start": segment.start,
19
- "end": segment.end,
20
- "text": segment.text
21
- }
22
- results.append(result)
23
 
24
- # Return the transcribed text along with language data
25
- language_code, language_prob = info.language, info.language_probability
26
- response = {
27
- "transcription": results,
28
- "language": {
29
- "code": language_code,
30
- "probability": language_prob
31
- }
 
 
32
  }
33
- return response
 
 
3
 
4
  class EndpointHandler:
5
  def __init__(self):
6
+ # Initialize WhisperModel; assume that "large-v2" model files are in /repository
7
+ self.model = WhisperModel("large-v2", device="cpu")
8
 
9
  def __call__(self, data: Dict) -> Dict:
10
+ # Process the input data expected to be in 'inputs' key containing audio file bytes
11
  audio_bytes = data["inputs"]
12
 
13
+ # Perform transcription using the model
 
14
  segments, info = self.model.transcribe(audio_bytes)
 
 
 
 
 
 
 
15
 
16
+ # Compile the results into a text string and extract language information
17
+ text = " ".join(segment.text for segment in segments)
18
+ language_code = info.language
19
+ language_prob = info.language_probability
20
+
21
+ # Compile the response dictionary
22
+ result = {
23
+ "text": text,
24
+ "language": language_code,
25
+ "language_probability": language_prob
26
  }
27
+
28
+ return result