Last push
Browse files10 unmodified lines1112131415161718192019 unmodified lines4041424344454647484950515256 unmodified lines10911011111211311411511610510610710810911011111211311711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616795 unmodified lines26326426521922026626726826927027127222227327427527627727827910 unmodified lines const [result, setResult] = useState(null); const [selectedFrameIndex, setSelectedFrameIndex] = useState(null); const [isModalClosing, setIsModalClosing] = useState(false); const [progress, setProgress] = useState(0); const [progressLabel, setProgressLabel] = useState(""); const fileInputRef = useRef(null); const processingIntervalRef = useRef(null);
const isValidSize = (file) => file && file.size <= MAX_FILE_BYTES;
19 unmodified lines } }, [videoFile]);
useEffect(() => () => { if (processingIntervalRef.current) { clearInterval(processingIntervalRef.current); processingIntervalRef.current = null; } }, []);
const onDrop = (e) => { e.preventDefault(); e.stopPropagation();56 unmodified lines return; } setLoading(true); setProgress(5); setProgressLabel("Preparing upload"); try { const formData = new FormData(); formData.append("video", videoFile); // Request 20 frames to match preprocessing, using same-origin backend const url = "/api/infer?frame_rate=5&max_frames=20"; const res = await fetch(url, { method: "POST", body: formData }); if (!res.ok) { const text = await res.text(); throw new Error(text || `Request failed with ${res.status}`); } const json = await res.json(); setResult(json); const data = await new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); // Request 20 frames to match preprocessing, using same-origin backend xhr.open("POST", "/api/infer?frame_rate=5&max_frames=20"); xhr.responseType = "json"; xhr.upload.onprogress = (event) => { if (event.lengthComputable) { const percent = Math.round((event.loaded / event.total) * 70); setProgress(Math.max(10, Math.min(70, percent))); setProgressLabel("Uploading video"); } else { setProgress((prev) => (prev < 60 ? prev + 3 : prev)); } }; xhr.upload.onload = () => { setProgress((prev) => Math.max(prev, 75)); setProgressLabel("Analyzing frames"); if (processingIntervalRef.current) clearInterval(processingIntervalRef.current); processingIntervalRef.current = setInterval(() => { setProgress((prev) => (prev < 95 ? prev + 2 : prev)); }, 400); }; xhr.onerror = () => reject(new Error("Network error")); xhr.onload = () => { if (processingIntervalRef.current) { clearInterval(processingIntervalRef.current); processingIntervalRef.current = null; } if (xhr.status >= 200 && xhr.status < 300) { const payload = xhr.response ?? JSON.parse(xhr.responseText || "{}"); resolve(payload); } else { reject(new Error(xhr.responseText || `Request failed with ${xhr.status}`)); } }; xhr.send(formData); }); setProgress(100); setProgressLabel("Completed"); setResult(data); } catch (err) { setError(err.message || "Upload failed"); } finally { setLoading(false); setTimeout(() => { setProgress(0); setProgressLabel(""); }, 1200); } };
95 unmodified lines )} {loading ? "Analyzing..." : "Analyze video"} </button> {loading && ( <div className="flex-1 min-w-[140px] max-w-xs"> {(loading || progress > 0) && ( <div className="flex-1 min-w-[180px] max-w-sm"> <div className="flex items-center justify-between text-[10px] uppercase tracking-wide text-emerald-300/80 mb-1"> <span>{progressLabel || (loading ? "Analyzing" : "")}</span> {progress > 0 && <span>{progress}%</span>} </div> <div className="h-1.5 w-full bg-slate-800 rounded-full overflow-hidden"> <div className="h-full w-1/2 bg-gradient-to-r from-emerald-400 via-cyan-400 to-emerald-500 animate-pulse" /> <div className="h-full bg-gradient-to-r from-emerald-400 via-cyan-400 to-emerald-500 transition-all duration-200" style={{ width: `${Math.min(100, Math.max(progress, loading ? 5 : 0))}%` }} /> </div> </div> )}frontend/pages/analyze.jsx+66-12