Download frontend/src/hooks/useInference.ts from vipan-kumar/Audio_Deepfake_Detection: direct link, hf CLI and curl.
- Browser
- Download file 2.52 kB
-
https://huggingface.co/spaces/vipan-kumar/Audio_Deepfake_Detection/resolve/main/frontend/src/hooks/useInference.ts
- Command line
-
hf download hf://spaces/vipan-kumar/Audio_Deepfake_Detection/frontend/src/hooks/useInference.ts
-
curl -L -o useInference.ts https://huggingface.co/spaces/vipan-kumar/Audio_Deepfake_Detection/resolve/main/frontend/src/hooks/useInference.ts
2.52 kB
| import { useCallback } from "react"; | |
| import { detectAudio, detectSample, visualizeAudio } from "@/lib/api"; | |
| import { useInferenceStore } from "@/store/inferenceStore"; | |
| export function useInference() { | |
| const setIsInferring = useInferenceStore((s) => s.setIsInferring); | |
| const setResult = useInferenceStore((s) => s.setResult); | |
| const setViz = useInferenceStore((s) => s.setViz); | |
| const setError = useInferenceStore((s) => s.setError); | |
| const audio = useInferenceStore((s) => s.audio); | |
| const selectedModels = useInferenceStore((s) => s.selectedModels); | |
| const fileFromAudio = useCallback((): File | null => { | |
| if (!audio) return null; | |
| if (audio.kind === "file") return audio.file; | |
| if (audio.kind === "recording") { | |
| const ext = | |
| audio.blob.type.includes("webm") | |
| ? "webm" | |
| : audio.blob.type.includes("ogg") | |
| ? "ogg" | |
| : "wav"; | |
| return new File([audio.blob], `recording.${ext}`, { type: audio.blob.type }); | |
| } | |
| return null; | |
| }, [audio]); | |
| const run = useCallback(async () => { | |
| if (!audio) return; | |
| setIsInferring(true); | |
| setError(null); | |
| setResult(null); | |
| try { | |
| const models = selectedModels.length ? selectedModels : undefined; | |
| if (audio.kind === "sample") { | |
| const res = await detectSample(audio.sampleId, models, true); | |
| setResult(res); | |
| setViz(null); | |
| return; | |
| } | |
| const file = fileFromAudio(); | |
| if (!file) throw new Error("No audio source available."); | |
| // Run inference and visualization in parallel. Re-throw viz errors via | |
| // store so the Demo page can surface them. | |
| const [det, viz] = await Promise.all([ | |
| detectAudio(file, models, true), | |
| visualizeAudio(file).catch((vizExc) => { | |
| // Don't fail the whole pipeline because viz failed — but log so the | |
| // page can pick it up. The Demo page also runs visualize directly. | |
| // eslint-disable-next-line no-console | |
| console.warn("visualize failed during inference:", vizExc); | |
| return null; | |
| }), | |
| ]); | |
| setResult(det); | |
| if (viz) setViz(viz); | |
| } catch (e: unknown) { | |
| const msg = | |
| e && typeof e === "object" && "message" in e | |
| ? String((e as { message: string }).message) | |
| : "Inference failed"; | |
| setError(msg); | |
| } finally { | |
| setIsInferring(false); | |
| } | |
| }, [audio, selectedModels, setError, setIsInferring, setResult, setViz, fileFromAudio]); | |
| return { run }; | |
| } | |