gaur3009 commited on
Commit
53470c2
·
verified ·
1 Parent(s): e231cdb

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +80 -41
index.html CHANGED
@@ -92,12 +92,70 @@
92
  </footer>
93
 
94
  <script>
95
- const API_URL = "https://gaur3009-speech-grammar.hf.space//predict";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
- async function checkGrammar() {
98
- const text = document.getElementById("speechInput").value.trim();
99
- const audioFile = document.getElementById("audioInput").files[0];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
 
 
 
101
  const resultBox = document.getElementById("resultBox");
102
  const baselineText = document.getElementById("baselineText");
103
  const scoreText = document.getElementById("scoreText");
@@ -109,50 +167,31 @@
109
  improvedText.innerText = "";
110
 
111
  try {
112
- let response;
113
-
114
- if (audioFile) {
115
- // FormData for file + text input
116
- const formData = new FormData();
117
- formData.append("data", audioFile); // audio file
118
- formData.append("data", ""); // empty string for text
119
-
120
- response = await fetch(API_URL, {
121
- method: "POST",
122
- body: formData
123
- });
124
  } else {
125
- // JSON body when no file
126
- response = await fetch(API_URL, {
127
- method: "POST",
128
- headers: {
129
- "Content-Type": "application/json"
130
- },
131
- body: JSON.stringify({
132
- data: [null, text]
133
- })
134
- });
135
  }
136
 
137
- if (!response.ok) {
138
- throw new Error("Network response was not OK");
139
- }
140
-
141
- const json = await response.json();
142
-
143
- if (!json || !json.data || json.data.length < 3) {
144
- throw new Error("Unexpected API response format");
145
- }
146
 
147
- const [baseline, score, improved] = json.data;
 
148
 
149
- baselineText.innerText = baseline;
150
  scoreText.innerText = `${score}%`;
151
- improvedText.innerText = improved;
152
 
153
- } catch (err) {
154
- baselineText.innerText = "❌ Error calling API.";
155
- console.error("API call error:", err);
156
  }
157
  }
158
  </script>
 
92
  </footer>
93
 
94
  <script>
95
+ // Transcribe speech from microphone
96
+ function transcribeSpeech() {
97
+ return new Promise((resolve, reject) => {
98
+ if (!('webkitSpeechRecognition' in window)) {
99
+ reject('Speech recognition not supported in this browser.');
100
+ return;
101
+ }
102
+ const recognition = new webkitSpeechRecognition();
103
+ recognition.lang = 'en-US';
104
+ recognition.interimResults = false;
105
+ recognition.maxAlternatives = 1;
106
+
107
+ recognition.onresult = (event) => {
108
+ resolve(event.results[0][0].transcript);
109
+ };
110
+ recognition.onerror = (event) => reject(event.error);
111
+
112
+ recognition.start();
113
+ });
114
+ }
115
 
116
+ // Call LanguageTool API for grammar check
117
+ async function checkGrammarWithLanguageTool(text) {
118
+ const url = 'https://api.languagetool.org/v2/check';
119
+ const params = new URLSearchParams();
120
+ params.append('text', text);
121
+ params.append('language', 'en-US');
122
+
123
+ const response = await fetch(url, {
124
+ method: 'POST',
125
+ body: params,
126
+ headers: {
127
+ 'Content-Type': 'application/x-www-form-urlencoded',
128
+ },
129
+ });
130
+ return await response.json();
131
+ }
132
+
133
+ // Parse LanguageTool response to build corrected text & score
134
+ function parseLanguageToolResult(text, result) {
135
+ let correctedText = text;
136
+ let offsetShift = 0;
137
+ const matches = result.matches || [];
138
+
139
+ matches.forEach((match) => {
140
+ if (match.replacements && match.replacements.length > 0) {
141
+ const replacement = match.replacements[0].value;
142
+ const start = match.offset + offsetShift;
143
+ const end = start + match.length;
144
+
145
+ correctedText =
146
+ correctedText.slice(0, start) + replacement + correctedText.slice(end);
147
+
148
+ offsetShift += replacement.length - match.length;
149
+ }
150
+ });
151
+
152
+ const score = 100 - Math.min(100, matches.length * 10); // crude score
153
+ return { correctedText, score };
154
+ }
155
 
156
+ async function checkGrammar() {
157
+ const textInput = document.getElementById("speechInput").value.trim();
158
+ const audioInput = document.getElementById("audioInput").files[0];
159
  const resultBox = document.getElementById("resultBox");
160
  const baselineText = document.getElementById("baselineText");
161
  const scoreText = document.getElementById("scoreText");
 
167
  improvedText.innerText = "";
168
 
169
  try {
170
+ let text;
171
+
172
+ if (audioInput) {
173
+ // Client-side audio file -> speech recognition is complex without server
174
+ // Instead, offer user to use microphone directly:
175
+ text = await transcribeSpeech();
176
+ document.getElementById("speechInput").value = text;
177
+ } else if (textInput) {
178
+ text = textInput;
 
 
 
179
  } else {
180
+ baselineText.innerText = "Please enter text or upload audio.";
181
+ return;
 
 
 
 
 
 
 
 
182
  }
183
 
184
+ baselineText.innerText = text;
 
 
 
 
 
 
 
 
185
 
186
+ const result = await checkGrammarWithLanguageTool(text);
187
+ const { correctedText, score } = parseLanguageToolResult(text, result);
188
 
 
189
  scoreText.innerText = `${score}%`;
190
+ improvedText.innerText = correctedText;
191
 
192
+ } catch (error) {
193
+ baselineText.innerText = "❌ Error processing input.";
194
+ console.error(error);
195
  }
196
  }
197
  </script>