File size: 2,583 Bytes
839c48d
 
 
 
df033d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Local dev: Vite proxies `/api` to the FastAPI backend. On Vercel this is
// set to the Hugging Face Space API, e.g.
// VITE_API_BASE_URL=https://arvind112-vision-ai.hf.space/api
const API_BASE = import.meta.env.VITE_API_BASE_URL || '/api'

async function request(path, options) {
  const res = await fetch(`${API_BASE}${path}`, options)
  if (!res.ok) {
    let detail = res.statusText
    try {
      const data = await res.json()
      detail = data.detail ?? detail
    } catch {
      // keep the status text
    }
    throw new Error(typeof detail === 'string' ? detail : JSON.stringify(detail))
  }
  return res.json()
}

export function getHealth() {
  return request('/health')
}

export function captionImage(file, { model, maxLen } = {}) {
  const form = new FormData()
  form.append('file', file)
  const params = new URLSearchParams()
  if (model) params.set('model', model)
  if (maxLen) params.set('max_len', String(maxLen))
  const query = params.toString() ? `?${params}` : ''
  return request(`/caption${query}`, { method: 'POST', body: form })
}

export function analyzePrescription(file) {
  const form = new FormData()
  form.append('file', file)
  return request('/prescription', { method: 'POST', body: form })
}

// ---- Product Label Interpreter -------------------------------------------
export function interpretProductLabel({ file, productName } = {}) {
  const form = new FormData()
  form.append('image', file)
  if (productName?.trim()) form.append('product_name', productName.trim())
  return request('/product-label/interpret', { method: 'POST', body: form })
}

export function getProductExplanation(sessionId, { signal } = {}) {
  return request(`/product-label/${encodeURIComponent(sessionId)}/explanation`, { signal })
}


// ---- Plant identification (PlantNet) -------------------------------------
/** 1-5 photos of the same plant; ``organ`` = auto | leaf | flower | fruit | bark. */
export function identifyPlant(files, organ = 'auto') {
  const form = new FormData()
  files.forEach((f) => form.append('images', f))
  form.append('organ', organ)
  return request('/plant/identify', { method: 'POST', body: form })
}

/** Medicinal uses written by the AI from the plant's name only (no photo); ``lang`` = en | hi | kn. */
export function getPlantUses(scientificName, commonNames = [], { lang = 'en', signal } = {}) {
  return request('/plant/uses', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ scientific_name: scientificName, common_names: commonNames, lang }),
    signal,
  })
}