birdnet-analyzer 2.1.0__py3-none-any.whl → 2.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. birdnet_analyzer/__init__.py +9 -9
  2. birdnet_analyzer/analyze/__init__.py +19 -19
  3. birdnet_analyzer/analyze/__main__.py +3 -3
  4. birdnet_analyzer/analyze/cli.py +30 -30
  5. birdnet_analyzer/analyze/core.py +268 -246
  6. birdnet_analyzer/analyze/utils.py +700 -694
  7. birdnet_analyzer/audio.py +368 -368
  8. birdnet_analyzer/cli.py +732 -732
  9. birdnet_analyzer/config.py +243 -243
  10. birdnet_analyzer/eBird_taxonomy_codes_2024E.json +13045 -13045
  11. birdnet_analyzer/embeddings/__init__.py +3 -3
  12. birdnet_analyzer/embeddings/__main__.py +3 -3
  13. birdnet_analyzer/embeddings/cli.py +12 -12
  14. birdnet_analyzer/embeddings/core.py +70 -70
  15. birdnet_analyzer/embeddings/utils.py +173 -220
  16. birdnet_analyzer/evaluation/__init__.py +189 -189
  17. birdnet_analyzer/evaluation/__main__.py +3 -3
  18. birdnet_analyzer/evaluation/assessment/metrics.py +388 -388
  19. birdnet_analyzer/evaluation/assessment/performance_assessor.py +364 -364
  20. birdnet_analyzer/evaluation/assessment/plotting.py +378 -378
  21. birdnet_analyzer/evaluation/preprocessing/data_processor.py +631 -631
  22. birdnet_analyzer/evaluation/preprocessing/utils.py +98 -98
  23. birdnet_analyzer/gui/__init__.py +19 -19
  24. birdnet_analyzer/gui/__main__.py +3 -3
  25. birdnet_analyzer/gui/analysis.py +179 -179
  26. birdnet_analyzer/gui/assets/arrow_down.svg +4 -4
  27. birdnet_analyzer/gui/assets/arrow_left.svg +4 -4
  28. birdnet_analyzer/gui/assets/arrow_right.svg +4 -4
  29. birdnet_analyzer/gui/assets/arrow_up.svg +4 -4
  30. birdnet_analyzer/gui/assets/gui.css +36 -36
  31. birdnet_analyzer/gui/assets/gui.js +89 -93
  32. birdnet_analyzer/gui/embeddings.py +638 -638
  33. birdnet_analyzer/gui/evaluation.py +801 -801
  34. birdnet_analyzer/gui/localization.py +75 -75
  35. birdnet_analyzer/gui/multi_file.py +265 -265
  36. birdnet_analyzer/gui/review.py +472 -472
  37. birdnet_analyzer/gui/segments.py +191 -191
  38. birdnet_analyzer/gui/settings.py +149 -149
  39. birdnet_analyzer/gui/single_file.py +264 -264
  40. birdnet_analyzer/gui/species.py +95 -95
  41. birdnet_analyzer/gui/train.py +687 -687
  42. birdnet_analyzer/gui/utils.py +803 -797
  43. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_af.txt +6522 -6522
  44. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_ar.txt +6522 -6522
  45. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_bg.txt +6522 -6522
  46. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_ca.txt +6522 -6522
  47. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_cs.txt +6522 -6522
  48. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_da.txt +6522 -6522
  49. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_de.txt +6522 -6522
  50. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_el.txt +6522 -6522
  51. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_en_uk.txt +6522 -6522
  52. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_es.txt +6522 -6522
  53. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_fi.txt +6522 -6522
  54. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_fr.txt +6522 -6522
  55. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_he.txt +6522 -6522
  56. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_hr.txt +6522 -6522
  57. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_hu.txt +6522 -6522
  58. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_in.txt +6522 -6522
  59. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_is.txt +6522 -6522
  60. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_it.txt +6522 -6522
  61. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_ja.txt +6522 -6522
  62. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_ko.txt +6522 -6522
  63. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_lt.txt +6522 -6522
  64. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_ml.txt +6522 -6522
  65. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_nl.txt +6522 -6522
  66. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_no.txt +6522 -6522
  67. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_pl.txt +6522 -6522
  68. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_pt_BR.txt +6522 -6522
  69. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_pt_PT.txt +6522 -6522
  70. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_ro.txt +6522 -6522
  71. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_ru.txt +6522 -6522
  72. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_sk.txt +6522 -6522
  73. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_sl.txt +6522 -6522
  74. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_sr.txt +6522 -6522
  75. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_sv.txt +6522 -6522
  76. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_th.txt +6522 -6522
  77. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_tr.txt +6522 -6522
  78. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_uk.txt +6522 -6522
  79. birdnet_analyzer/labels/V2.4/BirdNET_GLOBAL_6K_V2.4_Labels_zh.txt +6522 -6522
  80. birdnet_analyzer/lang/de.json +342 -341
  81. birdnet_analyzer/lang/en.json +342 -341
  82. birdnet_analyzer/lang/fi.json +342 -341
  83. birdnet_analyzer/lang/fr.json +342 -341
  84. birdnet_analyzer/lang/id.json +342 -341
  85. birdnet_analyzer/lang/pt-br.json +342 -341
  86. birdnet_analyzer/lang/ru.json +342 -341
  87. birdnet_analyzer/lang/se.json +342 -341
  88. birdnet_analyzer/lang/tlh.json +342 -341
  89. birdnet_analyzer/lang/zh_TW.json +342 -341
  90. birdnet_analyzer/model.py +1213 -1212
  91. birdnet_analyzer/search/__init__.py +3 -3
  92. birdnet_analyzer/search/__main__.py +3 -3
  93. birdnet_analyzer/search/cli.py +11 -11
  94. birdnet_analyzer/search/core.py +78 -78
  95. birdnet_analyzer/search/utils.py +104 -107
  96. birdnet_analyzer/segments/__init__.py +3 -3
  97. birdnet_analyzer/segments/__main__.py +3 -3
  98. birdnet_analyzer/segments/cli.py +13 -13
  99. birdnet_analyzer/segments/core.py +81 -81
  100. birdnet_analyzer/segments/utils.py +383 -383
  101. birdnet_analyzer/species/__init__.py +3 -3
  102. birdnet_analyzer/species/__main__.py +3 -3
  103. birdnet_analyzer/species/cli.py +13 -13
  104. birdnet_analyzer/species/core.py +35 -35
  105. birdnet_analyzer/species/utils.py +73 -74
  106. birdnet_analyzer/train/__init__.py +3 -3
  107. birdnet_analyzer/train/__main__.py +3 -3
  108. birdnet_analyzer/train/cli.py +13 -13
  109. birdnet_analyzer/train/core.py +113 -113
  110. birdnet_analyzer/train/utils.py +878 -877
  111. birdnet_analyzer/translate.py +132 -133
  112. birdnet_analyzer/utils.py +425 -425
  113. {birdnet_analyzer-2.1.0.dist-info → birdnet_analyzer-2.1.1.dist-info}/METADATA +147 -146
  114. birdnet_analyzer-2.1.1.dist-info/RECORD +124 -0
  115. {birdnet_analyzer-2.1.0.dist-info → birdnet_analyzer-2.1.1.dist-info}/licenses/LICENSE +18 -18
  116. birdnet_analyzer/playground.py +0 -5
  117. birdnet_analyzer-2.1.0.dist-info/RECORD +0 -125
  118. {birdnet_analyzer-2.1.0.dist-info → birdnet_analyzer-2.1.1.dist-info}/WHEEL +0 -0
  119. {birdnet_analyzer-2.1.0.dist-info → birdnet_analyzer-2.1.1.dist-info}/entry_points.txt +0 -0
  120. {birdnet_analyzer-2.1.0.dist-info → birdnet_analyzer-2.1.1.dist-info}/top_level.txt +0 -0
