File size: 2,866 Bytes
a1f1bf8
9bf569f
a1f1bf8
ed37070
 
0b598b9
 
a1f1bf8
 
 
0b598b9
 
 
a1f1bf8
 
 
0b598b9
98847a8
 
cca4f79
 
 
 
 
98847a8
 
 
 
 
 
ed37070
 
9bf569f
 
 
 
ed37070
 
b5bd664
 
 
 
 
 
 
d411f8a
d985ebc
 
 
 
 
 
 
d411f8a
 
 
 
cca4f79
 
 
 
 
 
 
 
 
 
 
 
 
 
0b598b9
 
a1f1bf8
cca4f79
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
const VITE_API_SERVER_URL = import.meta.env.VITE_API_SERVER_URL || ''
const VITE_LOAD_VIA_PROXY_URL = import.meta.env.VITE_LOAD_VIA_PROXY_URL === 'true'

console.log(`API Server URL: ${VITE_API_SERVER_URL}`)

class API {
  static async fetchIndex(): Promise<string> {
    const response = await fetch(VITE_API_SERVER_URL + '/')
    if (!response.ok) throw new Error('Failed to fetch index.html')
    return response.text()
  }

  static async fetchStaticFile(path: string): Promise<string> {
    const response = await fetch(`${VITE_API_SERVER_URL}/${path}`)
    if (!response.ok) throw new Error(`Failed to fetch ${path}`)
    return response.text()
  }

  // Rename the method to fetchExamplesByType
  static fetchExamplesByType(type: 'image' | 'audio' | 'video', dataset?: string): Promise<any> {
    const url = dataset
      ? `${VITE_API_SERVER_URL}/examples/${type}?dataset=${encodeURIComponent(dataset)}`
      : `${VITE_API_SERVER_URL}/examples/${type}`
    return fetch(url).then((response) => {
      if (!response.ok) {
        throw new Error(`Failed to fetch examples of type ${type}`)
      }
      return response.json()
    })
  }

  // Add a method to fetch a resource via the proxy endpoint to bypass CORS issues
  static getProxiedUrl(url: string, forceProxy = false): string {
    if (!VITE_LOAD_VIA_PROXY_URL && !forceProxy) {
      return url
    }
    return `${VITE_API_SERVER_URL}/proxy/${encodeURIComponent(url)}`
  }

  // Fetch dataset names from the backend, grouped by type
  static async fetchDatasets(): Promise<{ [type: string]: string[] }> {
    const response = await fetch(VITE_API_SERVER_URL + '/datasets')
    if (!response.ok) throw new Error('Failed to fetch datasets')
    return response.json()
  }

  // Fetch descriptions from the backend
  static async fetchDescriptions(): Promise<{
    descriptions: any;
    model_descriptions: any;
    metric_descriptions: any;
    dataset_descriptions: any;
  }> {
    const response = await fetch(VITE_API_SERVER_URL + '/descriptions')
    if (!response.ok) throw new Error('Failed to fetch descriptions')
    return response.json()
  }

  // Fetch leaderboard data from the backend
  static async fetchLeaderboard(datasetName: string): Promise<any> {
    const response = await fetch(`${VITE_API_SERVER_URL}/data/${datasetName}?dataset_type=benchmark`)
    if (!response.ok) throw new Error(`Failed to fetch leaderboard for ${datasetName}`)
    return response.json()
  }

  // Fetch leaderboard data from the backend
  static async fetchChart(datasetName: string): Promise<any> {
    const response = await fetch(`${VITE_API_SERVER_URL}/data/${datasetName}?dataset_type=attacks_variations`)
    if (!response.ok) throw new Error(`Failed to fetch chart data for ${datasetName}`)
    return response.json()
  }
}

export default API
export { VITE_API_SERVER_URL as API_BASE_URL }