|
1 |
| -import React from 'react'; |
| 1 | +import React, { useState, useRef } from 'react'; |
2 | 2 | import Navbar from './Navbar';
|
3 | 3 | import Lottie from 'lottie-react';
|
4 | 4 | import animationData from '../assets/134013-student-with-laptop.json';
|
5 | 5 | import animationData1 from '../assets/39701-robot-bot-3d.json';
|
6 | 6 |
|
7 | 7 | const Tutor = () => {
|
| 8 | + const [inputText, setInputText] = useState(''); |
| 9 | + const inputImageRef = useRef(null); |
| 10 | + const inputVoiceRef = useRef(null); |
| 11 | + const recognitionRef = useRef(null); |
| 12 | + const [isListening, setIsListening] = useState(false); |
| 13 | + const [outputText, setOutputText] = useState(''); |
| 14 | + const [outputSound, setOutputSound] = useState(null); |
| 15 | + const [outputImage, setOutputImage] = useState(null); |
| 16 | + |
8 | 17 | const avatarNarration = 'Hello! I am Luna, your tutor. How can I assist you today?';
|
9 | 18 |
|
10 | 19 | const handleReply = () => {
|
11 | 20 | // Handle the reply button functionality here
|
| 21 | + console.log('Input Text:', inputText); |
| 22 | + console.log('Input Image:', inputImageRef.current.files[0]); |
| 23 | + console.log('Input Voice:', inputVoiceRef.current.files[0]); |
| 24 | + // You can perform further actions based on the input and update the output state variables accordingly. |
| 25 | + }; |
| 26 | + |
| 27 | + const handleTextChange = (event) => { |
| 28 | + setInputText(event.target.value); |
| 29 | + }; |
| 30 | + |
| 31 | + const handleImageChange = (event) => { |
| 32 | + const file = event.target.files[0]; |
| 33 | + inputImageRef.current.value = ''; // Reset the input file value for re-selecting the same file |
| 34 | + setOutputImage(URL.createObjectURL(file)); |
| 35 | + }; |
| 36 | + |
| 37 | + const handleVoiceChange = () => { |
| 38 | + if (isListening) { |
| 39 | + recognitionRef.current.stop(); |
| 40 | + setIsListening(false); |
| 41 | + } else { |
| 42 | + const recognition = new window.SpeechRecognition(); |
| 43 | + recognition.continuous = true; |
| 44 | + recognition.interimResults = false; |
| 45 | + |
| 46 | + recognition.onstart = () => { |
| 47 | + setIsListening(true); |
| 48 | + }; |
| 49 | + |
| 50 | + recognition.onresult = (event) => { |
| 51 | + const transcript = Array.from(event.results) |
| 52 | + .map((result) => result[0]) |
| 53 | + .map((result) => result.transcript) |
| 54 | + .join(''); |
| 55 | + |
| 56 | + setInputText(transcript); |
| 57 | + }; |
| 58 | + |
| 59 | + recognition.onerror = (event) => { |
| 60 | + console.error('Recognition error:', event.error); |
| 61 | + setIsListening(false); |
| 62 | + }; |
| 63 | + |
| 64 | + recognition.onend = () => { |
| 65 | + setIsListening(false); |
| 66 | + }; |
| 67 | + |
| 68 | + recognition.start(); |
| 69 | + setIsListening(true); |
| 70 | + recognitionRef.current = recognition; |
| 71 | + } |
12 | 72 | };
|
13 | 73 |
|
14 | 74 | return (
|
@@ -41,18 +101,52 @@ const Tutor = () => {
|
41 | 101 | type="text"
|
42 | 102 | className="w-full mr-4 bg-white rounded-lg py-2 px-3 focus:outline-none"
|
43 | 103 | placeholder="Enter your message"
|
| 104 | + value={inputText} |
| 105 | + onChange={handleTextChange} |
| 106 | + /> |
| 107 | + <input |
| 108 | + type="file" |
| 109 | + accept="image/*" |
| 110 | + className="hidden" |
| 111 | + onChange={handleImageChange} |
| 112 | + ref={inputImageRef} |
44 | 113 | />
|
45 | 114 | <button
|
46 | 115 | className="bg-blue-500 text-white rounded-lg py-2 px-4 hover:bg-blue-600 transition-colors"
|
47 | 116 | onClick={handleReply}
|
48 | 117 | >
|
49 | 118 | Reply
|
50 | 119 | </button>
|
| 120 | + <button |
| 121 | + className={`bg-blue-500 text-white rounded-lg py-2 px-4 hover:bg-blue-600 transition-colors ml-2 ${ |
| 122 | + isListening ? 'bg-red-500' : '' |
| 123 | + }`} |
| 124 | + onClick={handleVoiceChange} |
| 125 | + > |
| 126 | + {isListening ? 'Stop Listening' : 'Add Voice'} |
| 127 | + </button> |
| 128 | + <button |
| 129 | + className="bg-blue-500 text-white rounded-lg py-2 px-4 hover:bg-blue-600 transition-colors ml-2" |
| 130 | + onClick={() => inputImageRef.current.click()} |
| 131 | + > |
| 132 | + Add Image |
| 133 | + </button> |
51 | 134 | </div>
|
52 | 135 | </div>
|
53 | 136 | </div>
|
54 | 137 | </div>
|
55 | 138 | </div>
|
| 139 | + <div className="fixed bottom-4 right-4"> |
| 140 | + <div className="w-64 bg-white rounded-lg shadow-lg p-4"> |
| 141 | + {outputText && <p className="mb-2">{outputText}</p>} |
| 142 | + {outputSound && ( |
| 143 | + <audio controls> |
| 144 | + <source src={outputSound} type="audio/mpeg" /> |
| 145 | + </audio> |
| 146 | + )} |
| 147 | + {outputImage && <img src={outputImage} alt="Output" className="w-full" />} |
| 148 | + </div> |
| 149 | + </div> |
56 | 150 | </div>
|
57 | 151 | </div>
|
58 | 152 | );
|
|
0 commit comments