-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPI.js
249 lines (221 loc) · 6.73 KB
/
API.js
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
// import pRetry from "p-retry";
const FEEDBACK_URL = process.env.REACT_APP_FEEDBACK_URL;
// const HUGGING_FACE_API_KEY = process.env.REACT_APP_HUGGING_FACE_API_KEY;
export const tracking_id = process.env.REACT_APP_GA4_TRACKING_ID;
export const token = localStorage.getItem("access_token")
? localStorage.getItem("access_token")
: process.env.REACT_APP_SB_API_TOKEN;
const asrUrl = `${process.env.REACT_APP_SB_API_URL}/tasks/stt`;
const asrDbUrl = `${process.env.REACT_APP_SB_API_URL}/transcriptions`;
const autoDetectUrl = `${process.env.REACT_APP_SB_API_URL}/tasks/auto_detect_audio_language`;
// const textToSpeechUrl = "https://api-inference.huggingface.co/models/Sunbird/sunbird-lug-tts";
// API.js
export async function detectAudioLanguage(audioData) {
const formData = new FormData();
formData.append("audio", audioData);
try {
const response = await fetch(autoDetectUrl, {
method: 'POST',
headers: {
Accept: 'application/json',
Authorization: `Bearer ${token}`,
},
body: formData,
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data; // Ensure the response contains the detected language code
} catch (error) {
console.error("Error detecting language:", error);
throw error;
}
}
/**
* Recognizes speech from an audio file and returns the transcribed text.
* @param {Blob} audioData - The audio file as a Blob.
* @param {string} languageCode - The language code (e.g., "eng","lug","nyn","teo","lgg","ach").
* @param {string} adapterCode - The adapter code (e.g., "eng","lug","nyn","teo","lgg","ach").
* @return {Promise<string>} The recognized text.
*/
export async function recognizeSpeech(audioData, languageCode, adapterCode) {
const formData = new FormData();
formData.append("audio", audioData); // You might need to adjust the filename.
formData.append("language", languageCode);
formData.append("adapter", adapterCode);
formData.append("whisper", true);
try {
const response = await fetch(asrUrl, {
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
Accept: "application/json",
},
body: formData,
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
console.error("Error recognizing speech:", error);
throw error; // Re-throw the error to be handled by the caller
}
}
export async function getTranscripts() {
const ascendingTranscriptUrl = `${asrDbUrl}?order_by=uploaded&descending=true`;
try {
const response = await fetch(`${ascendingTranscriptUrl}`, {
method: "GET",
headers: {
Authorization: `Bearer ${token}`,
Accept: "application/json",
},
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
console.log(error);
throw error;
}
}
export async function getSingleTranscript(id) {
const asrDbSingleUrl = `${asrDbUrl}/${id}`;
try {
const response = await fetch(asrDbSingleUrl, {
method: "GET",
headers: {
Authorization: `Bearer ${token}`,
Accept: "application/json",
},
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
console.log(error);
throw error;
}
}
export async function updateTranscript(id, transcript) {
const asrDbUpdateUrl = `${asrDbUrl}/${id}`;
const formData = new FormData();
formData.append("transcription_text", transcript);
try {
const response = await fetch(asrDbUpdateUrl, {
method: "PUT",
headers: {
Authorization: `Bearer ${token}`,
Accept: "application/json",
},
body: formData,
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
console.log(error);
throw error;
}
}
export const registerNewAccount = async (values) => {
let data = {};
try {
const response = await fetch(
`${process.env.REACT_APP_SB_API_URL}/auth/register`,
{
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({
username: values.name,
email: values.email,
organization: values.organisation,
password: values.password,
account_type: "Free",
}),
}
);
const responseBody = await response.json();
if (response.status === 400) {
data.error = responseBody.detail || "Invalid username or email";
} else if (response.status === 201) {
data.success = "Account succressfully created";
console.log("message", responseBody);
}
} catch (error) {
data.error = "Something went wrong";
console.error("Error occurred during form submission:", error);
throw error;
}
return data;
};
export const loginIntoAccount = async (values) => {
let data = {};
const formData = new FormData();
formData.append("username", values.username);
formData.append("password", values.password);
try {
const response = await fetch(
`${process.env.REACT_APP_SB_API_URL}/auth/token`,
{
method: "POST",
headers: {
Accept: "application/json",
},
body: formData,
}
);
const responseBody = await response.json();
console.log(`Response: ${response.ok} Status code: ${response.status}`);
if (response.status === 200) {
data.success = "Successful Login!";
localStorage.setItem("access_token", responseBody.access_token);
} else if (response.status === 401) {
data.error = responseBody.detail;
}
} catch (error) {
data.error = "Something went wrong";
console.error("Error occurred during form submission:", error);
throw error;
}
return data;
};
export const sendFeedback = async (
feedback,
CorrectTranslation,
username,
sourceText,
translation,
from,
to
) => {
const time = Date.now();
const requestOptions = {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
Timestamp: time,
feedback: feedback,
SourceText: sourceText,
LanguageFrom: from,
LanguageTo: to,
username: username,
CorrectTranslation: CorrectTranslation,
TranslatedText: translation,
}),
};
const response = await (await fetch(FEEDBACK_URL, requestOptions)).json();
return response;
};