File size: 7,534 Bytes
e24409d
 
 
 
 
2e2bb25
e24409d
 
7918102
 
2e2bb25
7918102
e24409d
2e2bb25
 
 
 
 
 
 
 
 
 
e24409d
2e2bb25
7918102
 
 
e24409d
2e2bb25
 
 
 
 
 
7918102
2e2bb25
 
 
 
 
7918102
e24409d
2e2bb25
 
e24409d
2e2bb25
e24409d
7918102
2e2bb25
7918102
2e2bb25
7918102
 
e24409d
2e2bb25
e24409d
2e2bb25
e24409d
2e2bb25
 
e24409d
2e2bb25
 
e24409d
2e2bb25
7918102
2e2bb25
 
e24409d
2e2bb25
e24409d
 
 
2e2bb25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e24409d
 
 
 
 
2e2bb25
 
e24409d
 
 
 
 
 
 
 
 
 
 
 
 
 
7918102
 
e24409d
2e2bb25
 
 
 
e24409d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7918102
e24409d
 
 
 
 
 
2e2bb25
e24409d
 
 
 
 
 
 
2e2bb25
e24409d
2e2bb25
 
 
 
 
 
e24409d
2e2bb25
 
 
e24409d
 
 
 
 
 
2e2bb25
 
e24409d
 
 
 
 
 
 
 
 
 
 
 
 
2e2bb25
 
 
 
 
e24409d
 
 
 
2e2bb25
 
e24409d
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Live Grammar and Vocabulary Test</title>
    <style>
        body {
            background-color: black;
            color: white;
            font-family: 'Arial', sans-serif;
            padding: 20px;
        }

        h1 {
            text-align: center;
            font-weight: bold;
            margin-bottom: 40px;
        }

        h2, h3 {
            margin-top: 20px;
            margin-bottom: 10px;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
        }

        .recording-bar {
            width: 0;
            height: 5px;
            background-color: #4caf50;
            transition: width 0.5s;
        }

        #transcription-result, #grammar-feedback, #vocabulary-feedback {
            background-color: white;
            color: black;
            padding: 15px;
            border: 1px solid lightgray;
            border-radius: 4px;
            margin-bottom: 20px;
            min-height: 50px;
        }

        .btn {
            background-color: green;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-top: 10px;
            transition: background-color 0.3s, transform 0.2s;
        }

        .btn:hover {
            transform: scale(1.05);
            background-color: darkgreen;
        }

        .form-select, input[type="text"] {
            padding: 10px;
            border: 1px solid lightgray;
            border-radius: 4px;
            margin-bottom: 20px;
            width: 100%;
        }

        #recording-status {
            margin-top: 10px;
            font-weight: bold;
            color: #4caf50;
        }

        .recording-container {
            margin-top: 10px;
            width: 100%;
            height: 10px;
            background-color: lightgray;
            border-radius: 5px;
        }

        .feedback-section {
            margin-top: 40px;
        }

        .feedback-section p {
            margin: 5px 0;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Live Grammar and Vocabulary Test</h1>
        <div class="mb-3">
            <label for="language-select" class="form-label">Select Language:</label>
            <select id="language-select" class="form-select">
                <option value="en">English</option>
                <option value="es">Spanish</option>
                <option value="fr">French</option>
                <option value="de">German</option>
                <option value="zh">Chinese</option>
                <option value="ja">Japanese</option>
                <option value="ko">Korean</option>
                <option value="ar">Arabic</option>
            </select>
        </div>

        <h2>Record Audio</h2>
        <button id="start-recording" class="btn">Start Recording</button>
        <button id="stop-recording" class="btn" disabled>Stop Recording</button>
        <div id="recording-status"></div>
        
        <div class="recording-container">
            <div id="recording-bar" class="recording-bar"></div>
        </div>

        <div class="feedback-section">
            <h3>Transcription Result:</h3>
            <div id="transcription-result"></div>
        </div>

        <div class="feedback-section">
            <h3>Grammar Feedback:</h3>
            <div id="grammar-feedback"></div>
            <p><strong>Grammar Score:</strong> <span id="grammar-score"></span>/10</p>

            <h3>Vocabulary Feedback:</h3>
            <div id="vocabulary-feedback"></div>
            <p><strong>Vocabulary Score:</strong> <span id="vocabulary-score"></span>/10</p>

            <button id="check-grammar" class="btn" disabled>Check Grammar and Vocabulary</button>
        </div>
    </div>

    <script>
        let mediaRecorder;
        let audioChunks = [];

        document.getElementById("start-recording").onclick = async () => {
            const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
            mediaRecorder = new MediaRecorder(stream);
            mediaRecorder.start();
            document.getElementById("recording-status").textContent = "Recording...";
            document.getElementById("start-recording").disabled = true;
            document.getElementById("stop-recording").disabled = false;
            document.getElementById("recording-bar").style.width = '0%';
            let recordingInterval = setInterval(() => {
                const currentWidth = parseFloat(document.getElementById("recording-bar").style.width);
                const newWidth = Math.min(currentWidth + 5, 100);
                document.getElementById("recording-bar").style.width = newWidth + '%';
                if (newWidth >= 100) {
                    clearInterval(recordingInterval);
                }
            }, 1000);
            mediaRecorder.ondataavailable = (event) => {
                audioChunks.push(event.data);
            };
            mediaRecorder.onstop = async () => {
                clearInterval(recordingInterval);
                document.getElementById("recording-bar").style.width = '0%';
                const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
                const formData = new FormData();
                formData.append('audio_data', audioBlob, 'audio.wav');
                const selectedLanguage = document.getElementById("language-select").value;
                formData.append('language', selectedLanguage);
                const response = await fetch('/transcribe', {
                    method: 'POST',
                    body: formData
                });
                const result = await response.json();
                document.getElementById("transcription-result").textContent = result.transcription;
                document.getElementById("check-grammar").disabled = false;
                audioChunks = [];
                document.getElementById("recording-status").textContent = "Recording stopped.";
                document.getElementById("start-recording").disabled = false;
                document.getElementById("stop-recording").disabled = true;
            };
        };

        document.getElementById("stop-recording").onclick = () => {
            mediaRecorder.stop();
        };

        document.getElementById("check-grammar").onclick = async () => {
            const transcription = document.getElementById("transcription-result").textContent;
            const formData = new FormData();
            formData.append('transcription', transcription);
            const selectedLanguage = document.getElementById("language-select").value;
            formData.append('language', selectedLanguage);
            const response = await fetch('/check_grammar', {
                method: 'POST',
                body: formData
            });
            const result = await response.json();
            document.getElementById("grammar-feedback").textContent = result.grammar_feedback;
            document.getElementById("vocabulary-feedback").textContent = result.vocabulary_feedback;
            document.getElementById("grammar-score").textContent = result.grammar_score;
            document.getElementById("vocabulary-score").textContent = result.vocabulary_score;
        };
    </script>
</body>
</html>