
    1Vh"                        d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZ d
ZdZej$                  j'                  d      diZ e eej/                               egz         Z G d dej2                        Zy)zThe TensorBoard Audio plugin.    N)wrappers)errors)plugin_util)	http_util)provider)base_plugin)metadatazapplication/octet-stream
   WAVz	audio/wavc                       e Zd ZdZej
                  Zd Zd Zd Z	d Z
d Zej                  j                  d        Zd Zd	 Zej                  j                  d
        Zej                  j                  d        Zy)AudioPluginzAudio Plugin for TensorBoard.c                     |j                   | _        |j                  xs i j                  | j                  t
              | _        t        j                  dd      | _	        y)z{Instantiates AudioPlugin via TensorBoard core.

        Args:
          context: A base_plugin.TBContext instance.
        audior   )	data_kindlatest_known_versionN)
data_provider_data_providersampling_hintsgetplugin_name_DEFAULT_DOWNSAMPLING_downsample_tor   _MetadataVersionChecker_version_checker)selfcontexts     V/home/dcms/DCMS/lib/python3.12/site-packages/tensorboard/plugins/audio/audio_plugin.py__init__zAudioPlugin.__init__-   sW     &33&55;@@3
 !, C C!"!
    c                 J    | j                   | j                  | j                  dS )N)z/audioz/individualAudioz/tags)_serve_audio_metadata_serve_individual_audio_serve_tagsr   s    r   get_plugin_appszAudioPlugin.get_plugin_apps<   s'    00 $ < <%%
 	
r   c                      y)NF r$   s    r   	is_activezAudioPlugin.is_activeC   s    r   c                 .    t        j                  d      S )Nztf-audio-dashboard)element_name)r   FrontendMetadatar$   s    r   frontend_metadatazAudioPlugin.frontend_metadataF   s    ++9MNNr   c                    | j                   j                  ||t        j                        }|D ci c]  }|i  }}|j	                         D ]  \  }}|j	                         D ]  \  }}t        j
                  |j                        }	| j                  j                  |	j                  ||      sMt        j                  |j                        }
|j                  |
|j                  d||   |<     |S c c}w )aU  Return information about the tags in each run.

        Result is a dictionary of the form

            {
              "runName1": {
                "tagName1": {
                  "displayName": "The first tag",
                  "description": "<p>Long ago there was just one tag...</p>",
                  "samples": 3
                },
                "tagName2": ...,
                ...
              },
              "runName2": ...,
              ...
            }

        For each tag, `samples` is the greatest number of audio clips that
        appear at any particular step. (It's not related to "samples of a
        waveform.") For example, if for tag `minibatch_input` there are
        five audio clips at step 0 and ten audio clips at step 1, then the
        dictionary for `"minibatch_input"` will contain `"samples": 10`.
        experiment_idr   )displayNamedescriptionsamples)r   list_blob_sequencesr	   PLUGIN_NAMEitemsparse_plugin_metadataplugin_contentr   okversionr   markdown_to_safe_htmlr1   display_name
max_length)r   ctx
experimentmappingrunresulttag_to_time_seriestagtime_seriesmdr1   s              r   _index_implzAudioPlugin._index_implI   s    2 %%99$ ,, : 

 &--c#r'--'.}} 	#C#$6$<$<$>  [33K4N4NO,,//

CE)??++ $/#;#;#.*55$sC 	  .s   
C5c                    t        j                  |j                        }t        j                  |j                        }|j                  j                  d      }|j                  j                  d      }t        |j                  j                  dd            }| j                  |||||      }t        j                  ||d      S )a  Given a tag and list of runs, serve a list of metadata for audio.

        Note that the actual audio data are not sent; instead, we respond
        with URLs to the audio. The frontend should treat these URLs as
        opaque and should not try to parse information about them or
        generate them itself, as the format may change.

        Args:
          request: A werkzeug.wrappers.Request object.

        Returns:
          A werkzeug.Response application.
        rC   r@   sampler   application/json)
