Spaces:
Sleeping
Sleeping
| import axios, { AxiosError } from 'axios' | |
| import type { PredictResponse } from '../types' | |
| const API_BASE = '/api' // proxied to http://localhost:8000 by Vite | |
| export async function predictPathology(files: File[]): Promise<PredictResponse> { | |
| const formData = new FormData() | |
| files.forEach((file) => formData.append('files', file)) | |
| try { | |
| const { data } = await axios.post<PredictResponse>( | |
| `${API_BASE}/v1/predict`, | |
| formData, | |
| { headers: { 'Content-Type': 'multipart/form-data' } }, | |
| ) | |
| return data | |
| } catch (err: unknown) { | |
| if (err instanceof AxiosError) { | |
| // No response at all → server not running or CORS/network block | |
| if (!err.response) { | |
| throw new Error( | |
| 'Không thể kết nối đến máy chủ (cổng 8000). ' + | |
| 'Vui lòng khởi động Python Backend Server rồi thử lại.', | |
| ) | |
| } | |
| // Server responded with an HTTP error status | |
| const body = err.response.data as Record<string, unknown> | undefined | |
| const detail = | |
| (body?.detail as string | undefined) ?? | |
| (body?.error as string | undefined) ?? | |
| err.message | |
| throw new Error(`Lỗi máy chủ (HTTP ${err.response.status}): ${detail}`) | |
| } | |
| // Re-throw anything that isn't an Axios error | |
| throw err | |
| } | |
| } | |