Spaces:
Sleeping
Sleeping
| // 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()), | |
| } |