kirimaru/Bat_Identification
0
1"""2 3Step 1: Run Your Script4Save the above code in a file, for example, app.py. Then, run the script using Python:5 6python app.py7 8Step 2: Access the Gradio Interface9After running the script, you should see output in the terminal indicating that the Gradio interface is running. It will provide a local URL (usually http://localhost:7860) that you can open in your web browser to interact with your Gradio app.10 11Additional Features12Gradio supports various input and output types, including images, audio, and more. You can customize the interface further by adding more inputs and outputs, changing the layout, and adding examples.13 14For more advanced usage and features, you can refer to the Gradio documentation.15 16"""17 18import gradio as gr19import matplotlib.pyplot as plt20import pandas as pd21import numpy as np22 23import bat_detect.utils.detector_utils as du24import bat_detect.utils.audio_utils as au25import bat_detect.utils.plot_utils as viz26 27 28# setup the arguments29args = {}30args = du.get_default_bd_args()31args['detection_threshold'] = 0.332args['time_expansion_factor'] = 133args['model_path'] = 'models/Net2DFast_UK_same.pth.tar'34max_duration = 10.035 36# load the model37model, params = du.load_model(args['model_path'])38 39 40prediction_df = gr.Dataframe(41 headers=["species", "time", "detection_prob", "species_prob"],42 datatype=["str", "str", "str", "str"],43 row_count=1,44 col_count=(4, "fixed"),45 # max_height=300,46 elem_classes="qa-pairs",47 label='Predictions'48 )49 50prediction_css = """51.qa-pairs .table-wrap {52 min-height: 300px;53 max-height: 300px;54}55 56#visualisation-container {57 overflow-x: auto;58 width: 100%;59 height: 200px; /* Fixed height */60}61 62#visualisation-container img {63 height: 100%; /* fixed height */64 width: auto; /* flexible width */65 object-fit: contain; 66}67"""68 69examples = [['example_data/audio/20170701_213954-MYOMYS-LR_0_0.5.wav', 0.3],70 ['example_data/audio/20180530_213516-EPTSER-LR_0_0.5.wav', 0.3],71 ['example_data/audio/20180627_215323-RHIFER-LR_0_0.5.wav', 0.3],72 ['example_data/audio/Myotis daubentonii_A004034_PVTRQZEYAT.flac', 0.3],73 # ['example_data/audio/Eptesicus serotinus_A003974_RLNMCAZDEJ.flac', 0.3],74 # ['example_data/audio/Nyctalus noctula_A004093_DOEYCSPEZD.flac', 0.3],75]76 77 78def make_prediction(file_name=None, detection_threshold=0.3):79 80 if file_name is not None:81 audio_file = file_name82 else:83 return "You must provide an input audio file."84 85 if detection_threshold is not None and detection_threshold != '':86 args['detection_threshold'] = float(detection_threshold)87 88 # process the file to generate predictions89 results = du.process_file(audio_file, model, params, args, max_duration=max_duration)90 # results = du.process_file(audio_file, model, params, args, max_duration=False)91 92 anns = [ann for ann in results['pred_dict']['annotation']]93 clss = [aa['class'] for aa in anns]94 st_time = [aa['start_time'] for aa in anns]95 cls_prob = [aa['class_prob'] for aa in anns]96 det_prob = [aa['det_prob'] for aa in anns]97 data = {'species': clss, 'time': st_time, 'detection_prob': det_prob, 'species_prob': cls_prob}98 99 prediction_df = pd.DataFrame(data=data)100 im = generate_results_image(audio_file, anns)101 102 return [prediction_df, im]103 104 105def generate_results_image(audio_file, anns): 106 107 # load audio108 sampling_rate, audio = au.load_audio_file(audio_file, args['time_expansion_factor'], 109 params['target_samp_rate'], params['scale_raw_audio'], max_duration=max_duration)110 duration = audio.shape[0] / sampling_rate111 112 # generate spec113 spec, spec_viz = au.generate_spectrogram(audio, sampling_rate, params, True, False)114 115 # create fig116 plt.close('all')117 # Adjust figsize calculation to control the width of the spectrogram118 fig_width = max(6, spec.shape[1] / 100) # Minimum width of 6 inches, scales with spectrogram width119 fig_height = spec.shape[0] / 100 # Adjust the divisor for height scaling (higher = smaller)120 fig = plt.figure(1, figsize=(fig_width, fig_height), dpi=100, frameon=False)121 122 spec_duration = au.x_coords_to_time(spec.shape[1], sampling_rate, params['fft_win_length'], params['fft_overlap'])123 viz.create_box_image(spec, fig, anns, 0, spec_duration, spec_duration, params, spec.max()*1.1, False, True)124 plt.ylabel('Freq - kHz')125 plt.xlabel('Time - secs')126 plt.tight_layout()127 128 # convert fig to image129 fig.canvas.draw()130 data = np.frombuffer(fig.canvas.buffer_rgba(), dtype=np.uint8)131 w, h = fig.canvas.get_width_height()132 im = data.reshape((int(h), int(w), -1))133 134 return im135 136 137descr_txt = "Demo of Bat identification tools. " \138 "<br>It is based on two state-of-the-art models BatDetect2 and Bat-cli. " \139 "For the demo purposes, the input file is longer than 10 seconds, only the first 10 seconds will be processed." \140 # "<br>Check out the two papers for more details [here](https://www.biorxiv.org/content/10.1101/2022.12.14.520490v1) and [here](https://ar5iv.labs.arxiv.org/html/2309.11218)."141 142Gradio_interface = gr.Interface(143 fn = make_prediction,144 inputs = [gr.Audio(sources=["upload"], type="filepath"), 145 gr.Dropdown([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])],146 outputs = [prediction_df, gr.Image(interactive=False, label="Visualisation", elem_id="visualisation-container")],147 # theme = "huggingface",148 title = "Bat Identification Demo",149 description = descr_txt,150 examples = examples,151 allow_flagging = 'never',152 css=prediction_css153)154 155Gradio_interface.launch()156 