| import axios from "axios"; |
|
|
| |
| |
| const CURRENT_ORIGIN = typeof window !== "undefined" ? window.location.origin : ""; |
|
|
| |
| const BACKEND_URL = process.env.REACT_APP_BACKEND_URL || CURRENT_ORIGIN; |
|
|
| |
| |
| const isHFSpace = typeof window !== "undefined" && window.location.hostname.endsWith(".hf.space"); |
| const HF_SPACE_URL = process.env.REACT_APP_HF_SPACE_URL || (isHFSpace ? CURRENT_ORIGIN : ""); |
|
|
| |
| |
| const useGradio = !!HF_SPACE_URL && isHFSpace; |
|
|
| |
| export const API = `${BACKEND_URL}/api`; |
| export const api = axios.create({ baseURL: API, timeout: 180000 }); |
|
|
| |
| |
| |
| |
| async function gradioCall(fnName, ...args) { |
| const url = `${HF_SPACE_URL}/api/${fnName}`; |
| const resp = await axios.post(url, { data: args }, { timeout: 180000 }); |
| |
| |
| const raw = resp.data?.data?.[0]; |
| if (typeof raw === "string") { |
| try { |
| return JSON.parse(raw); |
| } catch { |
| return raw; |
| } |
| } |
| return raw; |
| } |
|
|
| |
| |
| |
|
|
| export const forgesight = { |
| |
| async health() { |
| if (useGradio) return gradioCall("health"); |
| const { data } = await api.get("/"); |
| return data; |
| }, |
|
|
| |
| async createInspection({ image_base64, notes, product_spec, source }) { |
| if (useGradio) { |
| return gradioCall("inspect", image_base64, notes || "", product_spec || "", source || "upload"); |
| } |
| const { data } = await api.post("/inspections", { image_base64, notes, product_spec, source }); |
| return data; |
| }, |
|
|
| |
| async listInspections(limit = 50) { |
| if (useGradio) return gradioCall("list_inspections", limit); |
| const { data } = await api.get("/inspections", { params: { limit } }); |
| return data; |
| }, |
|
|
| |
| async getInspection(id) { |
| if (useGradio) return gradioCall("get_inspection", id); |
| const { data } = await api.get(`/inspections/${id}`); |
| return data; |
| }, |
|
|
| |
| async getMetrics() { |
| if (useGradio) return gradioCall("metrics"); |
| const { data } = await api.get("/metrics"); |
| return data; |
| }, |
|
|
| |
| async getTelemetry() { |
| if (useGradio) return gradioCall("telemetry"); |
| const { data } = await api.get("/telemetry"); |
| return data; |
| }, |
|
|
| |
| async getBlueprint() { |
| if (useGradio) return gradioCall("blueprint"); |
| const { data } = await api.get("/blueprint"); |
| return data; |
| }, |
|
|
| |
| async listJournal() { |
| if (useGradio) return gradioCall("journal_list"); |
| const { data } = await api.get("/journal"); |
| return data; |
| }, |
|
|
| |
| async createJournal({ title, body, tags }) { |
| if (useGradio) { |
| |
| const tagsStr = Array.isArray(tags) ? tags.join(", ") : tags || ""; |
| return gradioCall("journal_create", title, body, tagsStr); |
| } |
| const { data } = await api.post("/journal", { title, body, tags }); |
| return data; |
| }, |
|
|
| |
| async seedJournal() { |
| if (useGradio) { |
| |
| return { seeded: 0, reason: "auto-seeded via journal_list" }; |
| } |
| const { data } = await api.post("/journal/seed"); |
| return data; |
| }, |
| }; |
|
|
| |
| export const fileToBase64 = (file) => |
| new Promise((resolve, reject) => { |
| const reader = new FileReader(); |
| reader.onload = () => { |
| const str = reader.result; |
| const comma = str.indexOf(","); |
| resolve(comma >= 0 ? str.slice(comma + 1) : str); |
| }; |
| reader.onerror = reject; |
| reader.readAsDataURL(file); |
| }); |
|
|