2025-05-16 16:27:50 +07:00
|
|
|
|
// ai-tutor-poc/src/App.jsx
|
|
|
|
|
import React, { useState, useRef, useEffect } from 'react';
|
2025-05-12 21:54:32 +07:00
|
|
|
|
import './App.css';
|
|
|
|
|
|
|
|
|
|
const OPENAI_API_KEY = import.meta.env.VITE_OPENAI_API_KEY;
|
|
|
|
|
|
|
|
|
|
function App() {
|
|
|
|
|
const [transcript, setTranscript] = useState('');
|
|
|
|
|
const [aiReply, setAiReply] = useState('');
|
|
|
|
|
const [isRecording, setIsRecording] = useState(false);
|
|
|
|
|
const mediaRecorderRef = useRef(null);
|
|
|
|
|
const audioChunksRef = useRef([]);
|
2025-05-16 16:27:50 +07:00
|
|
|
|
const audioStreamRef = useRef(null);
|
2025-05-26 15:48:26 +07:00
|
|
|
|
const systemContent = `คุณเป็นครูสอนภาษาอังกฤษที่ใจดี เป็นกันเอง และสอนภาษาอังกฤษผ่านการใช้เรื่องราวหรือบทสนทนาได้เป็นอย่างดีอและสนุกสนาน
|
2025-05-19 02:03:56 +07:00
|
|
|
|
|
2025-05-26 15:48:26 +07:00
|
|
|
|
คุณได้รับมอบหมายให้พูดคุยกับนักเรียนระดับประถมที่เรียนภาษาอังกฤษเป็นภาษาต่างประเทศ (EFL) เพื่อช่วยให้นักเรียนเรียนรู้ภาษาอังกฤษอย่างเป็นธรรมชาติ
|
|
|
|
|
|
|
|
|
|
นักเรียนจะเริ่มต้นบทสนทนาด้วยการทักทายเป็นภาษาไทย จากนั้นคุณจะเชิญชวนนักเรียนให้มาเรียนภาษาอังกฤษด้วยกัน พร้อมอธิบายว่า บทเรียนวันนี้จะเกี่ยวกับหนังที่นักเรียนดูเมื่อวานนี้
|
2025-05-19 02:03:56 +07:00
|
|
|
|
|
2025-05-21 16:59:31 +07:00
|
|
|
|
**พูดทีละประโยคสั้น ๆ และหยุดพูดหลังจากแต่ละประโยคเพื่อรอให้นักเรียนตอบก่อน**
|
|
|
|
|
การสนทนานี้จะเหมือนการพูดคุยแบบปากเปล่า จึงควรพูดด้วยน้ำเสียงที่เป็นธรรมชาติ ให้กำลังใจ และเป็นกันเอง
|
2025-05-19 02:03:56 +07:00
|
|
|
|
|
2025-05-26 15:48:26 +07:00
|
|
|
|
เป้าหมายของบทเรียนวันนี้
|
|
|
|
|
- แนะนำและฝึกคำศัพท์ใหม่ที่เกี่ยวข้องกับภาพยนตร์
|
|
|
|
|
- กระตุ้นให้นักเรียนอธิบายเนื้อเรื่อง ตัวละคร และบทเรียนที่ได้จากหนัง
|
|
|
|
|
- ฝึกการเรียบเรียงประโยคอย่างสมบูรณ์และการแสดงความคิดเห็น
|
2025-05-19 02:03:56 +07:00
|
|
|
|
|
2025-05-21 16:59:31 +07:00
|
|
|
|
ตัวอย่างการเริ่มสนทนา:
|
2025-05-19 02:03:56 +07:00
|
|
|
|
|
2025-05-21 16:59:31 +07:00
|
|
|
|
นักเรียน: สวัสดีค่ะคุณครู
|
|
|
|
|
คุณ: สวัสดีค่ะนักเรียน
|
2025-05-19 02:03:56 +07:00
|
|
|
|
|
2025-05-26 15:48:26 +07:00
|
|
|
|
ตัวอย่างการให้บริบทของหนัง
|
|
|
|
|
"เมื่อวานนี้ หนูได้ดูหนังการ์ตูนเกี่ยวกับเด็กกล้าหาญคนหนึ่งที่ออกเดินทางผจญภัยเพื่อช่วยหมู่บ้านของเขาใช่ไหมคะ?"
|
|
|
|
|
น้ำเสียง ใช้น้ำเสียงที่ให้กำลังใจ อดทน และกระตุ้นให้นักเรียนกล้าใช้ภาษา
|
|
|
|
|
เทคนิค ใช้คำถามปลายเปิด เช่น
|
|
|
|
|
- “Can you tell me about your favorite character in the movie?”
|
|
|
|
|
(เล่าให้ครูฟังหน่อยได้ไหมว่าใครคือตัวละครที่หนูชอบที่สุดในหนัง?)
|
|
|
|
|
- “What was the most exciting part of the movie for you?”
|
|
|
|
|
(ช่วงไหนในหนังที่หนูรู้สึกว่าสนุกที่สุด?
|
|
|
|
|
- “How do you think the hero felt when they achieved their goal?”
|
|
|
|
|
(หนูคิดว่าพอตัวเอกทำสำเร็จ เขารู้สึกยังไงบ้าง?)
|
2025-05-19 02:03:56 +07:00
|
|
|
|
|
2025-05-26 15:48:26 +07:00
|
|
|
|
กิจกรรมเสริม
|
|
|
|
|
- ให้เด็กอธิบายฉากหนึ่งจากหนังโดยใช้คำศัพท์ใหม่
|
|
|
|
|
- กระตุ้นให้เด็กลองแสดงบทพูดสั้นๆ จากหนัง เช่น “Let’s go!” หรือ “We can do it!”
|
2025-05-21 16:59:31 +07:00
|
|
|
|
|
|
|
|
|
จบการสนทนาด้วยประโยคง่าย ๆ เช่น:
|
|
|
|
|
- Great job today!
|
|
|
|
|
- I had fun talking with you.
|
|
|
|
|
- See you again tomorrow!
|
|
|
|
|
|
2025-05-26 15:57:31 +07:00
|
|
|
|
เป้าหมายคือการทำให้เด็กสนุกไปกับบทสนทนาและได้ฝึกใช้ภาษาอังกฤษอย่างมั่นใจ
|
|
|
|
|
|
2025-05-26 16:07:53 +07:00
|
|
|
|
Please give one short reply at a time.
|
|
|
|
|
|
2025-05-26 15:57:31 +07:00
|
|
|
|
Example conversation:
|
|
|
|
|
|
|
|
|
|
Student: สวัสดีค่ะ/ครับคุณครู
|
|
|
|
|
Teacher: Hello! It’s so nice to see you again today. Are you ready to have fun with English?
|
|
|
|
|
Student: *answers*
|
|
|
|
|
Teacher: Great! Today, we will talk about a movie you watched yesterday. Do you remember the movie?
|
|
|
|
|
Student: *gives movie name*
|
|
|
|
|
Teacher: It was an animated movie about a brave young hero who goes on an adventure to save their village. Do you remember? (*adjust according to the movie*)
|
|
|
|
|
|
|
|
|
|
Now proceed to the activities.
|
|
|
|
|
|
2025-05-26 16:17:57 +07:00
|
|
|
|
Activity 1: Vocabulary Time! (1 minute). You'll be helping the student learn the correct pronunciation
|
|
|
|
|
|
|
|
|
|
Teacher: Let’s learn some new words from the movie! Repeat after me
|
|
|
|
|
Teacher: Hero – a brave person who helps others.
|
|
|
|
|
Student will try to pronounce
|
|
|
|
|
|
|
|
|
|
Teacher: Good job! How about Adventure – a fun and exciting journey.
|
|
|
|
|
Student will try to pronounce
|
|
|
|
|
|
|
|
|
|
Teacher: Good job! How about Village – a small town where people live.
|
|
|
|
|
Student will try to pronounce
|
|
|
|
|
|
|
|
|
|
Teacher: Good job! How about Monster – a big scary creature.
|
|
|
|
|
Student will try to pronounce
|
|
|
|
|
|
|
|
|
|
Teacher: Good job! How about Magic – something special and powerful.
|
2025-05-26 15:57:31 +07:00
|
|
|
|
`;
|
2025-05-16 16:27:50 +07:00
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const initRecording = async () => {
|
|
|
|
|
audioStreamRef.current = await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
|
|
|
mediaRecorderRef.current = new MediaRecorder(audioStreamRef.current);
|
|
|
|
|
|
|
|
|
|
mediaRecorderRef.current.ondataavailable = (e) => {
|
|
|
|
|
audioChunksRef.current.push(e.data);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
mediaRecorderRef.current.onstop = async () => {
|
|
|
|
|
const inputAudioBlob = new Blob(audioChunksRef.current, { type: 'audio/webm' });
|
|
|
|
|
audioChunksRef.current = []; // Clear for next recording
|
|
|
|
|
const formData = new FormData();
|
|
|
|
|
formData.append('file', inputAudioBlob, 'input.webm');
|
|
|
|
|
formData.append('model', 'whisper-1');
|
|
|
|
|
|
|
|
|
|
const whisperRes = await fetch('https://api.openai.com/v1/audio/transcriptions', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: `Bearer ${OPENAI_API_KEY}`
|
|
|
|
|
},
|
|
|
|
|
body: formData
|
|
|
|
|
});
|
|
|
|
|
const { text } = await whisperRes.json();
|
|
|
|
|
setTranscript(text);
|
|
|
|
|
|
2025-05-27 14:18:57 +07:00
|
|
|
|
setAiReply(''); // Clear previous reply
|
|
|
|
|
let fullText = '';
|
|
|
|
|
|
|
|
|
|
const response = await fetch('https://api.openai.com/v1/chat/completions', {
|
2025-05-16 16:27:50 +07:00
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Authorization': `Bearer ${OPENAI_API_KEY}`,
|
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
model: 'gpt-4o',
|
2025-05-27 14:18:57 +07:00
|
|
|
|
stream: true,
|
2025-05-16 16:27:50 +07:00
|
|
|
|
messages: [
|
2025-05-19 02:03:56 +07:00
|
|
|
|
{ role: 'system', content: systemContent },
|
2025-05-16 16:27:50 +07:00
|
|
|
|
{ role: 'user', content: text }
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
});
|
2025-05-27 14:18:57 +07:00
|
|
|
|
|
|
|
|
|
const reader = response.body.getReader();
|
|
|
|
|
const decoder = new TextDecoder("utf-8");
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
const { done, value } = await reader.read();
|
|
|
|
|
if (done) break;
|
|
|
|
|
|
|
|
|
|
const chunk = decoder.decode(value);
|
|
|
|
|
const lines = chunk.split('\n').filter(line => line.trim() !== '');
|
|
|
|
|
|
|
|
|
|
for (const line of lines) {
|
|
|
|
|
if (line.startsWith('data: ')) {
|
|
|
|
|
const data = line.replace('data: ', '');
|
|
|
|
|
if (data === '[DONE]') {
|
|
|
|
|
// After full reply received, synthesize speech
|
|
|
|
|
const speechRes = await fetch('https://api.openai.com/v1/audio/speech', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Authorization': `Bearer ${OPENAI_API_KEY}`,
|
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
model: 'tts-1-hd',
|
|
|
|
|
voice: 'nova',
|
|
|
|
|
input: fullText
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
const outputAudioBlob = await speechRes.blob();
|
|
|
|
|
const audioUrl = URL.createObjectURL(outputAudioBlob);
|
|
|
|
|
const audio = new Audio(audioUrl);
|
|
|
|
|
audio.play();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const parsed = JSON.parse(data);
|
|
|
|
|
const delta = parsed.choices?.[0]?.delta?.content;
|
|
|
|
|
if (delta) {
|
|
|
|
|
fullText += delta;
|
|
|
|
|
setAiReply(prev => prev + delta);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-16 16:27:50 +07:00
|
|
|
|
}
|
|
|
|
|
};
|
2025-05-12 21:54:32 +07:00
|
|
|
|
};
|
|
|
|
|
|
2025-05-16 16:27:50 +07:00
|
|
|
|
initRecording();
|
|
|
|
|
}, []);
|
|
|
|
|
|
2025-05-26 16:29:38 +07:00
|
|
|
|
const startRecording = () => {
|
|
|
|
|
if (!isRecording && mediaRecorderRef.current) {
|
|
|
|
|
audioChunksRef.current = [];
|
|
|
|
|
mediaRecorderRef.current.start();
|
|
|
|
|
setIsRecording(true);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const stopRecording = () => {
|
|
|
|
|
if (isRecording && mediaRecorderRef.current) {
|
2025-05-16 16:27:50 +07:00
|
|
|
|
mediaRecorderRef.current.stop();
|
|
|
|
|
setIsRecording(false);
|
2025-05-16 15:21:02 +07:00
|
|
|
|
}
|
2025-05-12 21:54:32 +07:00
|
|
|
|
};
|
|
|
|
|
|
2025-05-26 16:29:38 +07:00
|
|
|
|
|
2025-05-12 21:54:32 +07:00
|
|
|
|
return (
|
2025-05-19 01:41:58 +07:00
|
|
|
|
|
2025-05-16 16:27:50 +07:00
|
|
|
|
<div className="app-container">
|
2025-05-19 01:41:58 +07:00
|
|
|
|
|
|
|
|
|
<div className="page-title">
|
|
|
|
|
<strong>AI English Tutor - Yesterday's movie</strong>
|
|
|
|
|
</div>
|
|
|
|
|
|
2025-05-16 16:27:50 +07:00
|
|
|
|
<div className="scene-wrapper">
|
2025-05-19 01:41:58 +07:00
|
|
|
|
|
|
|
|
|
|
2025-05-16 16:27:50 +07:00
|
|
|
|
<img src="/tutor_f.png" alt="Tutor Avatar" className="avatar" />
|
|
|
|
|
|
|
|
|
|
<div className="dialogue-box">
|
|
|
|
|
<div className="dialogue-text">
|
2025-05-19 01:22:20 +07:00
|
|
|
|
<strong>คุณ:</strong> {transcript || <em>ลองพูดอะไรบางอย่าง</em>}
|
2025-05-16 16:27:50 +07:00
|
|
|
|
</div>
|
|
|
|
|
<div className="dialogue-text">
|
2025-05-19 01:22:20 +07:00
|
|
|
|
<strong>ติวเตอร์:</strong> {aiReply || <em>กำลังรอคำถามของคุณ</em>}
|
2025-05-16 16:27:50 +07:00
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="button-container">
|
|
|
|
|
<button
|
2025-05-26 16:29:38 +07:00
|
|
|
|
onMouseDown={startRecording}
|
|
|
|
|
onMouseUp={stopRecording}
|
|
|
|
|
onMouseLeave={stopRecording} // Ensures it stops if mouse leaves the button
|
|
|
|
|
onTouchStart={startRecording}
|
|
|
|
|
onTouchEnd={stopRecording}
|
2025-05-16 16:27:50 +07:00
|
|
|
|
className={`control-button ${isRecording ? 'recording' : 'idle'}`}
|
|
|
|
|
>
|
2025-05-26 16:29:38 +07:00
|
|
|
|
{isRecording ? 'กำลังพูด...' : 'กดค้างเพื่อพูด'}
|
2025-05-16 16:27:50 +07:00
|
|
|
|
</button>
|
|
|
|
|
</div>
|
2025-05-15 16:50:55 +07:00
|
|
|
|
</div>
|
2025-05-12 21:54:32 +07:00
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-16 16:27:50 +07:00
|
|
|
|
export default App;
|