Spaces:
Sleeping
Sleeping
File size: 1,416 Bytes
c02c6ce | 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 | import { useState } from 'react'
export function useDetect() {
const [loading, setLoading] = useState(false)
const [error, setError] = useState(null)
const [results, setResults] = useState(null)
async function detect(imageFile, options = {}) {
setLoading(true)
setError(null)
setResults(null)
const {
useLlm = true,
topK = 5,
enableOcr = false,
confidenceThreshold = 0.25,
} = options
const params = new URLSearchParams({
use_llm: useLlm,
top_k: topK,
enable_ocr: enableOcr,
confidence_threshold: confidenceThreshold,
})
const formData = new FormData()
formData.append('image', imageFile)
const apiBase = import.meta.env.VITE_API_BASE_URL ?? '/api'
try {
const response = await fetch(`${apiBase}/detect-and-recommend?${params}`, {
method: 'POST',
body: formData,
})
if (!response.ok) {
const errBody = await response.text()
throw new Error(`API error ${response.status}: ${errBody}`)
}
const data = await response.json()
setResults(data)
return data
} catch (err) {
setError(err.message || 'Something went wrong')
throw err
} finally {
setLoading(false)
}
}
function reset() {
setResults(null)
setError(null)
setLoading(false)
}
return { detect, loading, error, results, reset }
}
|