Delik commited on
Commit
7180a69
1 Parent(s): ce1f6bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -11
app.py CHANGED
@@ -13,20 +13,21 @@ except Exception as e:
13
  print(f"Error initializing pipeline: {e}")
14
  pipeline = None
15
 
16
- @spaces.GPU
17
- def process_audio(audio, num_speakers, min_speakers, max_speakers):
18
  if pipeline is None:
19
  return "Error: Pipeline not initialized"
20
 
21
- # Read the uploaded audio file
22
- with open(audio, "rb") as f:
23
- audio_data = f.read()
24
-
25
  # Save the uploaded audio file to a temporary location
26
  with open("temp.wav", "wb") as f:
27
- f.write(audio_data)
 
 
 
 
 
 
 
28
 
29
- # Use the diarization pipeline to process the audio
30
  try:
31
  params = {}
32
  if num_speakers > 0:
@@ -36,12 +37,12 @@ def process_audio(audio, num_speakers, min_speakers, max_speakers):
36
  if max_speakers > 0:
37
  params["max_speakers"] = max_speakers
38
 
39
- diarization = pipeline("temp.wav", **params)
40
  except Exception as e:
41
  return f"Error processing audio: {e}"
42
 
43
  # Remove the temporary file
44
- os.remove("temp.wav")
45
 
46
  # Return the diarization output
47
  return str(diarization)
@@ -54,6 +55,11 @@ with gr.Blocks() as demo:
54
  process_button = gr.Button("Process")
55
  diarization_output = gr.Textbox(label="Diarization Output")
56
 
57
- process_button.click(fn=process_audio, inputs=[audio_input, num_speakers_input, min_speakers_input, max_speakers_input], outputs=diarization_output)
 
 
 
 
 
58
 
59
  demo.launch()
 
13
  print(f"Error initializing pipeline: {e}")
14
  pipeline = None
15
 
16
+ def save_audio(audio):
 
17
  if pipeline is None:
18
  return "Error: Pipeline not initialized"
19
 
 
 
 
 
20
  # Save the uploaded audio file to a temporary location
21
  with open("temp.wav", "wb") as f:
22
+ f.write(audio)
23
+
24
+ return "temp.wav"
25
+
26
+ @spaces.GPU
27
+ def diarize_audio(temp_file, num_speakers, min_speakers, max_speakers):
28
+ if pipeline is None:
29
+ return "Error: Pipeline not initialized"
30
 
 
31
  try:
32
  params = {}
33
  if num_speakers > 0:
 
37
  if max_speakers > 0:
38
  params["max_speakers"] = max_speakers
39
 
40
+ diarization = pipeline(temp_file, **params)
41
  except Exception as e:
42
  return f"Error processing audio: {e}"
43
 
44
  # Remove the temporary file
45
+ os.remove(temp_file)
46
 
47
  # Return the diarization output
48
  return str(diarization)
 
55
  process_button = gr.Button("Process")
56
  diarization_output = gr.Textbox(label="Diarization Output")
57
 
58
+ process_button.click(
59
+ fn=lambda audio, num_speakers, min_speakers, max_speakers:
60
+ diarize_audio(save_audio(audio), num_speakers, min_speakers, max_speakers),
61
+ inputs=[audio_input, num_speakers_input, min_speakers_input, max_speakers_input],
62
+ outputs=diarization_output
63
+ )
64
 
65
  demo.launch()