Spaces:
Running
on
A100
Running
on
A100
File size: 7,542 Bytes
ae238b3 |
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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
// API service for transcription functionality
// Common API error handling and utilities
class ApiError extends Error {
constructor(message: string, public status?: number, public isServerBusy: boolean = false) {
super(message);
this.name = 'ApiError';
}
}
const getServerUrl = (): string => import.meta.env.VITE_SERVER_URL || "";
// Centralized fetch wrapper with consistent error handling
const fetchApi = async (
endpoint: string,
options: RequestInit = {},
expectJson: boolean = true
): Promise<any> => {
const response = await fetch(endpoint, options);
if (!response.ok) {
let errorMessage = `HTTP error! status: ${response.status}`;
let isServerBusy = false;
// Try to extract error details from response
try {
const errorData = await response.json();
errorMessage = errorData?.error || errorMessage;
if (response.status === 503) {
isServerBusy = true;
errorMessage = `Server busy: ${errorData?.error || 'Server is currently processing another request'}`;
}
} catch {
// If JSON parsing fails, use default error message
if (response.status === 503) {
isServerBusy = true;
errorMessage = 'Server busy: Server is currently processing another request';
}
}
throw new ApiError(errorMessage, response.status, isServerBusy);
}
if (expectJson) {
return response.json();
}
return response;
};
// Create form data helper
const createFormData = (data: Record<string, string | File | Blob>): FormData => {
const formData = new FormData();
Object.entries(data).forEach(([key, value]) => {
formData.append(key, value);
});
return formData;
};
export interface AlignedSegment {
duration: number;
end: number;
start: number;
text: string;
chunk_index?: number;
speech_segment_index?: number;
// Merge history to allow intelligent splitting
mergedFrom?: AlignedSegment[];
mergeThreshold?: number; // The threshold used when this merge was created
}
export interface ChunkInfo {
chunk_index: number;
start_time: number;
end_time: number;
duration: number;
num_segments: number;
transcription: string;
}
export interface PreprocessedAudio {
data: string; // base64 encoded audio data
format: string; // "wav"
sample_rate: number;
duration: number;
size_bytes: number;
}
export interface TranscriptionResponse {
aligned_segments: AlignedSegment[];
alignment_available?: boolean;
device?: string;
model: string;
num_segments: number;
status: string;
total_duration: number;
transcription: string;
// Long-form specific fields
chunks?: ChunkInfo[];
num_chunks?: number;
// Preprocessed audio data
preprocessed_audio?: PreprocessedAudio;
}
export interface ServerStatus {
is_busy: boolean;
current_operation?: string;
current_filename?: string;
progress?: number;
duration_seconds?: number;
total_completed: number;
}
export interface HealthResponse {
status: string;
message: string;
version: string;
service: string;
device: string;
cuda_available: boolean;
ffmpeg_available: boolean;
transcription_status: ServerStatus;
gpu_count?: number;
current_device?: number;
gpu_name?: string;
gpu_memory_allocated_mb?: number;
gpu_memory_reserved_mb?: number;
gpu_memory_total_mb?: number;
gpu_memory_free_mb?: number;
}
// Main transcription API function
export const transcribeAudio = async (
file: File,
languageCode?: string | null,
scriptCode?: string | null,
onVideoProcessing?: (isProcessing: boolean) => void
): Promise<TranscriptionResponse> => {
// Determine if this is a video file for UI feedback
const isVideoFile = file.type.startsWith("video/");
if (isVideoFile) {
onVideoProcessing?.(true);
console.log("Processing video file on server side:", file.name);
}
try {
// Create form data with unified 'media' field
const formData = createFormData({
media: file // Single 'media' parameter for all file types
});
// Combine language and script codes for server if both are specified
if (languageCode && scriptCode) {
const combinedLanguage = `${languageCode}_${scriptCode}`;
formData.append("language", combinedLanguage);
}
// Request preprocessed audio for waveform generation
formData.append("include_preprocessed", "true");
console.log('transcribeAudio - About to make API call with formData:', {
fileName: file.name,
fileType: file.type,
fileSize: file.size,
hasLanguage: !!languageCode && !!scriptCode,
combinedLanguage: languageCode && scriptCode ? `${languageCode}_${scriptCode}` : null
});
// Debug: Check if the File object is still valid
if (file instanceof File) {
console.log('transcribeAudio - File is valid File object');
// Try to read a small portion to ensure it's accessible
try {
const slice = file.slice(0, 100);
const arrayBuffer = await slice.arrayBuffer();
console.log('transcribeAudio - File slice readable, first 100 bytes length:', arrayBuffer.byteLength);
} catch (error) {
console.error('transcribeAudio - File slice read failed:', error);
}
} else {
console.error('transcribeAudio - File is not a valid File object:', file);
}
const result = await fetchApi(`${getServerUrl()}/transcribe`, {
method: "POST",
body: formData,
});
if (result.status !== "success") {
throw new Error("Transcription failed");
}
return result;
} finally {
if (isVideoFile) {
onVideoProcessing?.(false);
}
}
};
// Server status API functions
export const getServerStatus = async (): Promise<ServerStatus> => {
return fetchApi(`${getServerUrl()}/status`);
};
export const getServerHealth = async (): Promise<HealthResponse> => {
return fetchApi(`${getServerUrl()}/health`);
};
// Video + Subtitles combination API function
export const combineVideoWithSubtitles = async (
videoFile: File,
subtitleContent: string,
language: string = 'eng',
format: 'srt' | 'webvtt' = 'srt',
outputFormat: 'mp4' | 'mkv' = 'mp4'
): Promise<Blob> => {
const formData = createFormData({
video: videoFile,
subtitles: subtitleContent,
format,
output_format: outputFormat,
language
});
const response = await fetchApi(
`${getServerUrl()}/combine-video-subtitles`,
{ method: 'POST', body: formData },
false // Don't expect JSON, expect blob
);
return response.blob();
};
// Cache for supported languages
let supportedLanguagesCache: string[] | null = null;
let supportedLanguagesPromise: Promise<string[]> | null = null;
// Get supported languages with caching
export const getSupportedLanguages = async (): Promise<string[]> => {
// Return from cache if available
if (supportedLanguagesCache) {
return supportedLanguagesCache;
}
// Return existing promise if already in flight
if (supportedLanguagesPromise) {
return supportedLanguagesPromise;
}
// Create new promise and cache it
supportedLanguagesPromise = (async () => {
try {
const response = await fetchApi(`${getServerUrl()}/supported-languages`);
const languages = response.supported_languages;
// Cache the result
supportedLanguagesCache = languages;
return languages;
} catch (error) {
// Reset promise on error so we can retry
supportedLanguagesPromise = null;
throw error;
}
})();
return supportedLanguagesPromise;
};
|