File size: 4,497 Bytes
e6a1f55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import axios from "axios";
import type {
  AdversarialPoint,
  BenchmarksResponse,
  DatasetInfo,
  DetectResponse,
  ExplainabilityExample,
  HealthResponse,
  MetricInfo,
  ModelEvaluation,
  ModelsListResponse,
  RadarResponse,
  SamplesResponse,
  VisualizeResponse,
} from "@/types/inference";

const API_BASE_URL =
  (import.meta as ImportMeta & { env?: Record<string, string> }).env
    ?.VITE_API_BASE_URL || "http://localhost:8000";
const WS_BASE_URL =
  (import.meta as ImportMeta & { env?: Record<string, string> }).env
    ?.VITE_WS_BASE_URL || "ws://localhost:8000";

export const apiBaseUrl = API_BASE_URL;
export const wsBaseUrl = WS_BASE_URL;

export const api = axios.create({
  baseURL: API_BASE_URL,
  timeout: 120_000, // first HF download can take a while
});

// ---------------------------------------------------------------- meta -- //

export async function getHealth(): Promise<HealthResponse> {
  const { data } = await api.get<HealthResponse>("/api/health");
  return data;
}

export async function listModels(): Promise<ModelsListResponse> {
  const { data } = await api.get<ModelsListResponse>("/api/models");
  return data;
}

export async function listSamples(): Promise<SamplesResponse> {
  const { data } = await api.get<SamplesResponse>("/api/samples");
  return data;
}

export function sampleDownloadUrl(sampleId: string): string {
  return `${API_BASE_URL}/api/samples/${encodeURIComponent(sampleId)}/download`;
}

// ----------------------------------------------------------- benchmarks -- //

export async function getBenchmarks(): Promise<BenchmarksResponse> {
  const { data } = await api.get<BenchmarksResponse>("/api/benchmarks");
  return data;
}

export async function getRadar(): Promise<RadarResponse> {
  const { data } = await api.get<RadarResponse>("/api/benchmarks/radar");
  return data;
}

export async function getHighlights(): Promise<{ highlights: string[] }> {
  const { data } = await api.get<{ highlights: string[] }>(
    "/api/benchmarks/highlights"
  );
  return data;
}

export async function getBenchmarkDatasets(): Promise<{ datasets: DatasetInfo[] }> {
  const { data } = await api.get<{ datasets: DatasetInfo[] }>("/api/benchmarks/datasets");
  return data;
}

export async function getBenchmarkMetrics(): Promise<{ metrics: MetricInfo[] }> {
  const { data } = await api.get<{ metrics: MetricInfo[] }>("/api/benchmarks/metrics");
  return data;
}

export async function getModelEvaluations(): Promise<{ evaluations: ModelEvaluation[] }> {
  const { data } = await api.get<{ evaluations: ModelEvaluation[] }>("/api/benchmarks/evaluations");
  return data;
}

export async function getAdversarialCurve(): Promise<{ epsilon_curve: AdversarialPoint[] }> {
  const { data } = await api.get<{ epsilon_curve: AdversarialPoint[] }>("/api/benchmarks/adversarial");
  return data;
}

export async function getExplainabilityExamples(): Promise<{ examples: ExplainabilityExample[] }> {
  const { data } = await api.get<{ examples: ExplainabilityExample[] }>(
    "/api/benchmarks/explainability"
  );
  return data;
}

// ------------------------------------------------------------ inference -- //

export async function detectAudio(
  file: File,
  models?: string[],
  returnFeatures = true
): Promise<DetectResponse> {
  const fd = new FormData();
  fd.append("audio_file", file);
  if (models && models.length) fd.append("models", JSON.stringify(models));
  fd.append("return_features", String(returnFeatures));
  const { data } = await api.post<DetectResponse>("/api/detect", fd, {
    headers: { "Content-Type": "multipart/form-data" },
  });
  return data;
}

export async function detectSample(
  sampleId: string,
  models?: string[],
  returnFeatures = true
): Promise<DetectResponse> {
  const fd = new FormData();
  if (models && models.length) fd.append("models", JSON.stringify(models));
  fd.append("return_features", String(returnFeatures));
  const { data } = await api.post<DetectResponse>(
    `/api/detect/sample/${encodeURIComponent(sampleId)}`,
    fd
  );
  return data;
}

export async function visualizeAudio(file: File): Promise<VisualizeResponse> {
  const fd = new FormData();
  fd.append("audio_file", file);
  const { data } = await api.post<VisualizeResponse>("/api/visualize", fd, {
    headers: { "Content-Type": "multipart/form-data" },
  });
  return data;
}

export function inferenceWebSocketUrl(sessionId: string): string {
  return `${WS_BASE_URL}/ws/inference/${encodeURIComponent(sessionId)}`;
}