-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparagraph_tts.html
158 lines (155 loc) · 8.02 KB
/
paragraph_tts.html
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
{% extends "mainfront/base.html" %}
{% block content %}
<div class="row justify-content-center mt-3">
<div class="col-lg-6 col-md-8 col-sm-10">
<div class="card mb-2">
<div class="card-body" >
<div class="page">
<div class="container py-5">
<div class="row">
<div class="col">
<h2 class="card-title text-center mb-4">Listen carfully and write down what you hear:</h2>
<div id="paragraphForTTS" style="display: none;">this is a test paragraph</div>
<div class="mt-4 mb-4 d-flex justify-content-center">
<img src="static/img/audio.png" class="img-fluid w-100 rounded" alt="Img" style="max-width: 140px;">
</div>
<div class="mt-4 mb-4 d-flex justify-content-center">
<button id="playAudioParag" class="btn btn-secondary btn-lg btn-block mr-4 " style="margin: 3px;">🎧</button>
<button id="playAudioParagSlow" class="btn btn-secondary btn-lg btn-block ml-4" style="margin: 3px;">🐢</button>
</div>
<div class="mt-4 mb-4 d-flex justify-content-center">
<textarea id="userParagraphInput" class="form-control mt-4" rows="5" placeholder="Type the paragraph you hear here..."></textarea>
</div>
<div class="mt-4 d-flex justify-content-center">
<button id="checkParagraph" class="btn btn-success">Check Paragraph</button>
</div>
<div>
<div id="feedback" class="mt-3 d-flex align-items-center justify-content-center" style="text-align: center;"></div>
</div>
<div class="mt-3 d-flex justify-content-center">
<button id="nextParagraph" class="btn btn-secondary" style="display: none;" onclick="nextParagraph()">Next Paragraph</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
function readAloudParag(playbackRate = 1) {
let audioElement = document.getElementById('dynamicAudioParag');
if (!audioElement) {
audioElement = document.createElement('audio');
audioElement.id = 'dynamicAudioParag';
document.body.appendChild(audioElement);
}
var currentTime = new Date().getTime();
audioElement.src = './static/audio/ttsparag.mp3?_=' + currentTime;
audioElement.load();
audioElement.playbackRate = playbackRate;
audioElement.play().catch(error => console.error('Error playing the audio file:', error));
}
document.getElementById('playAudioParag').addEventListener('click', function() { readAloudParag(1); });
document.getElementById('playAudioParagSlow').addEventListener('click', function() { readAloudParag(0.6); });
let incorrectAttempts = 0;
function generateParagraphForTTS() {
incorrectAttempts = 0;
document.getElementById('checkParagraph').disabled = true;
document.getElementById('userParagraphInput').value = '';
updateFeedback('', '');
document.getElementById('playAudioParag').disabled = true;
document.getElementById('playAudioParagSlow').disabled = true;
fetch('/generate_paragraph_for_tts')
.then(response => response.json())
.then(data => {
if (data.paragraph) {
document.getElementById('paragraphForTTS').innerText = data.paragraph;
document.getElementById('checkParagraph').disabled = false;
document.getElementById('nextParagraph').style.display = 'none';
fetch('/tts_for_parag', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({paragraph: data.paragraph}),
})
.then(ttsResponse => {
if (!ttsResponse.ok) {
throw new Error('TTS Network response was not ok');
}
return ttsResponse.json();
})
.then(ttsData => {
document.getElementById('playAudioParag').disabled = false;
document.getElementById('playAudioParagSlow').disabled = false;
})
.catch(ttsError => {
console.error('Error occurred during TTS fetch:', ttsError);
});
} else {
console.error('Failed to generate paragraph');
updateFeedback('Failed to generate paragraph.', 'danger');
}
})
.catch(error => {
console.error('Error occurred during fetch:', error);
updateFeedback('Error fetching paragraph.', 'danger');
});
document.getElementById('checkParagraph').onclick = checkParagraph;
}
function checkParagraph() {
const generatedParagraph = document.getElementById('paragraphForTTS').innerText.toLowerCase();
const userParagraph = document.getElementById('userParagraphInput').value.toLowerCase();
const normalizedGeneratedWords = generatedParagraph.replace(/[^a-z\sáàâäãåæçéèêëíìîïðñóòôöõøùúûüýÿ]/g, '').trim().split(/\s+/);
const normalizedUserWords = userParagraph.replace(/[^a-z\sáàâäãåæçéèêëíìîïðñóòôöõøùúûüýÿ]/g, '').trim().split(/\s+/);
const incorrectWords = normalizedUserWords.filter(word => !normalizedGeneratedWords.includes(word));
const excessWords = normalizedUserWords.length > normalizedGeneratedWords.length;
const insufficientWords = normalizedUserWords.length < normalizedGeneratedWords.length;
if (!excessWords && !insufficientWords && incorrectWords.length === 0) {
updateFeedback('Correct!', 'success');
document.getElementById('nextParagraph').style.display = 'block';
document.getElementById('checkParagraph').disabled = true;
updatePoints();
} else {
let feedbackMessage = '';
if (excessWords) {
feedbackMessage += `Too many words. Expected ${normalizedGeneratedWords.length} words.`;
} else if (insufficientWords) {
feedbackMessage += `Not enough words. Expected ${normalizedGeneratedWords.length} words. `;
} else if (incorrectAttempts < 2) {
incorrectAttempts += 1;
feedbackMessage += `Incorrect words count: ${incorrectWords.length}. Attempts left: ${3 - incorrectAttempts}. `;
} else {
const incorrectWordsString = incorrectWords.join(', ');
feedbackMessage += `Incorrect, wrong words: ${incorrectWordsString}. The correct sentence was: "${document.getElementById('paragraphForTTS').innerText}". `;
document.getElementById('nextParagraph').style.display = 'block';
document.getElementById('checkParagraph').disabled = true;
}
updateFeedback(`${feedbackMessage}Try again.`, 'danger');
}
}
function nextParagraph() {
generateParagraphForTTS();
}
function updateFeedback(message, type) {
const feedbackElement = document.getElementById('feedback');
feedbackElement.innerText = message;
feedbackElement.classList.remove('text-success', 'text-danger');
if (type) {
feedbackElement.classList.add(`text-${type}`);
}
}
document.addEventListener('DOMContentLoaded', function() {
generateParagraphForTTS();
document.getElementById('userParagraphInput').addEventListener('input', function() {
const userInput = this.value;
this.value = userInput.replace(/\d+/g, '');
});
});
</script>
{% endblock %}
{% block scripts %}
<script src="{{ url_for('static', filename='js/ourcode.js') }}" defer></script>
{% endblock %}