File size: 7,862 Bytes
d3a18f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ASR Transcription App</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #011227;
            color: #333;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            box-sizing: border-box;
        }
        .container {
            max-width: 650px;
            width: 100%;
            margin: auto;
            background: linear-gradient(135deg, #ffffff, #f0f8ff);
            padding: 40px;
            border-radius: 12px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.1);
            border: 1px solid #d0e0f0;
        }
        h1 {
            text-align: center;
            color: #0056b3;
            margin-bottom: 30px;
            font-size: 2em;
        }
        .form-group {
            margin-bottom: 25px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: #555;
        }
        input[type="file"] {
            display: block;
            width: 100%;
            padding: 12px;
            border: 1px solid #a7d0e0;
            border-radius: 6px;
            box-sizing: border-box;
            background-color: #fcfdff;
            cursor: pointer;
        }
        input[type="file"]::-webkit-file-upload-button {
            background-color: #007bff;
            color: white;
            padding: 8px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-right: 15px;
            transition: background-color 0.2s ease;
        }
        input[type="file"]::-webkit-file-upload-button:hover {
            background-color: #0056b3;
        }
        button {
            background-color: #28a745;
            color: white;
            padding: 15px 25px;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 1.1em;
            width: 100%;
            transition: background-color 0.2s ease, transform 0.1s ease;
        }
        button:hover {
            background-color: #218838;
            transform: translateY(-2px);
        }
        button:disabled {
            background-color: #cccccc;
            cursor: not-allowed;
        }
        #loading {
            text-align: center;
            margin-top: 30px;
            font-weight: bold;
            color: #007bff;
            font-size: 1.1em;
            display: none; /* Hidden by default */
        }
        #response-card {
            margin-top: 30px;
            padding: 20px;
            background-color: #f8fafd;
            border: 1px solid #d0e0f0;
            border-radius: 8px;
            min-height: 80px;
            box-shadow: inset 0 1px 3px rgba(0,0,0,0.05);
        }
        #response-card strong {
            color: #0056b3;
            display: block;
            margin-bottom: 10px;
            font-size: 1.1em;
        }
        #transcriptionOutput {
            white-space: pre-wrap; /* Preserve whitespace and line breaks */
            word-wrap: break-word; /* Break long words */
            font-size: 1.05em;
            color: #333;
        }
        .error {
            color: #dc3545;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Audio Transcription</h1>
        <form id="uploadForm">
            <div class="form-group">
                <label for="audioFile">Select an audio or video file:</label>
                <input type="file" id="audioFile" name="file" accept="audio/*,video/*">
            </div>
            <button type="submit" id="submitButton">Transcribe Audio</button>
        </form>

        <div id="loading">Processing... Please wait, this might take a moment.</div>

        <div id="response-card">
            <strong>Transcription Output:</strong>
            <span id="transcriptionOutput"></span>
        </div>
    </div>

    <script>
        const uploadForm = document.getElementById('uploadForm');
        const audioFile = document.getElementById('audioFile');
        const loadingDiv = document.getElementById('loading');
        const transcriptionOutput = document.getElementById('transcriptionOutput');
        const submitButton = document.getElementById('submitButton');

        uploadForm.addEventListener('submit', async (event) => {
            event.preventDefault(); // Prevent default form submission

            transcriptionOutput.textContent = ''; // Clear previous output
            transcriptionOutput.classList.remove('error'); // Remove error styling
            loadingDiv.style.display = 'block'; // Show loading text
            submitButton.disabled = true; // Disable button during processing

            const file = audioFile.files[0];
            if (!file) {
                transcriptionOutput.textContent = 'Please select an audio or video file.';
                transcriptionOutput.classList.add('error');
                loadingDiv.style.display = 'none';
                submitButton.disabled = false;
                return;
            }

            const formData = new FormData();
            formData.append('file', file); // 'file' must match the parameter name in your FastAPI endpoint

            try {
                // Use a relative path to the API endpoint
                const response = await fetch('/transcribefile/', {
                    method: 'POST',
                    body: formData,
                    // fetch will automatically set the 'Content-Type' header correctly for FormData
                });

                if (response.ok) { // Check if HTTP status is 2xx (e.g., 200 OK)
                    const data = await response.json();
                    transcriptionOutput.textContent = data.rnnt_transcription || 'No transcription found.';
                } else {
                    // Handle API errors (e.g., 400 Bad Request, 500 Internal Server Error)
                    let errorMessage = `Error: ${response.status} - ${response.statusText}`;
                    try {
                        const errorData = await response.json(); // FastAPI often returns JSON for errors
                        if (errorData.detail) {
                            errorMessage = `Error: ${response.status} - ${errorData.detail}`;
                        } else {
                             errorMessage = `Error: ${response.status} - ${JSON.stringify(errorData)}`;
                        }
                    } catch (e) {
                        // If response is not JSON, use raw text
                        const rawText = await response.text();
                        errorMessage = `Error: ${response.status} - ${rawText.substring(0, 200)}...`; // Limit length
                    }
                    transcriptionOutput.textContent = errorMessage;
                    transcriptionOutput.classList.add('error');
                    console.error('API Error:', errorMessage);
                }
            } catch (error) {
                // Handle network errors (e.g., server unreachable)
                transcriptionOutput.textContent = `Network error: ${error.message}. Please check your connection or try again.`;
                transcriptionOutput.classList.add('error');
                console.error('Fetch error:', error);
            } finally {
                loadingDiv.style.display = 'none'; // Hide loading text
                submitButton.disabled = false; // Re-enable button
            }
        });
    </script>
</body>
</html>