shaml / frontend /src /api.ts
Cursor Agent
Add editable transcription workflow and Supabase save
536f592 unverified
raw
history blame contribute delete
2.58 kB
const API_BASE = import.meta.env.VITE_API_BASE_URL ?? "/api";
export type TranscribeResult = {
transcript: string;
extracted_text: string;
};
export type DiacritizeResult = {
input: string;
diacritized: string;
};
export type SaveRecordResult = {
id: string;
audio_path: string;
audio_url: string;
message: string;
};
function authHeaders(): HeadersInit {
const h: HeadersInit = {};
const key = import.meta.env.VITE_API_KEY;
if (key) {
h["X-API-Key"] = key;
}
return h;
}
function jsonHeaders(): HeadersInit {
return { "Content-Type": "application/json", ...authHeaders() };
}
async function parseError(res: Response): Promise<string> {
try {
const data = await res.json();
if (typeof data?.detail === "string") return data.detail;
if (Array.isArray(data?.detail)) {
return data.detail.map((d: { msg?: string }) => d.msg ?? JSON.stringify(d)).join(" — ");
}
} catch {
/* ignore */
}
return `خطأ في الخادم (${res.status})`;
}
export async function transcribeAudio(file: File): Promise<TranscribeResult> {
const form = new FormData();
form.append("file", file);
const res = await fetch(`${API_BASE}/transcribe/audio`, {
method: "POST",
headers: authHeaders(),
body: form,
});
if (!res.ok) throw new Error(await parseError(res));
return res.json();
}
export async function diacritizeText(
text: string,
asrText?: string,
): Promise<DiacritizeResult> {
const res = await fetch(`${API_BASE}/diacritize`, {
method: "POST",
headers: jsonHeaders(),
body: JSON.stringify({
text,
asr_text: asrText || undefined,
}),
});
if (!res.ok) throw new Error(await parseError(res));
return res.json();
}
export async function saveRecord(
file: File,
extractedText: string,
editedText: string,
diacritizedText: string,
): Promise<SaveRecordResult> {
const form = new FormData();
form.append("file", file);
form.append("extracted_text", extractedText);
form.append("edited_text", editedText);
form.append("diacritized_text", diacritizedText);
const res = await fetch(`${API_BASE}/records/save`, {
method: "POST",
headers: authHeaders(),
body: form,
});
if (!res.ok) throw new Error(await parseError(res));
return res.json();
}
export async function checkHealth(): Promise<{
status: string;
model_loaded: boolean;
asr_model?: string;
supabase_enabled?: boolean;
}> {
const res = await fetch(`${API_BASE}/health`);
if (!res.ok) throw new Error("الخادم غير متاح");
return res.json();
}