achouffe commited on
Commit
f31bb0b
·
verified ·
1 Parent(s): 6d518d4

feat: add download CSV button

Browse files
Files changed (2) hide show
  1. .gitignore +1 -0
  2. app.py +14 -3
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ data/predictions/
app.py CHANGED
@@ -34,7 +34,7 @@ def interface_fn(
34
  model: YOLO,
35
  audio_filepath: str,
36
  config_model: dict[str, float | int],
37
- ) -> Tuple[list[Image.Image], pd.DataFrame, str]:
38
  """
39
  Main interface function that runs the model on the provided audio_filepath and
40
  returns the exepected tuple to populate the gradio interface.
@@ -99,6 +99,11 @@ def interface_fn(
99
  for waveform in waveforms
100
  ]
101
 
 
 
 
 
 
102
  spectrograms_pil_images = [Image.fromarray(a) for a in spectrograms_array_images]
103
 
104
  predictions = model.predict(spectrograms_pil_images)
@@ -106,7 +111,12 @@ def interface_fn(
106
  Image.fromarray(bgr_to_rgb(p.plot())) for p in predictions
107
  ]
108
 
109
- return (pil_image_spectrogram_with_predictions, df[CSV_COLUMNS], prediction_to_str(df=df))
 
 
 
 
 
110
 
111
 
112
  def examples(dir_examples: Path) -> list[Path]:
@@ -150,6 +160,7 @@ with gr.Blocks() as demo:
150
  headers=CSV_COLUMNS,
151
  label="prediction as CSV",
152
  )
 
153
 
154
  fn = lambda audio_filepath: interface_fn(
155
  model=model,
@@ -160,7 +171,7 @@ with gr.Blocks() as demo:
160
  title="ML model for forest elephant rumble detection 🐘",
161
  fn=fn,
162
  inputs=input,
163
- outputs=[output_gallery, output_dataframe, output_raw],
164
  examples=sound_filepaths,
165
  flagging_mode="never",
166
  )
 
34
  model: YOLO,
35
  audio_filepath: str,
36
  config_model: dict[str, float | int],
37
+ ) -> Tuple[list[Image.Image], pd.DataFrame, str, Path]:
38
  """
39
  Main interface function that runs the model on the provided audio_filepath and
40
  returns the exepected tuple to populate the gradio interface.
 
99
  for waveform in waveforms
100
  ]
101
 
102
+ dir_predictions_csv = Path("./data/predictions/csv/")
103
+ dir_predictions_csv.mkdir(parents=True, exist_ok=True)
104
+ filepath_predictions_csv = dir_predictions_csv / f"{Path(audio_filepath).stem}.csv"
105
+ df.to_csv(filepath_predictions_csv, index=False)
106
+
107
  spectrograms_pil_images = [Image.fromarray(a) for a in spectrograms_array_images]
108
 
109
  predictions = model.predict(spectrograms_pil_images)
 
111
  Image.fromarray(bgr_to_rgb(p.plot())) for p in predictions
112
  ]
113
 
114
+ return (
115
+ pil_image_spectrogram_with_predictions,
116
+ df[CSV_COLUMNS],
117
+ prediction_to_str(df=df),
118
+ filepath_predictions_csv,
119
+ )
120
 
121
 
122
  def examples(dir_examples: Path) -> list[Path]:
 
160
  headers=CSV_COLUMNS,
161
  label="prediction as CSV",
162
  )
163
+ output_download_button = gr.DownloadButton(label="Download CSV")
164
 
165
  fn = lambda audio_filepath: interface_fn(
166
  model=model,
 
171
  title="ML model for forest elephant rumble detection 🐘",
172
  fn=fn,
173
  inputs=input,
174
+ outputs=[output_gallery, output_dataframe, output_raw, output_download_button],
175
  examples=sound_filepaths,
176
  flagging_mode="never",
177
  )