r   r   environr/   argsr   int_audio_response_for_runr   Respond)r   requestr=   r>   rC   r@   rH   responses           r   r!   z!AudioPlugin._serve_audio_metadataw   s     !!'//2 ..w?
llu%llu%W\\%%h23//S#v
   (4FGGr   c           
      V   | j                   j                  ||t        j                  | j                  t        j                  |g|g            }|j                  |i       j                  |d      }|t        j                  d|d|      | j                  ||||      }g }	|D ]  }
t        |
j                        |k  rt        j                  j                  |
j                  |   j                   |d      }|	j#                  |
j$                  d|
j&                  ||d        |	S )	a  Builds a JSON-serializable object with information about audio.

        Args:
          run: The name of the run.
          tag: The name of the tag the audio entries all belong to.
          sample: The zero-indexed sample of the audio sample for which to
          retrieve information. For instance, setting `sample` to `2` will
            fetch information about only the third audio clip of each batch,
            and steps with fewer than three audio clips will be omitted from
            the results.

        Returns:
          A list of dictionaries containing the wall time, step, label,
          content type, and query string for each audio entry.
        )runstags)r/   r   
downsamplerun_tag_filterNNo audio data for run=, tag=)blob_keycontent_type )	wall_timelabelstepcontentTypequery)r   read_blob_sequencesr	   r4   r   r   RunTagFilterr   r   NotFoundError_get_mime_typelenvaluesurllibparse	urlencoderX   appendr[   r]   )r   r=   r>   r@   rC   rH   	all_audior   rY   rP   datumr_   s               r   rM   z#AudioPlugin._audio_response_for_run   s)     '';;$ ,,**#00se3%H < 
	 c2&**35=&&693?  **3
CE 	E5<< 6)LL** %V 4 = =$0E OO!&!JJ#/"	$ r   c                 ^   | j                   j                  ||t        j                        }|j	                  |i       j	                  |d       }|t        j                  d|d|      t        j                  |j                        }t        j	                  |j                  t              S )Nr.   rV   rW   )r   r3   r	   r4   r   r   rb   r6   r7   _MIME_TYPESencoding_DEFAULT_MIME_TYPE)r   r=   r>   r@   rC   r?   rD   parseds           r   rc   zAudioPlugin._get_mime_type   s     %%99$ ,, : 

 kk#r*..sD9&&693?  //0J0JKv0BCCr   c                 b   t        j                  |j                        }t        j                  |j                        }|j                  d   }|t
        vrt        j                  d|z        |j                  d   }| j                  j                  ||      }t        j                  |||      S )zServe encoded audio data.rY   zIllegal mime type %rrX   )rX   )r   r   rJ   r/   rK   _ALLOWED_MIME_TYPESr   InvalidArgumentErrorr   	read_blobr   rN   )r   rO   r=   r>   	mime_typerX   datas          r   r"   z#AudioPlugin._serve_individual_audio   s     !!'//2 ..w?
LL0	//--&2  <<
+"",,S8,D  $	::r   c                     t        j                  |j                        }t        j                  |j                        }| j	                  ||      }t        j                  ||d      S )NrI   )r   r   rJ   r/   rF   r   rN   )r   rO   r=   r>   indexs        r   r#   zAudioPlugin._serve_tags   sR    !!'//2 ..w?
  j1  %1CDDr   N)__name__
__module____qualname____doc__r	   r4   r   r   r%   r(   r,   rF   r   Requestapplicationr!   rM   rc   r"   r#   r'   r   r   r   r   (   s    '&&K

O,\ !!H "H20dD" !!; "; !!E "Er   r   )r|   urllib.parserf   werkzeugr   tensorboardr   r   tensorboard.backendr   tensorboard.datar   tensorboard.pluginsr   tensorboard.plugins.audior	   ro   r   EncodingValuerm   	frozensetlistre   rr   TBPluginr   r'   r   r   <module>r      s    $    # ) % + . 0  E"K  			"4!55 
E+&& Er   