File size: 7,559 Bytes
804fd64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Audio Transcription with Grammar and Vocabulary Check</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
    <style>
        body {
            padding: 20px;
            background: linear-gradient(135deg, #6d7be5, #8699ee);
            font-family: 'Arial', sans-serif;
            color: #333;
        }

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

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

        #transcription-result, #grammar-feedback, #vocabulary-feedback {
            padding: 10px;
            background-color: rgba(255, 255, 255, 0.8);
            border-radius: 4px;
            margin-bottom: 20px;
            min-height: 50px;
        }

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

        .btn {
            margin-top: 10px;
            transition: background-color 0.3s, transform 0.2s;
        }

        .btn:hover {
            transform: scale(1.05);
        }

        .form-select,
        input[type="text"] {
            padding: 10px;
            transition: border-color 0.3s;
        }

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

        footer {
            margin-top: 50px;
            text-align: center;
            font-size: 14px;
            color: #fff;
        }

        .recording-container {
            margin-top: 20px;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1 style="text-align: center; font-weight: bold; color: #000;">Audio Transcription with Grammar and Vocabulary Check</h1>

        <!-- Language Selection -->
        <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>

        <!-- Recording Controls -->
        <h2>Record Audio</h2>
        <button id="start-recording" class="btn btn-primary">Start Recording</button>
        <button id="stop-recording" class="btn btn-danger" disabled>Stop Recording</button>
        <div id="recording-status"></div>

        <div class="recording-container">
            <div id="recording-bar" class="recording-bar"></div>
        </div>

        <!-- Transcription and Feedback Sections -->
        <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 btn-secondary" disabled>Check Grammar and Vocabulary</button>
        </div>
    </div>

    <!-- Footer Section -->
    <footer>
        <p>Made with ❤️ by usmanyousaaf ✨</p>
        <p>Feel free to open issues, contribute, and share your feedback! 🌟 Let's build something great together! 💻👩‍💻👨‍💻</p>
    </footer>

    <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>