Spaces:
Running
Running
File size: 9,333 Bytes
503a577 08dfd47 503a577 9bf569f eb27538 9eea4a2 08dfd47 9eea4a2 44072a9 503a577 eb27538 08dfd47 9eea4a2 08dfd47 eb27538 9eea4a2 08dfd47 eb27538 503a577 eb27538 08dfd47 eb27538 503a577 eb27538 9eea4a2 08dfd47 eb27538 08dfd47 eb27538 08dfd47 44072a9 08dfd47 eb27538 08dfd47 9bf569f 08dfd47 eb27538 9eea4a2 eb27538 503a577 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
import React from 'react'
import AudioPlayer, { AudioPlayerHandle } from './AudioPlayer'
import type { ExamplesData } from './Examples'
import { groupByNameAndVariant, VARIANT_NAME_MAP } from './galleryUtils'
import ExampleDetailsSection from './ExampleDetailsSection'
import ExampleVariantSelector from './ExampleVariantSelector'
import ExampleVariantMetricsTables from './ExampleVariantMetricsTable'
import ExampleVariantToggle, { handleVariantToggleClick } from './ExampleVariantToggle'
import LoadingSpinner from './LoadingSpinner'
import API from '../API'
import { InformationCircleIcon } from '@heroicons/react/24/outline'
import { Tooltip as ReactTooltip } from 'react-tooltip'
import 'react-tooltip/dist/react-tooltip.css'
interface GalleryProps {
selectedModel: string
selectedAttack: string
examples: {
[model: string]: {
[attack: string]: ExamplesData[]
}
}
}
const AudioGallery: React.FC<GalleryProps> = ({ selectedModel, selectedAttack, examples }) => {
const exampleItems = examples[selectedModel][selectedAttack]
const grouped = groupByNameAndVariant(exampleItems)
const audioNames = Object.keys(grouped)
const [selectedAudio, setSelectedAudio] = React.useState(audioNames[0] || '')
const variants = grouped[selectedAudio] || {}
const variantKeys = Object.keys(variants)
const [selectedVariant, setSelectedVariant] = React.useState(variantKeys[0] || '')
const [toggleMode, setToggleMode] = React.useState<'wmd' | 'attacked'>('wmd')
// Shared playback state
const playbackTimeRef = React.useRef(0)
const [playingVariant, setPlayingVariant] = React.useState<string | null>(null)
// Refs for all players
const audioRefs = React.useMemo(() => {
const refs: Record<string, React.RefObject<AudioPlayerHandle>> = {}
variantKeys.forEach((v) => {
refs[v] = React.createRef<AudioPlayerHandle>()
})
return refs
}, [variantKeys.join(',')])
// Add state for rewind seconds
const [rewindSeconds, setRewindSeconds] = React.useState(0.5)
const [imageLoading, setImageLoading] = React.useState(true)
// Play handler: pause all others, sync time
const handlePlay = (variant: string) => {
console.log(`Playing variant: ${variant}`)
setPlayingVariant(variant)
variantKeys.forEach((v) => {
if (v !== variant && audioRefs[v]?.current) {
audioRefs[v]?.current?.pause()
// audioRefs[v]?.current?.setTime(playbackTimeRef.current)
}
})
}
// Pause handler
const handlePause = (variant: string) => {
console.log(`Pausing variant: ${variant}`)
if (playingVariant === variant) setPlayingVariant(null)
}
React.useEffect(() => {
setSelectedVariant(variantKeys[0] || '')
}, [selectedAudio])
// Reset image loading state when selected variant changes
React.useEffect(() => {
if (variants[selectedVariant]?.image_url) {
setImageLoading(true)
}
}, [selectedVariant, variants[selectedVariant]?.image_url])
// When selectedVariant changes, play that variant and pause others, syncing position
React.useEffect(() => {
if (!selectedVariant) {
return
}
if (playingVariant == null) {
// On page load don't auto play, only when swapping tracks
return
}
// Rewind playbackTimeRef by rewindSeconds, clamp to 0
playbackTimeRef.current = Math.max(0, playbackTimeRef.current - rewindSeconds)
setPlayingVariant(selectedVariant)
variantKeys.forEach((v) => {
if (v !== selectedVariant) {
audioRefs[v]?.current?.pause()
}
if (audioRefs[v]?.current) {
audioRefs[v]?.current?.setTime(playbackTimeRef.current)
}
})
}, [selectedVariant])
console.log(audioRefs[selectedVariant]?.current?.getCurrentTime())
if (!audioNames.length) {
return (
<div className="w-full mt-12 flex items-center justify-center">
<div className="text-gray-500">
No audio examples available. Please select another model and attack.
</div>
</div>
)
}
return (
<div className="w-full overflow-auto" style={{ minHeight: '100vh' }}>
<div className="example-display">
<div className="mb-4">
<fieldset className="fieldset">
<legend className="fieldset-legend">Audio</legend>
<select
className="select select-bordered"
value={selectedAudio || ''}
onChange={(e) => {
setSelectedAudio(e.target.value || '')
}}
>
{audioNames.map((name) => (
<option key={name} value={name}>
{name}
</option>
))}
</select>
</fieldset>
</div>
{selectedAudio && selectedVariant && variants[selectedVariant] && (
<>
<ExampleVariantMetricsTables
variantMetadatas={Object.fromEntries(
variantKeys.map((v) => [v, variants[v]?.metadata || {}])
)}
/>
<ExampleDetailsSection>
<ExampleVariantSelector
variantKeys={variantKeys}
selectedVariant={selectedVariant}
setSelectedVariant={setSelectedVariant}
/>
<ReactTooltip
id="variant-selector-tooltip"
place="top"
className="z-[10000] max-w-xs !opacity-100 bg-base-100 text-base-content"
style={{ boxShadow: '0 0 10px rgba(0,0,0,0.2)', zIndex: 10000 }}
positionStrategy="fixed"
>
<div className="p-2 text-xs text-left relative z-[10000]">
You can also change the variant using keys <b>1</b>, <b>2</b>, <b>3</b>, <b>4</b>{' '}
on your keyboard.
</div>
</ReactTooltip>
<ExampleVariantToggle
toggleMode={toggleMode}
setToggleMode={setToggleMode}
selectedVariant={selectedVariant}
setSelectedVariant={setSelectedVariant}
variantKeys={variantKeys}
/>
<fieldset className="fieldset mt-2">
<legend className="fieldset-legend">Rewind Seconds</legend>
<input
id="rewind-seconds"
type="number"
min={0}
step={0.1}
value={rewindSeconds}
onChange={(e) => setRewindSeconds(Math.max(0, Number(e.target.value)))}
className="input input-bordered w-20"
placeholder="Seconds"
/>
</fieldset>
<div className="flex flex-col items-center gap-4">
{variantKeys.map((variantKey) =>
variants[variantKey].audio_url ? (
<div
key={variantKey}
style={{
width: '100%',
display: selectedVariant === variantKey ? 'block' : 'none',
}}
>
<div className="font-mono text-xs mb-1">{variantKey}</div>
<AudioPlayer
ref={audioRefs[variantKey]}
src={API.getProxiedUrl(variants[variantKey].audio_url, true)}
playing={playingVariant === variantKey}
onPlay={() => handlePlay(variantKey)}
onPause={() => handlePause(variantKey)}
onAudioProcess={(currentTime) => {
playbackTimeRef.current = currentTime
variantKeys.forEach((v) => {
if (v !== variantKey && audioRefs[v]?.current) {
audioRefs[v]?.current?.setTime(currentTime)
}
})
}}
/>
</div>
) : null
)}
{variants[selectedVariant].image_url && (
<div className="relative">
{imageLoading && <LoadingSpinner />}
<img
key={variants[selectedVariant].image_url}
src={API.getProxiedUrl(variants[selectedVariant].image_url)}
alt={selectedAudio}
className="example-image"
style={{
display: 'block',
opacity: imageLoading ? 0 : 1,
transition: 'opacity 0.3s',
}}
onLoad={() => setImageLoading(false)}
onError={() => setImageLoading(false)}
onClick={() =>
handleVariantToggleClick(
toggleMode,
selectedVariant,
setSelectedVariant,
variantKeys
)
}
/>
</div>
)}
</div>
</ExampleDetailsSection>
</>
)}
</div>
</div>
)
}
export default AudioGallery
|