File size: 8,395 Bytes
9a03fcf
 
 
d588824
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a03fcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d588824
 
 
 
9a03fcf
 
d588824
 
 
 
9a03fcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d588824
 
 
 
 
 
 
9a03fcf
 
 
d588824
 
 
 
 
 
9a03fcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d588824
 
 
 
9a03fcf
 
d588824
 
 
 
9a03fcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d588824
9a03fcf
 
 
d588824
 
 
 
 
 
 
9a03fcf
d588824
 
 
 
 
 
 
 
 
9a03fcf
d588824
9a03fcf
 
 
 
d588824
 
 
 
 
 
 
 
 
 
9a03fcf
 
 
 
 
d588824
9a03fcf
 
 
 
 
 
 
d588824
 
 
 
9a03fcf
 
 
 
 
d588824
9a03fcf
 
 
 
 
 
 
 
 
 
d588824
 
 
9a03fcf
d588824
 
 
9a03fcf
d588824
 
 
9a03fcf
d588824
9a03fcf
d588824
 
 
 
 
 
 
 
 
 
 
 
9a03fcf
d588824
 
 
 
 
 
9a03fcf
 
 
 
 
 
 
 
 
 
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
247
248
249
250
251
252
253
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 (
        <img 
          src={example.path} 
          alt={example.name} 
          className="w-full h-auto rounded object-cover"
        />
      );
    } else if (isAudio) {
      return (
        <audio 
          controls 
          src={example.path} 
          className="w-full mt-2"
        >
          Your browser does not support audio.
        </audio>
      );
    } else {
      return <div className="p-4 bg-gray-100 text-gray-500 text-center rounded">Unsupported file type</div>;
    }
  };

  return (
    <div className="mt-4">
      <h2 className="text-2xl font-display font-medium text-secondary mb-6">
        Example Viewer - {benchmark.charAt(0).toUpperCase() + benchmark.slice(1)} Watermarks
      </h2>

      <div className="flex flex-wrap gap-4 mb-6">
        <div className="flex flex-col">
          <label htmlFor="model-select" className="mb-2 text-sm font-medium text-gray-700">
            Model:
          </label>
          <select
            id="model-select"
            value={selectedModel}
            onChange={handleModelChange}
            disabled={loading || models.length === 0}
            className="border rounded-md px-3 py-2 focus:outline-none focus:ring-2 focus:ring-primary/50 min-w-[200px]"
          >
            {models.map(model => (
              <option key={model} value={model}>{model}</option>
            ))}
          </select>
        </div>

        <div className="flex flex-col">
          <label htmlFor="attack-select" className="mb-2 text-sm font-medium text-gray-700">
            Attack:
          </label>
          <select
            id="attack-select"
            value={selectedAttack}
            onChange={handleAttackChange}
            disabled={loading}
            className="border rounded-md px-3 py-2 focus:outline-none focus:ring-2 focus:ring-primary/50 min-w-[200px]"
          >
            <option value="">All Attacks</option>
            {attacks.map(attack => (
              <option key={attack.name} value={attack.name}>{attack.name}</option>
            ))}
          </select>
        </div>
      </div>

      {loading ? (
        <div className="flex justify-center items-center p-8">
          <div className="animate-pulse text-primary font-medium">Loading examples...</div>
        </div>
      ) : error ? (
        <div className="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-md">
          Error: {error}
        </div>
      ) : examples.length === 0 ? (
        <div className="bg-yellow-50 border border-yellow-200 text-yellow-700 px-4 py-3 rounded-md">
          No examples found for the selected options.
        </div>
      ) : (
        <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
          {examples.map((example, index) => (
            <div key={index} className="bg-white rounded-lg shadow-md overflow-hidden border border-gray-200 hover:shadow-lg transition-shadow">
              <div className="bg-primary text-white px-4 py-3">
                <h3 className="font-medium truncate">{example.name}</h3>
              </div>
              
              {example.attack && (
                <div className="px-4 py-2 bg-gray-50 border-b border-gray-200 text-sm text-gray-600">
                  Attack: {example.attack}
                </div>
              )}
              
              <div className="p-4">
                {renderExampleContent(example)}
                
                {example.hasOwnProperty('detected') && (
                  <div className={`mt-3 text-sm font-medium ${example.detected ? 'text-green-600' : 'text-red-600'}`}>
                    Watermark {example.detected ? 'detected ✓' : 'not detected ✗'}
                  </div>
                )}
              </div>
            </div>
          ))}
        </div>
      )}
    </div>
  );
}

export default ExampleViewer;