Spaces:
Sleeping
Sleeping
File size: 2,342 Bytes
764531e | 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 | // api.js
// All backend API calls in one place.
// Each function takes exactly what the endpoint needs and returns the response JSON.
const BASE_URL = import.meta.env.DEV ? 'http://localhost:7860' : ''
async function request(endpoint, body) {
const response = await fetch(`${BASE_URL}${endpoint}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
})
if (!response.ok) {
const error = await response.json().catch(() => ({}))
throw new Error(error.detail || `Request failed: ${response.status}`)
}
return response.json()
}
export const api = {
checkDocument: (document, documentType, model) =>
request('/api/check-document', { document, document_type: documentType, model }),
extract: (sessionId, document, customAbbreviations = {}, model) =>
request('/api/extract', {
session_id: sessionId,
document,
custom_abbreviations: customAbbreviations,
model,
}),
extractDecisionPoints: (sessionId, document, structuredInformation, model) =>
request('/api/extract-decision-points', {
session_id: sessionId,
document,
structured_information: structuredInformation,
model,
}),
summarizeDecisionPoints: (sessionId, decisionPointsDisplay, model) =>
request('/api/summarize-decision-points', {
session_id: sessionId,
decision_points_display: decisionPointsDisplay,
model,
}),
summarizeDocument: (sessionId, document, structuredInformation, model) =>
request('/api/summarize-document', {
session_id: sessionId,
document,
structured_information: structuredInformation,
model,
}),
chat: (sessionId, userQuery, document, structuredInformation, decisionPointsSummary, chatHistory, model) =>
request('/api/chat', {
session_id: sessionId,
user_query: userQuery,
document,
structured_information: structuredInformation,
decision_points_summary: decisionPointsSummary,
chat_history: chatHistory,
model,
}),
log: (sessionId, eventType, before, after) =>
request('/api/log', {
session_id: sessionId,
event_type: eventType,
before,
after,
}),
downloadSession: (sessionId) =>
fetch(`${BASE_URL}/api/session/${sessionId}/download`).then(r => r.blob()),
} |