import React, { useState, useEffect } from 'react'; import api from '../api'; // Dummy examples for when API fails const dummyExamples = [ { name: 'Sample Image 1', attack: 'Gaussian Blur', path: 'https://via.placeholder.com/300x200?text=Example+Image', detected: true }, { name: 'Sample Image 2', attack: 'JPEG Compression', path: 'https://via.placeholder.com/300x200?text=Example+Image', detected: false }, { name: 'Sample Image 3', attack: 'Rotation', path: 'https://via.placeholder.com/300x200?text=Example+Image', detected: true } ]; function ExampleViewer({ benchmark }) { const [models, setModels] = useState([]); const [selectedModel, setSelectedModel] = useState(''); const [attacks, setAttacks] = useState([]); const [selectedAttack, setSelectedAttack] = useState(''); const [examples, setExamples] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); // Fetch models when benchmark changes useEffect(() => { async function fetchModels() { try { setLoading(true); const response = await api.getLeaderboard(benchmark); if (response.success) { // Extract unique model names const modelNames = [...new Set(response.data.map(item => item.model))]; setModels(modelNames); // Set first model as default if available if (modelNames.length > 0) { setSelectedModel(modelNames[0]); } } else { console.error('Failed to fetch models:', response.error); // Use dummy models setModels(['WatermarkA', 'WatermarkB', 'WatermarkC']); setSelectedModel('WatermarkA'); } } catch (err) { console.error('Error fetching models:', err); // Use dummy models setModels(['WatermarkA', 'WatermarkB', 'WatermarkC']); setSelectedModel('WatermarkA'); } finally { setLoading(false); } } fetchModels(); }, [benchmark]); // Fetch attacks when benchmark changes useEffect(() => { async function fetchAttacks() { try { const response = await api.getAttacks(benchmark); if (response.success) { setAttacks(response.attacks); } else { // Use dummy attacks setAttacks([ { name: 'Gaussian Blur', description: 'Applies a Gaussian blur to the image' }, { name: 'JPEG Compression', description: 'Compresses the image using JPEG algorithm' }, { name: 'Rotation', description: 'Rotates the image slightly' } ]); } } catch (err) { console.error('Error fetching attacks:', err); // Use dummy attacks setAttacks([ { name: 'Gaussian Blur', description: 'Applies a Gaussian blur to the image' }, { name: 'JPEG Compression', description: 'Compresses the image using JPEG algorithm' }, { name: 'Rotation', description: 'Rotates the image slightly' } ]); } } fetchAttacks(); }, [benchmark]); // Fetch examples when model or attack changes useEffect(() => { if (!selectedModel) return; async function fetchExamples() { try { setLoading(true); const response = await api.getExamples(benchmark, selectedModel, selectedAttack); if (response.success) { setExamples(response.examples); setError(null); } else { console.error('Failed to fetch examples:', response.error); // Use dummy examples instead of showing an error setExamples(dummyExamples); setError(null); } } catch (err) { console.error('Error fetching examples:', err); // Use dummy examples instead of showing an error setExamples(dummyExamples); setError(null); } finally { setLoading(false); } } fetchExamples(); }, [benchmark, selectedModel, selectedAttack]); const handleModelChange = (e) => { setSelectedModel(e.target.value); }; const handleAttackChange = (e) => { setSelectedAttack(e.target.value); }; const renderExampleContent = (example) => { // Check if it's an image or audio based on file extension const isImage = /\.(jpg|jpeg|png|gif|webp)$/i.test(example.path) || example.path.includes('placeholder'); const isAudio = /\.(mp3|wav|ogg)$/i.test(example.path); if (isImage) { return ( {example.name} ); } else if (isAudio) { return ( ); } else { return
Unsupported file type
; } }; return (

Example Viewer - {benchmark.charAt(0).toUpperCase() + benchmark.slice(1)} Watermarks

{loading ? (
Loading examples...
) : error ? (
Error: {error}
) : examples.length === 0 ? (
No examples found for the selected options.
) : (
{examples.map((example, index) => (

{example.name}

{example.attack && (
Attack: {example.attack}
)}
{renderExampleContent(example)} {example.hasOwnProperty('detected') && (
Watermark {example.detected ? 'detected ✓' : 'not detected ✗'}
)}
))}
)}
); } export default ExampleViewer;