import React, { useState, useEffect, useRef } from "react"; import SpeechRecognition, {…
- User
import React, { useState, useEffect, useRef } from "react"; import SpeechRecognition, { useSpeechRecognition, } from "react-speech-recognition"; import { Mic, MicOff, Sun, Moon, PauseCircle, PlayCircle } from "lucide-react";
function App() { const [isListening, setIsListening] = useState(false); const [aiResponse, setAiResponse] = useState(""); const { transcript, resetTranscript, listening } = useSpeechRecognition(); const [isDarkMode, setIsDarkMode] = useState(false); const [isPaused, setIsPaused] = 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 (isListening) { SpeechRecognition.stopListening(); handleSend(); } else { SpeechRecognition.startListening({ continuous: true, language: "es-ES", }); } setIsListening(!isListening); };
const togglePause = () => { if (isPaused) { window.speechSynthesis.resume(); } else { window.speechSynthesis.pause(); } setIsPaused(!isPaused); };
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.onend = () => { setIsPaused(false); }; speech.onerror = () => { setIsPaused(false); }; window.speechSynthesis.speak(speech); }}, [aiResponse, selectedVoice]);
const handleSend = async () => { if (transcript.trim() === "") { return; }
setAiResponse("Procesando..."); try { const response = await fetch( "https://api.groq.com/openai/v1/chat/completions", { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${GROQ_API_KEY}`, }, body: JSON.stringify({ messages: [{ role: "user", content: transcript }], model: "llama-3.2-90b-vision-preview", }), } ); if (!response.ok) { const errorData = await response.json(); console.error(`Error de generación de respuesta: `, errorData); throw new Error( `Error de generación de respuesta: ${response.status} - ${ errorData.message || response.statusText }` ); } const responseData = await response.json(); const respuestaAI = responseData.choices[0].message.content; setAiResponse(respuestaAI); } catch (error) { console.error("Error al comunicarse con la API de Groq:", error); setAiResponse("Lo siento, hubo un error al procesar tu solicitud."); alert(`Error: ${error.message}`); } resetTranscript(); - Assistant