Spaces:
Sleeping
Sleeping
Download src/components/messageInputContainer.tsx from ratalai/chatvrm_for_desh: direct link, hf CLI and curl.
- Browser
- Download file 3.41 kB
-
https://huggingface.co/spaces/ratalai/chatvrm_for_desh/resolve/main/src/components/messageInputContainer.tsx
- Command line
-
hf download hf://spaces/ratalai/chatvrm_for_desh/src/components/messageInputContainer.tsx
-
curl -L -o messageInputContainer.tsx https://huggingface.co/spaces/ratalai/chatvrm_for_desh/resolve/main/src/components/messageInputContainer.tsx
3.41 kB
| import { MessageInput } from "@/components/messageInput"; | |
| import { useState, useEffect, useCallback } from "react"; | |
| type Props = { | |
| isChatProcessing: boolean; | |
| onChatProcessStart: (text: string) => void; | |
| onTestVoice: (text: string) => void; | |
| selectVoiceLanguage: string; | |
| }; | |
| /** | |
| * テキスト入力と音声入力を提供する | |
| * | |
| * 音声認識の完了時は自動で送信し、返答文の生成中は入力を無効化する | |
| * | |
| */ | |
| export const MessageInputContainer = ({ | |
| isChatProcessing, | |
| onChatProcessStart, | |
| onTestVoice, | |
| selectVoiceLanguage | |
| }: Props) => { | |
| const [userMessage, setUserMessage] = useState(""); | |
| const [speechRecognition, setSpeechRecognition] = | |
| useState<SpeechRecognition>(); | |
| const [isMicRecording, setIsMicRecording] = useState(false); | |
| const [lang, setLang] = useState("ja-JP"); | |
| // 音声認識の結果を処理する | |
| const handleRecognitionResult = useCallback( | |
| (event: SpeechRecognitionEvent) => { | |
| const text = event.results[0][0].transcript; | |
| setUserMessage(text); | |
| // 発言の終了時 | |
| if (event.results[0].isFinal) { | |
| setUserMessage(text); | |
| // 返答文の生成を開始 | |
| onChatProcessStart(text); | |
| } | |
| }, | |
| [onChatProcessStart] | |
| ); | |
| // 無音が続いた場合も終了する | |
| const handleRecognitionEnd = useCallback(() => { | |
| setIsMicRecording(false); | |
| }, []); | |
| const handleClickMicButton = useCallback(() => { | |
| if (isMicRecording) { | |
| speechRecognition?.abort(); | |
| setIsMicRecording(false); | |
| return; | |
| } | |
| speechRecognition?.start(); | |
| setIsMicRecording(true); | |
| }, [isMicRecording, speechRecognition]); | |
| const handleClickSendButton = useCallback(() => { | |
| onChatProcessStart(userMessage); | |
| }, [onChatProcessStart, userMessage]); | |
| const handleClickTestVoiceButton = useCallback(() => { | |
| onTestVoice(userMessage); | |
| }, [onTestVoice, userMessage]); | |
| const handleClickLangButton = useCallback(() => { | |
| setLang((prevLang) => (prevLang === "ja-JP" ? "zh-TW" : "ja-JP")); | |
| }, []); | |
| useEffect(() => { | |
| const SpeechRecognition = | |
| window.webkitSpeechRecognition || window.SpeechRecognition; | |
| // FirefoxなどSpeechRecognition非対応環境対策 | |
| if (!SpeechRecognition) { | |
| return; | |
| } | |
| const recognition = new SpeechRecognition(); | |
| recognition.lang = selectVoiceLanguage; | |
| //recognition.lang = "ja-JP"; | |
| //recognition.lang = "zh-TW"; | |
| //recognition.lang = lang; | |
| recognition.interimResults = true; // 認識の途中結果を返す | |
| recognition.continuous = false; // 発言の終了時に認識を終了する | |
| recognition.addEventListener("result", handleRecognitionResult); | |
| recognition.addEventListener("end", handleRecognitionEnd); | |
| setSpeechRecognition(recognition); | |
| }, [handleRecognitionResult, handleRecognitionEnd, selectVoiceLanguage]); | |
| useEffect(() => { | |
| if (!isChatProcessing) { | |
| setUserMessage(""); | |
| } | |
| }, [isChatProcessing]); | |
| return ( | |
| <MessageInput | |
| userMessage={userMessage} | |
| isChatProcessing={isChatProcessing} | |
| isMicRecording={isMicRecording} | |
| onChangeUserMessage={(e) => setUserMessage(e.target.value)} | |
| onClickMicButton={handleClickMicButton} | |
| onClickSendButton={handleClickSendButton} | |
| onClickTestVoiceButton={handleClickTestVoiceButton} | |
| onClickLangButton={handleClickLangButton} | |
| /> | |
| ); | |
| }; | |