Spaces:
Sleeping
Sleeping
| import axios from 'axios' | |
| const api = axios.create({ | |
| baseURL: '/api', | |
| timeout: 180000, // 3 min โ model inference can be slow on CPU | |
| }) | |
| // โโ Helper: extract readable error message โโโโโโโโโโโโโโโโโโ | |
| function extractError(err) { | |
| const detail = err?.response?.data?.detail | |
| if (detail) return detail | |
| if (err?.message) return `เฆจเงเฆเฆเฆฏเฆผเฆพเฆฐเงเฆ เฆธเฆฎเฆธเงเฆฏเฆพ: ${err.message}` | |
| return 'เฆเฆเฆเฆฟ เฆ เฆเฆพเฆจเฆพ เฆธเฆฎเฆธเงเฆฏเฆพ เฆนเฆฏเฆผเงเฆเงเฅค' | |
| } | |
| // โโ Health Check โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| export async function checkHealth() { | |
| const res = await api.get('/health') | |
| return res.data | |
| } | |
| // โโ Summarize: PDF file or raw text โโโโโโโโโโโโโโโโโโโโโโโโโ | |
| export async function summarizeDocument({ file, text, onProgress }) { | |
| const formData = new FormData() | |
| if (file) formData.append('file', file) | |
| if (text) formData.append('text', text) | |
| try { | |
| const res = await api.post('/summarize', formData, { | |
| headers: { 'Content-Type': 'multipart/form-data' }, | |
| onUploadProgress: (e) => { | |
| if (onProgress && e.total) { | |
| onProgress(Math.round((e.loaded / e.total) * 100)) | |
| } | |
| }, | |
| }) | |
| return res.data | |
| } catch (err) { | |
| throw new Error(extractError(err)) | |
| } | |
| } | |
| // โโ Chat / QA โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| export async function sendChatMessage({ question, context, summary, history }) { | |
| try { | |
| const res = await api.post('/chat', { | |
| question, | |
| context: context || '', | |
| summary: summary || '', | |
| history: history || [], | |
| }) | |
| return res.data | |
| } catch (err) { | |
| throw new Error(extractError(err)) | |
| } | |
| } | |
| // โโ Transcribe audio blob โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
| export async function transcribeAudio(audioBlob) { | |
| const formData = new FormData() | |
| formData.append('audio', audioBlob, 'recording.webm') | |
| try { | |
| const res = await api.post('/transcribe', formData, { | |
| headers: { 'Content-Type': 'multipart/form-data' }, | |
| }) | |
| return res.data | |
| } catch (err) { | |
| throw new Error(extractError(err)) | |
| } | |
| } | |