poiqazwsx commited on
Commit
9ecee17
1 Parent(s): a599ac3

Update app.py

Browse files

Added labelled audio.txt that can be used on a daw like audacity
![image.png](https://cdn-uploads.huggingface.co/production/uploads/64dcc1db4e2c7863c308bd88/hbbYVo2pFiXibfVhtzYoO.png)

Files changed (1) hide show
  1. app.py +52 -9
app.py CHANGED
@@ -55,6 +55,49 @@ def diarize_audio(temp_file, num_speakers, min_speakers, max_speakers):
55
 
56
  # Return the diarization output
57
  return str(diarization)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  with gr.Blocks() as demo:
60
  gr.Markdown("""
@@ -69,18 +112,18 @@ with gr.Blocks() as demo:
69
  If you find this space helpful, please ❤ it.
70
 
71
  """)
72
- audio_input = gr.Audio(type="filepath", label="Upload Audio")
73
- num_speakers_input = gr.Number(label="Number of Speakers (The maximum number of speakers to detect)", value=0)
74
- min_speakers_input = gr.Number(label="Minimum Number of Speakers (The maximum number of speakers to detect)", value=0)
75
- max_speakers_input = gr.Number(label="Maximum Number of Speakers (The maximum number of speakers to detect)", value=0)
76
  process_button = gr.Button("Process")
77
  diarization_output = gr.Textbox(label="Diarization Output")
 
78
 
 
79
  process_button.click(
80
- fn=lambda audio, num_speakers, min_speakers, max_speakers:
81
- diarize_audio(save_audio(audio), num_speakers, min_speakers, max_speakers),
82
  inputs=[audio_input, num_speakers_input, min_speakers_input, max_speakers_input],
83
- outputs=diarization_output
84
- )
85
-
86
  demo.launch()
 
55
 
56
  # Return the diarization output
57
  return str(diarization)
58
+
59
+ def timestamp_to_seconds(timestamp):
60
+ try:
61
+ # Extracts hour, minute, and second from timestamp and converts to total seconds
62
+ h, m, s = map(float, timestamp.split(':'))
63
+ return 3600 * h + 60 * m + s
64
+ except ValueError as e:
65
+ print(f"Error converting timestamp to seconds: '{timestamp}'. Error: {e}")
66
+ return None
67
+
68
+ def generate_labels_from_diarization(diarization_output):
69
+ successful_lines = 0 # Counter for successfully processed lines
70
+ labels_path = 'labels.txt'
71
+ try:
72
+ with open(labels_path, 'w') as outfile:
73
+ lines = diarization_output.strip().split('\n')
74
+ for line in lines:
75
+ try:
76
+ parts = line.strip()[1:-1].split(' --> ')
77
+ start_time = parts[0].strip()
78
+ end_time = parts[1].split(']')[0].strip()
79
+ label = line.split()[-1].strip() # Extracting the last word as label
80
+ start_seconds = timestamp_to_seconds(start_time)
81
+ end_seconds = timestamp_to_seconds(end_time)
82
+ outfile.write(f"{start_seconds}\t{end_seconds}\t{label}\n")
83
+ successful_lines += 1
84
+ except Exception as e:
85
+ print(f"Error processing line: '{line.strip()}'. Error: {e}")
86
+ print(f"Processed {successful_lines} lines successfully.")
87
+ return labels_path if successful_lines > 0 else None
88
+ except Exception as e:
89
+ print(f"Cannot write to file '{labels_path}'. Error: {e}")
90
+ return None
91
+
92
+
93
+
94
+ def process_audio(audio, num_speakers, min_speakers, max_speakers):
95
+ diarization_result = diarize_audio(save_audio(audio), num_speakers, min_speakers, max_speakers)
96
+ if diarization_result.startswith("Error"):
97
+ return diarization_result, None # Return None for label file link if there's an error
98
+ else:
99
+ label_file = generate_labels_from_diarization(diarization_result)
100
+ return diarization_result, label_file
101
 
102
  with gr.Blocks() as demo:
103
  gr.Markdown("""
 
112
  If you find this space helpful, please ❤ it.
113
 
114
  """)
115
+ audio_input = gr.Audio(type="filepath", label="Upload Audio")
116
+ num_speakers_input = gr.Number(label="Number of Speakers", value=0)
117
+ min_speakers_input = gr.Number(label="Minimum Number of Speakers", value=0)
118
+ max_speakers_input = gr.Number(label="Maximum Number of Speakers", value=0)
119
  process_button = gr.Button("Process")
120
  diarization_output = gr.Textbox(label="Diarization Output")
121
+ label_file_link = gr.File(label="Download Audacity Labels")
122
 
123
+ # Use a lambda function to preprocess the inputs or to directly pass them
124
  process_button.click(
125
+ fn=process_audio,
 
126
  inputs=[audio_input, num_speakers_input, min_speakers_input, max_speakers_input],
127
+ outputs=[diarization_output, label_file_link]
128
+ )
 
129
  demo.launch()