@@ -1,264 +1,264 @@
1
- import os
2
-
3
- import gradio as gr
4
-
5
- import birdnet_analyzer.config as cfg
6
- import birdnet_analyzer.gui.localization as loc
7
- import birdnet_analyzer.gui.utils as gu
8
- from birdnet_analyzer import audio, utils
9
-
10
- MATPLOTLIB_FIGURE_NUM = "single-file-tab-spectrogram-plot"
11
-
12
-
13
- @gu.gui_runtime_error_handler
14
- def run_single_file_analysis(
15
- input_path,
16
- use_top_n,
17
- top_n,
18
- confidence,
19
- sensitivity,
20
- overlap,
21
- merge_consecutive,
22
- audio_speed,
23
- fmin,
24
- fmax,
25
- species_list_choice,
26
- species_list_file,
27
- lat,
28
- lon,
29
- week,
30
- use_yearlong,
31
- sf_thresh,
32
- custom_classifier_file,
33
- locale,
34
- ):
35
- import csv
36
- from datetime import timedelta
37
-
38
- from birdnet_analyzer.gui.analysis import run_analysis
39
-
40
- if species_list_choice == gu._CUSTOM_SPECIES:
41
- gu.validate(species_list_file, loc.localize("validation-no-species-list-selected"))
42
-
43
- gu.validate(input_path, loc.localize("validation-no-file-selected"))
44
-
45
- if fmin is None or fmax is None or fmin < cfg.SIG_FMIN or fmax > cfg.SIG_FMAX or fmin > fmax:
46
- raise gr.Error(f"{loc.localize('validation-no-valid-frequency')} [{cfg.SIG_FMIN}, {cfg.SIG_FMAX}]")
47
-
48
- result_filepath = run_analysis(
49
- input_path,
50
- None,
51
- use_top_n,
52
- top_n,
53
- confidence,
54
- sensitivity,
55
- overlap,
56
- merge_consecutive,
57
- audio_speed,
58
- fmin,
59
- fmax,
60
- species_list_choice,
61
- species_list_file,
62
- lat,
63
- lon,
64
- week,
65
- use_yearlong,
66
- sf_thresh,
67
- custom_classifier_file,
68
- "csv",
69
- None,
70
- False,
71
- locale if locale else "en",
72
- 1,
73
- 4,
74
- None,
75
- skip_existing=False,
76
- save_params=False,
77
- progress=None,
78
- )
79
-
80
- if not result_filepath:
81
- raise gr.Error(loc.localize("single-tab-analyze-file-error"))
82
-
83
- # read the result file to return the data to be displayed.
84
- with open(result_filepath, encoding="utf-8") as f:
85
- reader = csv.reader(f)
86
- data = list(reader)
87
- data = [lc[0:-1] for lc in data[1:]] # remove last column (file path) and first row (header)
88
-
89
- for row in data:
90
- for col_idx in range(2):
91
- seconds = float(row[col_idx])
92
- time_str = str(timedelta(seconds=seconds))
93
-
94
- if "." in time_str:
95
- time_str = time_str[: time_str.index(".") + 2]
96
-
97
- row[col_idx] = time_str
98
- row.insert(0, "▶")
99
-
100
- return data, gr.update(visible=True), result_filepath
101
-
102
-
103
- def build_single_analysis_tab():
104
- with gr.Tab(loc.localize("single-tab-title")):
105
- audio_input = gr.Audio(type="filepath", label=loc.localize("single-audio-label"), sources=["upload"])
106
-
107
- with gr.Group():
108
- spectogram_output = gr.Plot(label=loc.localize("review-tab-spectrogram-plot-label"), visible=False, show_label=False)
109
- generate_spectrogram_cb = gr.Checkbox(
110
- value=True,
111
- label=loc.localize("single-tab-spectrogram-checkbox-label"),
112
- info=loc.localize("single-tab-spectrogram-checkbox-info"),
113
- )
114
- audio_path_state = gr.State()
115
- table_path_state = gr.State()
116
-
117
- (
118
- use_top_n,
119
- top_n_input,
120
- confidence_slider,
121
- sensitivity_slider,
122
- overlap_slider,
123
- merge_consecutive_slider,
124
- audio_speed_slider,
125
- fmin_number,
126
- fmax_number,
127
- ) = gu.sample_sliders(False)
128
-
129
- (
130
- species_list_radio,
131
- species_file_input,
132
- lat_number,
133
- lon_number,
134
- week_number,
135
- sf_thresh_number,
136
- yearlong_checkbox,
137
- selected_classifier_state,
138
- map_plot,
139
- ) = gu.species_lists(False)
140
- locale_radio = gu.locale()
141
-
142
- single_file_analyze = gr.Button(loc.localize("analyze-start-button-label"), variant="huggingface", interactive=False)
143
-
144
- with gr.Row(visible=False) as action_row:
145
- table_download_button = gr.Button(
146
- loc.localize("single-tab-download-button-label"),
147
- )
148
- segment_audio = gr.Audio(autoplay=True, type="numpy", show_download_button=True, show_label=False, editable=False, visible=False)
149
-
150
- output_dataframe = gr.Dataframe(
151
- type="pandas",
152
- headers=[
153
- "",
154
- loc.localize("single-tab-output-header-start"),
155
- loc.localize("single-tab-output-header-end"),
156
- loc.localize("single-tab-output-header-sci-name"),
157
- loc.localize("single-tab-output-header-common-name"),
158
- loc.localize("single-tab-output-header-confidence"),
159
- ],
160
- elem_id="single-file-output",
161
- interactive=False,
162
- )
163
-
164
- def get_audio_path(i, generate_spectrogram):
165
- if i:
166
- try:
167
- return (
168
- i["path"],
169
- gr.Audio(label=os.path.basename(i["path"])),
170
- gr.Plot(visible=True, value=utils.spectrogram_from_file(i["path"], fig_size=(20, 4), fig_num=MATPLOTLIB_FIGURE_NUM))
171
- if generate_spectrogram
172
- else gr.Plot(visible=False),
173
- gr.Button(interactive=True),
174
- )
175
- except Exception as e:
176
- raise gr.Error(loc.localize("single-tab-generate-spectrogram-error")) from e
177
- else:
178
- return None, None, gr.Plot(visible=False), gr.update(interactive=False)
179
-
180
- def try_generate_spectrogram(audio_path, generate_spectrogram):
181
- if audio_path and generate_spectrogram:
182
- try:
183
- return gr.Plot(visible=True, value=utils.spectrogram_from_file(audio_path["path"], fig_size=(20, 4), fig_num=MATPLOTLIB_FIGURE_NUM))
184
- except Exception as e:
185
- raise gr.Error(loc.localize("single-tab-generate-spectrogram-error")) from e
186
- else:
187
- return gr.Plot()
188
-
189
- generate_spectrogram_cb.change(
190
- try_generate_spectrogram,
191
- inputs=[audio_input, generate_spectrogram_cb],
192
- outputs=spectogram_output,
193
- preprocess=False,
194
- )
195
-
196
- audio_input.change(
197
- get_audio_path,
198
- inputs=[audio_input, generate_spectrogram_cb],
199
- outputs=[audio_path_state, audio_input, spectogram_output, single_file_analyze],
200
- preprocess=False,
201
- )
202
-
203
- inputs = [
204
- audio_path_state,
205
- use_top_n,
206
- top_n_input,
207
- confidence_slider,
208
- sensitivity_slider,
209
- overlap_slider,
210
- merge_consecutive_slider,
211
- audio_speed_slider,
212
- fmin_number,
213
- fmax_number,
214
- species_list_radio,
215
- species_file_input,
216
- lat_number,
217
- lon_number,
218
- week_number,
219
- yearlong_checkbox,
220
- sf_thresh_number,
221
- selected_classifier_state,
222
- locale_radio,
223
- ]
224
-
225
- def time_to_seconds(time_str):
226
- try:
227
- hours, minutes, seconds = time_str.split(":")
228
- return int(hours) * 3600 + int(minutes) * 60 + float(seconds)
229
-
230
- except ValueError as e:
231
- raise ValueError("Input must be in the format hh:mm:ss or hh:mm:ss.ssssss with numeric values.") from e
232
-
233
- def get_selected_audio(evt: gr.SelectData, audio_path):
234
- if evt.index[1] == 0 and evt.row_value[1] and evt.row_value[2]:
235
- start = time_to_seconds(evt.row_value[1])
236
- end = time_to_seconds(evt.row_value[2])
237
-
238
- data, sr = audio.open_audio_file(audio_path, offset=start, duration=end - start)
239
- return gr.update(visible=True, value=(sr, data))
240
-
241
- return gr.update()
242
-
243
- def download_table(filepath):
244
- if filepath:
245
- ext = os.path.splitext(filepath)[1]
246
- file_location = gu.save_file_dialog(
247
- state_key="single-file-table",
248
- default_filename=os.path.basename(filepath),
249
- filetypes=(f"{ext[1:]} (*{ext})",),
250
- )
251
-
252
- if file_location:
253
- with open(filepath, "rb") as src, open(file_location, "wb") as dst:
254
- dst.write(src.read())
255
-
256
- output_dataframe.select(get_selected_audio, inputs=audio_path_state, outputs=segment_audio)
257
- single_file_analyze.click(run_single_file_analysis, inputs=inputs, outputs=[output_dataframe, action_row, table_path_state])
258
- table_download_button.click(download_table, inputs=table_path_state)
259
-
260
- return lat_number, lon_number, map_plot
261
-
262
-
263
- if __name__ == "__main__":
264
- gu.open_window(build_single_analysis_tab)
1
+ import os
2
+
3
+ import gradio as gr
4
+
5
+ import birdnet_analyzer.config as cfg
6
+ import birdnet_analyzer.gui.localization as loc
7
+ import birdnet_analyzer.gui.utils as gu
8
+ from birdnet_analyzer import audio, utils
9
+
10
+ MATPLOTLIB_FIGURE_NUM = "single-file-tab-spectrogram-plot"
11
+
12
+
13
+ @gu.gui_runtime_error_handler
14
+ def run_single_file_analysis(
15
+ input_path,
16
+ use_top_n,
17
+ top_n,
18
+ confidence,
19
+ sensitivity,
20
+ overlap,
21
+ merge_consecutive,
22
+ audio_speed,
23
+ fmin,
24
+ fmax,
25
+ species_list_choice,
26
+ species_list_file,
27
+ lat,
28
+ lon,
29
+ week,
30
+ use_yearlong,
31
+ sf_thresh,
32
+ custom_classifier_file,
33
+ locale,
34
+ ):
35
+ import csv
36
+ from datetime import timedelta
37
+
38
+ from birdnet_analyzer.gui.analysis import run_analysis
39
+
40
+ if species_list_choice == gu._CUSTOM_SPECIES:
41
+ gu.validate(species_list_file, loc.localize("validation-no-species-list-selected"))
42
+
43
+ gu.validate(input_path, loc.localize("validation-no-file-selected"))
44
+
45
+ if fmin is None or fmax is None or fmin < cfg.SIG_FMIN or fmax > cfg.SIG_FMAX or fmin > fmax:
46
+ raise gr.Error(f"{loc.localize('validation-no-valid-frequency')} [{cfg.SIG_FMIN}, {cfg.SIG_FMAX}]")
47
+
48
+ result_filepath = run_analysis(
49
+ input_path,
50
+ None,
51
+ use_top_n,
52
+ top_n,
53
+ confidence,
54
+ sensitivity,
55
+ overlap,
56
+ merge_consecutive,
57
+ audio_speed,
58
+ fmin,
59
+ fmax,
60
+ species_list_choice,
61
+ species_list_file,
62
+ lat,
63
+ lon,
64
+ week,
65
+ use_yearlong,
66
+ sf_thresh,
67
+ custom_classifier_file,
68
+ "csv",
69
+ None,
70
+ False,
71
+ locale if locale else "en",
72
+ 1,
73
+ 4,
74
+ None,
75
+ skip_existing=False,
76
+ save_params=False,
77
+ progress=None,
78
+ )
79
+
80
+ if not result_filepath:
81
+ raise gr.Error(loc.localize("single-tab-analyze-file-error"))
82
+
83
+ # read the result file to return the data to be displayed.
84
+ with open(result_filepath, encoding="utf-8") as f:
85
+ reader = csv.reader(f)
86
+ data = list(reader)
87
+ data = [lc[0:-1] for lc in data[1:]] # remove last column (file path) and first row (header)
88
+
89
+ for row in data:
90
+ for col_idx in range(2):
91
+ seconds = float(row[col_idx])
92
+ time_str = str(timedelta(seconds=seconds))
93
+
94
+ if "." in time_str:
95
+ time_str = time_str[: time_str.index(".") + 2]
96
+
97
+ row[col_idx] = time_str
98
+ row.insert(0, "▶")
99
+
100
+ return data, gr.update(visible=True), result_filepath
101
+
102
+
103
+ def build_single_analysis_tab():
104
+ with gr.Tab(loc.localize("single-tab-title")):
105
+ audio_input = gr.Audio(type="filepath", label=loc.localize("single-audio-label"), sources=["upload"])
106
+
107
+ with gr.Group():
108
+ spectogram_output = gr.Plot(label=loc.localize("review-tab-spectrogram-plot-label"), visible=False, show_label=False)
109
+ generate_spectrogram_cb = gr.Checkbox(
110
+ value=True,
111
+ label=loc.localize("single-tab-spectrogram-checkbox-label"),
112
+ info=loc.localize("single-tab-spectrogram-checkbox-info"),
113
+ )
114
+ audio_path_state = gr.State()
115
+ table_path_state = gr.State()
116
+
117
+ (
118
+ use_top_n,
119
+ top_n_input,
120
+ confidence_slider,
121
+ sensitivity_slider,
122
+ overlap_slider,
123
+ merge_consecutive_slider,
124
+ audio_speed_slider,
125
+ fmin_number,
126
+ fmax_number,
127
+ ) = gu.sample_sliders(False)
128
+
129
+ (
130
+ species_list_radio,
131
+ species_file_input,
132
+ lat_number,
133
+ lon_number,
134
+ week_number,
135
+ sf_thresh_number,
136
+ yearlong_checkbox,
137
+ selected_classifier_state,
138
+ map_plot,
139
+ ) = gu.species_lists(False)
140
+ locale_radio = gu.locale()
141
+
142
+ single_file_analyze = gr.Button(loc.localize("analyze-start-button-label"), variant="huggingface", interactive=False)
143
+
144
+ with gr.Row(visible=False) as action_row:
145
+ table_download_button = gr.Button(
146
+ loc.localize("single-tab-download-button-label"),
147
+ )
148
+ segment_audio = gr.Audio(autoplay=True, type="numpy", show_download_button=True, show_label=False, editable=False, visible=False)
149
+
150
+ output_dataframe = gr.Dataframe(
151
+ type="pandas",
152
+ headers=[
153
+ "",
154
+ loc.localize("single-tab-output-header-start"),
155
+ loc.localize("single-tab-output-header-end"),
156
+ loc.localize("single-tab-output-header-sci-name"),
157
+ loc.localize("single-tab-output-header-common-name"),
158
+ loc.localize("single-tab-output-header-confidence"),
159
+ ],
160
+ elem_id="single-file-output",
161
+ interactive=False,
162
+ )
163
+
164
+ def get_audio_path(i, generate_spectrogram):
165
+ if i:
166
+ try:
167
+ return (
168
+ i["path"],
169
+ gr.Audio(label=os.path.basename(i["path"])),
170
+ gr.Plot(visible=True, value=utils.spectrogram_from_file(i["path"], fig_size=(20, 4), fig_num=MATPLOTLIB_FIGURE_NUM))
171
+ if generate_spectrogram
172
+ else gr.Plot(visible=False),
173
+ gr.Button(interactive=True),
174
+ )
175
+ except Exception as e:
176
+ raise gr.Error(loc.localize("single-tab-generate-spectrogram-error")) from e
177
+ else:
178
+ return None, None, gr.Plot(visible=False), gr.update(interactive=False)
179
+
180
+ def try_generate_spectrogram(audio_path, generate_spectrogram):
181
+ if audio_path and generate_spectrogram:
182
+ try:
183
+ return gr.Plot(visible=True, value=utils.spectrogram_from_file(audio_path["path"], fig_size=(20, 4), fig_num=MATPLOTLIB_FIGURE_NUM))
184
+ except Exception as e:
185
+ raise gr.Error(loc.localize("single-tab-generate-spectrogram-error")) from e
186
+ else:
187
+ return gr.Plot()
188
+
189
+ generate_spectrogram_cb.change(
190
+ try_generate_spectrogram,
191
+ inputs=[audio_input, generate_spectrogram_cb],
192
+ outputs=spectogram_output,
193
+ preprocess=False,
194
+ )
195
+
196
+ audio_input.change(
197
+ get_audio_path,
198
+ inputs=[audio_input, generate_spectrogram_cb],
199
+ outputs=[audio_path_state, audio_input, spectogram_output, single_file_analyze],
200
+ preprocess=False,
201
+ )
202
+
203
+ inputs = [
204
+ audio_path_state,
205
+ use_top_n,
206
+ top_n_input,
207
+ confidence_slider,
208
+ sensitivity_slider,
209
+ overlap_slider,
210
+ merge_consecutive_slider,
211
+ audio_speed_slider,
212
+ fmin_number,
213
+ fmax_number,
214
+ species_list_radio,
215
+ species_file_input,
216
+ lat_number,
217
+ lon_number,
218
+ week_number,
219
+ yearlong_checkbox,
220
+ sf_thresh_number,
221
+ selected_classifier_state,
222
+ locale_radio,
223
+ ]
224
+
225
+ def time_to_seconds(time_str):
226
+ try:
227
+ hours, minutes, seconds = time_str.split(":")
228
+ return int(hours) * 3600 + int(minutes) * 60 + float(seconds)
229
+
230
+ except ValueError as e:
231
+ raise ValueError("Input must be in the format hh:mm:ss or hh:mm:ss.ssssss with numeric values.") from e
232
+
233
+ def get_selected_audio(evt: gr.SelectData, audio_path):
234
+ if evt.index[1] == 0 and evt.row_value[1] and evt.row_value[2]:
235
+ start = time_to_seconds(evt.row_value[1])
236
+ end = time_to_seconds(evt.row_value[2])
237
+
238
+ data, sr = audio.open_audio_file(audio_path, offset=start, duration=end - start)
239
+ return gr.update(visible=True, value=(sr, data))
240
+
241
+ return gr.update()
242
+
243
+ def download_table(filepath):
244
+ if filepath:
245
+ ext = os.path.splitext(filepath)[1]
246
+ file_location = gu.save_file_dialog(
247
+ state_key="single-file-table",
248
+ default_filename=os.path.basename(filepath),
249
+ filetypes=(f"{ext[1:]} (*{ext})",),
250
+ )
251
+
252
+ if file_location:
253
+ with open(filepath, "rb") as src, open(file_location, "wb") as dst:
254
+ dst.write(src.read())
255
+
256
+ output_dataframe.select(get_selected_audio, inputs=audio_path_state, outputs=segment_audio)
257
+ single_file_analyze.click(run_single_file_analysis, inputs=inputs, outputs=[output_dataframe, action_row, table_path_state])
258
+ table_download_button.click(download_table, inputs=table_path_state)
259
+
260
+ return lat_number, lon_number, map_plot
261
+
262
+
263
+ if __name__ == "__main__":
264
+ gu.open_window(build_single_analysis_tab)