KIFF commited on
Commit
eea9e6d
·
verified ·
1 Parent(s): f7697ff

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +5 -23
handler.py CHANGED
@@ -10,7 +10,7 @@ SAMPLE_RATE = 16000
10
  class EndpointHandler():
11
  def __init__(self, path=""):
12
  self.pipeline = Pipeline.from_pretrained(
13
- "pyannote/speaker-diarization",
14
  use_auth_token=os.environ.get("HF_API_TOKEN")
15
  )
16
  self.pipeline.to(torch.device("cuda" if torch.cuda.is_available() else "cpu"))
@@ -20,12 +20,12 @@ class EndpointHandler():
20
  Args:
21
  data (Dict):
22
  'inputs': Base64-encoded audio bytes
23
- 'parameters': Additional diarization parameters, including 'num_speakers' (optional)
24
  Return:
25
  Dict: Speaker diarization results
26
  """
27
  inputs = data.get("inputs")
28
- parameters = data.get("parameters", {})
29
 
30
  # Decode the base64 audio data
31
  audio_data = base64.b64decode(inputs)
@@ -42,27 +42,9 @@ class EndpointHandler():
42
 
43
  pyannote_input = {"waveform": audio_tensor, "sample_rate": SAMPLE_RATE}
44
 
45
- # Extract num_speakers from parameters, if present
46
- num_speakers = parameters.pop("num_speakers", None)
47
-
48
- # Run diarization pipeline
49
  try:
50
- if num_speakers is not None:
51
- diarization = self.pipeline(pyannote_input, num_speakers=num_speakers) # Adjust parameters as needed for version 2.1.1
52
- else:
53
- diarization = self.pipeline(pyannote_input)
54
- except TypeError as e:
55
- print(f"Error: TypeError: {e}")
56
- if "num_speakers" in str(e):
57
- print("The 'num_speakers' parameter might not be supported by this version of the pipeline.")
58
- print("Trying without num_speakers...")
59
- try:
60
- diarization = self.pipeline(pyannote_input)
61
- except Exception as e:
62
- print(f"An error occurred even without 'num_speakers': {e}")
63
- return {"error": "Diarization failed"}
64
- else:
65
- return {"error": "Diarization failed with an unexpected TypeError. Check the server logs for details."}
66
  except Exception as e:
67
  print(f"An unexpected error occurred: {e}")
68
  return {"error": "Diarization failed unexpectedly"}
 
10
  class EndpointHandler():
11
  def __init__(self, path=""):
12
  self.pipeline = Pipeline.from_pretrained(
13
+ "pyannote/speaker-diarization@2.1", # 3.0 and later is nor supported as of yet in dec 2023
14
  use_auth_token=os.environ.get("HF_API_TOKEN")
15
  )
16
  self.pipeline.to(torch.device("cuda" if torch.cuda.is_available() else "cpu"))
 
20
  Args:
21
  data (Dict):
22
  'inputs': Base64-encoded audio bytes
23
+ 'parameters': Additional diarization parameters (currently unused)
24
  Return:
25
  Dict: Speaker diarization results
26
  """
27
  inputs = data.get("inputs")
28
+ parameters = data.get("parameters", {}) # We are not using them now, since model don't take speaker count anymore
29
 
30
  # Decode the base64 audio data
31
  audio_data = base64.b64decode(inputs)
 
42
 
43
  pyannote_input = {"waveform": audio_tensor, "sample_rate": SAMPLE_RATE}
44
 
45
+ # Run diarization pipeline (without num_speakers)
 
 
 
46
  try:
47
+ diarization = self.pipeline(pyannote_input)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  except Exception as e:
49
  print(f"An unexpected error occurred: {e}")
50
  return {"error": "Diarization failed unexpectedly"}