mira, tengo esto: import React, { useState, useEffect, useRef } from "react"; import…
- User
mira, tengo esto:
import React, { useState, useEffect, useRef } from "react"; import SpeechRecognition, { useSpeechRecognition, } from "react-speech-recognition"; import { Mic, MicOff, Sun, Moon, PauseCircle, PlayCircle, Loader2, Power, } from "lucide-react";
function App() { const [aiResponse, setAiResponse] = useState(""); const { transcript, resetTranscript, listening } = useSpeechRecognition(); const [isDarkMode, setIsDarkMode] = useState(false); const [isPaused, setIsPaused] = useState(false); const [isProcessing, setIsProcessing] = useState(false); const [isSpeaking, setIsSpeaking] = useState(false); const speechRef = useRef(null); const [selectedVoice, setSelectedVoice] = useState(null);
const GROQ_API_KEY = "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";
useEffect(() => { if (!SpeechRecognition.browserSupportsSpeechRecognition()) { alert("Lo siento, tu navegador no soporta reconocimiento de voz."); } }, []);
const toggleDarkMode = () => { setIsDarkMode(!isDarkMode); };
useEffect(() => { if (isDarkMode) { document.documentElement.classList.add("dark"); } else { document.documentElement.classList.remove("dark"); } }, [isDarkMode]);
const toggleListening = () => { if (listening) { SpeechRecognition.stopListening(); handleSend(); } else { SpeechRecognition.startListening({ continuous: true, language: "es-ES", }); } };
const togglePause = () => { if (isSpeaking) { if (isPaused) { window.speechSynthesis.resume(); setIsPaused(false); } else { window.speechSynthesis.pause(); setIsPaused(true); } } };
useEffect(() => { const loadVoices = () => { const voices = window.speechSynthesis.getVoices(); const spanishVoices = voices.filter( (voice) => voice.lang === "es-ES" || voice.lang === "es-MX" ); const preferredVoice = spanishVoices.find((voice) => voice.name.includes("Google")) || spanishVoices.find((voice) => voice.name.includes("Microsoft")) || spanishVoices[0]; setSelectedVoice(preferredVoice); };
if (window.speechSynthesis.onvoiceschanged !== undefined) { window.speechSynthesis.onvoiceschanged = loadVoices; } loadVoices();}, []);
useEffect(() => { if (aiResponse && selectedVoice) { window.speechSynthesis.cancel();
const speech = new SpeechSynthesisUtterance(aiResponse); speech.lang = selectedVoice.lang; speech.voice = selectedVoice; speech.pitch = 1.0; speech.rate = 1.0; speechRef.current = speech; speech.onstart = () => { setIsSpeaking(true); setIsPaused(false); }; speech.onend = () => { setIsSpeaking(false); setIsPaused(false); SpeechRecognition.startListening({ continuous: true, language: "es-ES", }); }; speech.onerror = () => { setIsSpeaking(false); setIsPaused(false); }; window.speechSynthesis.speak(speech); }}, [aiResponse, selectedVoice]);
const handleSend = async () => { if (transcript.trim() === "") { return; }
- Assistant