Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
import { useState, useRef, useEffect } from "react"; | |
// Simulation time in milliseconds for pre-calculated documents | |
const SIMULATION_DURATION = 120000; // 2 minutes | |
// Starting messages with their timing | |
const STARTING_MESSAGES = [ | |
{ message: "Initializing evaluation environment", step: 1, totalSteps: 5 }, | |
{ message: "Finding available model providers", step: 2, totalSteps: 5 }, | |
{ message: "Starting evaluation process", step: 3, totalSteps: 5 }, | |
{ message: "Evaluating models", step: 4, totalSteps: 5 }, | |
{ message: "Storing evaluation results", step: 5, totalSteps: 5 }, | |
]; | |
export const useSimulation = (onComplete) => { | |
const [startingMessageIndex, setStartingMessageIndex] = useState(0); | |
const [evaluationComplete, setEvaluationComplete] = useState(false); | |
const simulationTimeoutRef = useRef(null); | |
const startingMessageIntervalRef = useRef(null); | |
useEffect(() => { | |
// Configure automatic interval for message changes | |
startingMessageIntervalRef.current = setInterval(() => { | |
setStartingMessageIndex((prev) => { | |
if (prev < STARTING_MESSAGES.length - 1) { | |
return prev + 1; | |
} | |
return prev; | |
}); | |
}, SIMULATION_DURATION / STARTING_MESSAGES.length); | |
// Complete after simulation duration | |
simulationTimeoutRef.current = setTimeout(() => { | |
setEvaluationComplete(true); | |
if (startingMessageIntervalRef.current) { | |
clearInterval(startingMessageIntervalRef.current); | |
} | |
setStartingMessageIndex(STARTING_MESSAGES.length - 1); | |
if (onComplete) { | |
onComplete(); | |
} | |
}, SIMULATION_DURATION); | |
return () => { | |
if (simulationTimeoutRef.current) { | |
clearTimeout(simulationTimeoutRef.current); | |
} | |
if (startingMessageIntervalRef.current) { | |
clearInterval(startingMessageIntervalRef.current); | |
} | |
}; | |
}, [onComplete]); | |
return { | |
startingMessageIndex, | |
evaluationComplete, | |
currentMessage: STARTING_MESSAGES[startingMessageIndex], | |
}; | |
}; | |