| export const API_BASE = import.meta.env.VITE_API_BASE || 'https://yahyoha-omnidiag.hf.space'; |
|
|
| |
| const REQUEST_TIMEOUT_MS = 120_000; |
|
|
| |
| |
| |
| |
| class OmniDiagApi { |
| constructor(baseUrl = API_BASE) { |
| this.baseUrl = baseUrl.replace(/\/+$/, ''); |
| this._token = null; |
| } |
|
|
| |
| setToken(token) { |
| this._token = token; |
| } |
|
|
| |
| _authHeader() { |
| return this._token ? { Authorization: `Bearer ${this._token}` } : {}; |
| } |
|
|
| async _fetch(path, options = {}, timeoutMs = REQUEST_TIMEOUT_MS) { |
| const controller = new AbortController(); |
| const timer = setTimeout(() => controller.abort(), timeoutMs); |
|
|
| const url = `${this.baseUrl}${path}`; |
| try { |
| const res = await fetch(url, { |
| headers: { |
| 'Content-Type': 'application/json', |
| ...this._authHeader(), |
| ...options.headers, |
| }, |
| signal: controller.signal, |
| ...options, |
| }); |
| if (!res.ok) { |
| const body = await res.json().catch(() => ({})); |
| const msg = body?.detail?.error || body?.detail || res.statusText; |
| throw new Error(msg); |
| } |
| return res.json(); |
| } catch (err) { |
| if (err.name === 'AbortError') { |
| throw new Error( |
| 'The diagnostic engine is waking up β this can take a minute or two on the first scan of the day. Please try again.' |
| ); |
| } |
| throw err; |
| } finally { |
| clearTimeout(timer); |
| } |
| } |
|
|
| |
| health() { |
| return this._fetch('/'); |
| } |
|
|
| |
| listDiseases() { |
| return this._fetch('/api/v4/diseases'); |
| } |
|
|
| |
| getSchema(disease) { |
| return this._fetch(`/api/v4/${disease}/schema`); |
| } |
|
|
| |
| predict(disease, patientData) { |
| return this._fetch(`/api/v4/${disease}/predict`, { |
| method: 'POST', |
| body: JSON.stringify(patientData), |
| }); |
| } |
|
|
| |
| explain(disease, patientData) { |
| return this._fetch(`/api/v4/${disease}/explain`, { |
| method: 'POST', |
| body: JSON.stringify(patientData), |
| }); |
| } |
|
|
| |
| counterfactuals(disease, patientData) { |
| return this._fetch(`/api/v4/${disease}/counterfactuals`, { |
| method: 'POST', |
| body: JSON.stringify(patientData), |
| }); |
| } |
|
|
| |
| generateReport(payload) { |
| return this._fetch('/api/v4/generate-report', { |
| method: 'POST', |
| body: JSON.stringify(payload), |
| }); |
| } |
|
|
| |
| parseNotes(note, useBert = false) { |
| return this._fetch('/api/v4/parse-notes', { |
| method: 'POST', |
| body: JSON.stringify({ note, use_bert: useBert }), |
| }); |
| } |
| } |
|
|
| export const api = new OmniDiagApi(); |
| export default OmniDiagApi; |
|
|