File size: 6,209 Bytes
0fc56a3
 
 
2773b8c
0fc56a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2773b8c
0fc56a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2773b8c
 
 
 
0fc56a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2773b8c
0fc56a3
8fa9f8c
0fc56a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2773b8c
0fc56a3
e40ce0a
0fc56a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e40ce0a
 
0fc56a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e40ce0a
8fa9f8c
0fc56a3
 
 
8fa9f8c
0fc56a3
 
 
e40ce0a
0fc56a3
e40ce0a
0fc56a3
 
 
2773b8c
0fc56a3
 
 
 
 
 
 
 
 
 
2773b8c
0fc56a3
 
 
 
 
 
 
 
 
 
 
 
2773b8c
 
8fa9f8c
 
e40ce0a
8fa9f8c
e40ce0a
8fa9f8c
 
 
 
 
 
 
 
 
e40ce0a
8fa9f8c
0fc56a3
 
 
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<!DOCTYPE html>
<html>
<head>
  <title>ASR</title>
  <!-- Add Bootstrap CSS link here -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.3.0/css/bootstrap.min.css">
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      font-family: Arial, sans-serif;
      background-color: #f8f9fa;
    }

    #header {
      background-color: #101725;
      color: #fff;
      padding: 20px;
      text-align: center;
    }

    #title {
      font-size: 24px;
      margin-bottom: 10px;
    }

    #subtitle {
      font-size: 20px;
      font-weight: 500;
    }

    .container {
      max-width: 600px;
      margin: 20px auto;
      background-color: #fff;
      border-radius: 8px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
      padding: 20px;
    }

    .form-group {
      margin-bottom: 20px;
    }

    .btn {
      width: 100%;
      padding: 10px;
      font-size: 18px;
      font-weight: bold;
      border-radius: 5px;
      cursor: pointer;
    }

    #outputText {
      width: 100%;
      min-height: 100px;
      padding: 10px;
      font-size: 16px;
      border-radius: 5px;
      resize: vertical;
    }

    #playButton {
        color: #fff;
    }
    
  </style>
</head>
<body>
  
  <div id="header">
    <h1 id="title">Automatic Speech Recognition</h1>
    <p id="subtitle">Group 27</p>
  </div>

<div class="container">
  <div class="form-group">
    <label for="audioUpload">Upload Audio File:</label>
    <input type="file" class="form-control" id="audioUpload">
  </div>

  <div class="form-group">
    <label for="languageSelect">Select Language:</label>
    <select class="form-control" id="languageSelect">
      <option value="en">English</option>
    </select>
  </div>

  <div class="form-group">
    <button class="btn btn-danger" id="recordButton">Start Recording</button>
    <button class="btn btn-secondary" id="submitButton">Transcribe</button>
    <button class="btn btn-info" id="playButton">Play Audio</button>
  </div>

  <div class="form-group">
    <textarea class="form-control" id="outputText" rows="5" readonly style="display: none;"></textarea>
  </div>
</div>

  <!-- Add Bootstrap JS link and any other required scripts here -->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.3.0/js/bootstrap.min.js"></script>
  <script>
    let isRecording = false;
    let recordedChunks = [];
    let uploadedFile;
    let recordedAudioBlob;
    let mediaRecorder;

    const recordButton = document.getElementById("recordButton");
    const submitButton = document.getElementById("submitButton");
    const languageSelect = document.getElementById("languageSelect");
    const playButton = document.getElementById("playButton");
    const outputText = document.getElementById("outputText");
    let lastModifiedType = null;

    recordButton.addEventListener("click", function () {
      if (!isRecording) {
        navigator.mediaDevices.getUserMedia({ audio: true }).then(function (stream) {
          mediaRecorder = new MediaRecorder(stream);
          recordedChunks = [];

          mediaRecorder.ondataavailable = function (e) {
            recordedChunks.push(e.data);
          };

          mediaRecorder.onstop = function () {
            recordedAudioBlob = new Blob(recordedChunks, { type: "audio/wav" });
          };

          mediaRecorder.start();
          isRecording = true;
          recordButton.innerText = "Stop Recording";
        }).catch(function (err) {
          console.error("Error accessing microphone:", err);
        });
      } else {
        mediaRecorder.stop();
        console.log('Audio recorded')
        lastModifiedType = "recorded";
        isRecording = false;
        recordButton.innerText = "Start Recording";
      }
    });

    const audioUpload = document.getElementById("audioUpload");

    audioUpload.addEventListener("change", function (event) {
      uploadedFile = event.target.files[0];
      if (uploadedFile) {
        const fileReader = new FileReader();
        fileReader.onload = function () {
          uploadedArrayBuffer = fileReader.result;
        };
        fileReader.readAsArrayBuffer(uploadedFile);
      }
      lastModifiedType = "uploaded";
      console.log('File uploaded')
    });

    submitButton.addEventListener("click", function () {
      outputText.value = "Transcribed Text : "
      if (recordedAudioBlob || uploadedFile) {
        const formData = new FormData();

        if (lastModifiedType === "recorded" && recordedAudioBlob) {
          formData.append("audio", recordedAudioBlob, "recorded_audio.wav");
        } else if (lastModifiedType === "uploaded" && uploadedFile) {
          formData.append("audio", uploadedFile, uploadedFile.name);
        }

        outputText.style.display="block";

        formData.append("language", languageSelect.value);

        fetch("/process_audio", {
          method: "POST",
          body: formData,
        })
          .then((response) => response.json())
          .then((data) => {
            if (data.success) {
              outputText.value = "Transcribed Text : " + data.language;
            } else {
              outputText.value = data.message;
            }
          })
          .catch((error) => {
            console.error("Error communicating with the backend:", error);
            outputText.value = "Backend communication failed.";
          });
      } else {
        outputText.value = "Please record audio or upload a file and select a language before submitting.";
      }
    });

    playButton.addEventListener("click", function () {
    let audioURL;

    if (lastModifiedType === "recorded" && recordedAudioBlob) {
      audioURL = URL.createObjectURL(recordedAudioBlob);
    } else if (lastModifiedType === "uploaded" && uploadedFile) {
      audioURL = URL.createObjectURL(uploadedFile);
    }

    if (audioURL) {
      const audio = new Audio(audioURL);
      audio.play();
    } else {
      outputText.value = "No audio available.";
    }
    });

  </script>
</body>
</html>