Spaces:
Edmond98
/
Running on A100

File size: 17,149 Bytes
9575eea
c0c3100
9575eea
bc747e6
09ab406
4b305c9
09ab406
 
 
06e4c74
3cf82c2
 
 
 
1388ad6
73af305
5d16050
 
bc747e6
 
 
73af305
0960663
3cf82c2
 
4b305c9
09ab406
 
00f260b
4d56027
 
 
09ab406
5d16050
 
 
 
 
 
 
09ab406
 
bc747e6
f84bdcd
 
bc747e6
 
 
9575eea
 
 
 
bc747e6
 
 
 
 
 
 
 
3cf82c2
 
0ec2266
4b305c9
3cf82c2
09ab406
 
4b305c9
 
09ab406
9104ce6
4b305c9
 
9575eea
 
 
 
 
73af305
 
 
 
 
 
0960663
 
 
 
 
 
 
 
 
 
 
 
 
73af305
 
 
 
 
 
0960663
73af305
 
 
0960663
73af305
 
0960663
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73af305
 
0ec2266
09ab406
9575eea
521a4ba
4d56027
0ec2266
73af305
0ec2266
aa3c419
 
0ec2266
aa3c419
 
 
0ec2266
4b305c9
 
 
 
 
 
 
521a4ba
 
4d56027
5d16050
 
 
 
 
521a4ba
5d16050
 
521a4ba
5d16050
09ab406
9575eea
c0c3100
 
 
 
 
9575eea
 
 
 
 
 
 
 
 
 
 
 
c0c3100
4b305c9
 
 
 
c0c3100
4b305c9
9575eea
 
 
 
 
 
 
 
 
 
 
 
 
 
09ab406
9575eea
521a4ba
be921fa
4d56027
4b305c9
 
 
 
 
 
1771b70
be921fa
 
 
1771b70
4b305c9
be921fa
 
5d16050
1771b70
4b305c9
5d16050
 
 
 
 
0eaed7a
 
be921fa
5d16050
 
0eaed7a
5d16050
0eaed7a
5d16050
 
 
 
be921fa
 
5d16050
0eaed7a
5d16050
0eaed7a
5d16050
0eaed7a
5d16050
4d56027
0eaed7a
4d56027
5d16050
0eaed7a
bc747e6
 
 
a921671
bc747e6
efc7284
 
 
 
a921671
efc7284
bc747e6
 
311f9e9
 
efc7284
bc747e6
521a4ba
 
5d16050
bc747e6
efc7284
 
 
 
 
be921fa
 
 
521a4ba
be921fa
 
521a4ba
be921fa
4d56027
be921fa
5d16050
 
be921fa
5d16050
 
521a4ba
5d16050
 
521a4ba
5d16050
7bcf8d7
09ab406
9575eea
521a4ba
4d56027
0ec2266
73af305
4d56027
521a4ba
 
4d56027
5d16050
 
 
 
 
521a4ba
5d16050
 
521a4ba
5d16050
09ab406
9575eea
c0c3100
 
 
 
9575eea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0960663
 
9575eea
9104ce6
 
521a4ba
4d56027
4b305c9
 
 
 
 
9104ce6
521a4ba
9104ce6
4d56027
5d16050
 
 
 
 
521a4ba
5d16050
 
521a4ba
5d16050
09ab406
9104ce6
 
521a4ba
4d56027
4b305c9
 
 
 
 
9104ce6
 
 
4d56027
5d16050
 
 
 
 
521a4ba
5d16050
 
521a4ba
9104ce6
c0c3100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import os
from fastapi import FastAPI, HTTPException, File, UploadFile, Depends, Security, Form
from fastapi.security.api_key import APIKeyHeader, APIKey
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from typing import Optional
import numpy as np
import io
import soundfile as sf
import base64
import logging
import torch
import librosa
from pathlib import Path
from pydub import AudioSegment
from moviepy.editor import VideoFileClip
import traceback
from logging.handlers import RotatingFileHandler
import boto3
from botocore.exceptions import NoCredentialsError
import time
import tempfile
import magic

# Import functions from other modules
from asr import transcribe, ASR_LANGUAGES, ASR_SAMPLING_RATE
from tts import synthesize, TTS_LANGUAGES
from lid import identify

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Add a file handler
file_handler = RotatingFileHandler('app.log', maxBytes=10000000, backupCount=5)
file_handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)

app = FastAPI(title="MMS: Scaling Speech Technology to 1000+ languages")

# S3 Configuration
S3_BUCKET = os.environ.get("S3_BUCKET")
S3_REGION = os.environ.get("S3_REGION")
S3_ACCESS_KEY_ID = os.environ.get("AWS_ACCESS_KEY_ID")
S3_SECRET_ACCESS_KEY = os.environ.get("AWS_SECRET_ACCESS_KEY")

# API Key Configuration
API_KEY = os.environ.get("API_KEY")
api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False)

# Initialize S3 client
s3_client = boto3.client(
    's3',
    aws_access_key_id=S3_ACCESS_KEY_ID,
    aws_secret_access_key=S3_SECRET_ACCESS_KEY,
    region_name=S3_REGION
)

# Define request models
class AudioRequest(BaseModel):
    audio: str  # Base64 encoded audio or video data
    language: Optional[str] = None

class TTSRequest(BaseModel):
    text: str
    language: Optional[str] = None
    speed: float = 1.0

class LanguageRequest(BaseModel):
    language: Optional[str] = None

async def get_api_key(api_key_header: str = Security(api_key_header)):
    if api_key_header == API_KEY:
        return api_key_header
    raise HTTPException(status_code=403, detail="Could not validate credentials")

def extract_audio_from_file(input_bytes):
    with tempfile.NamedTemporaryFile(delete=False, suffix='.tmp') as temp_file:
        temp_file.write(input_bytes)
        temp_file_path = temp_file.name

    try:
        # Log file info
        file_info = magic.from_file(temp_file_path, mime=True)
        logger.info(f"Received file of type: {file_info}")

        # Try reading with soundfile first
        try:
            audio_array, sample_rate = sf.read(temp_file_path)
            logger.info(f"Successfully read audio with soundfile. Shape: {audio_array.shape}, Sample rate: {sample_rate}")
            return audio_array, sample_rate
        except Exception as e:
            logger.info(f"Could not read with soundfile: {str(e)}")

        # Try reading as video
        try:
            video = VideoFileClip(temp_file_path)
            audio = video.audio
            if audio is not None:
                audio_array = audio.to_soundarray()
                sample_rate = audio.fps
                audio_array = audio_array.mean(axis=1) if len(audio_array.shape) > 1 and audio_array.shape[1] > 1 else audio_array
                audio_array = audio_array.astype(np.float32)
                audio_array /= np.max(np.abs(audio_array))
                video.close()
                logger.info(f"Successfully extracted audio from video. Shape: {audio_array.shape}, Sample rate: {sample_rate}")
                return audio_array, sample_rate
            else:
                logger.info("Video file contains no audio")
        except Exception as e:
            logger.info(f"Could not read as video: {str(e)}")

        # Try reading with pydub
        try:
            audio = AudioSegment.from_file(temp_file_path)
            audio_array = np.array(audio.get_array_of_samples())
            audio_array = audio_array.astype(np.float32) / (2**15 if audio.sample_width == 2 else 2**7)
            audio_array = audio_array.reshape((-1, 2)).mean(axis=1) if audio.channels == 2 else audio_array
            logger.info(f"Successfully read audio with pydub. Shape: {audio_array.shape}, Sample rate: {audio.frame_rate}")
            return audio_array, audio.frame_rate
        except Exception as e:
            logger.info(f"Could not read with pydub: {str(e)}")

        raise ValueError(f"Unsupported file format: {file_info}")
    finally:
        os.unlink(temp_file_path)

@app.post("/transcribe")
async def transcribe_audio(request: AudioRequest, api_key: APIKey = Depends(get_api_key)):
    start_time = time.time()
    try:
        input_bytes = base64.b64decode(request.audio)
        audio_array, sample_rate = extract_audio_from_file(input_bytes)

        # Ensure audio_array is float32
        audio_array = audio_array.astype(np.float32)

        # Resample if necessary
        if sample_rate != ASR_SAMPLING_RATE:
            audio_array = librosa.resample(audio_array, orig_sr=sample_rate, target_sr=ASR_SAMPLING_RATE)

        if request.language is None:
            # If no language is provided, use language identification
            identified_language = identify(audio_array)
            result = transcribe(audio_array, identified_language)
        else:
            result = transcribe(audio_array, request.language)

        processing_time = time.time() - start_time
        return JSONResponse(content={"transcription": result, "processing_time_seconds": processing_time})
    except Exception as e:
        logger.error(f"Error in transcribe_audio: {str(e)}", exc_info=True)
        error_details = {
            "error": str(e),
            "traceback": traceback.format_exc()
        }
        processing_time = time.time() - start_time
        return JSONResponse(
            status_code=500,
            content={"message": "An error occurred during transcription", "details": error_details, "processing_time_seconds": processing_time}
        )

@app.post("/transcribe_file")
async def transcribe_audio_file(
    file: UploadFile = File(...),
    language: Optional[str] = Form(None),
    api_key: APIKey = Depends(get_api_key)
):
    start_time = time.time()
    try:
        contents = await file.read()
        audio_array, sample_rate = extract_audio_from_file(contents)

        # Ensure audio_array is float32
        audio_array = audio_array.astype(np.float32)

        # Resample if necessary
        if sample_rate != ASR_SAMPLING_RATE:
            audio_array = librosa.resample(audio_array, orig_sr=sample_rate, target_sr=ASR_SAMPLING_RATE)

        if language is None:
            # If no language is provided, use language identification
            identified_language = identify(audio_array)
            result = transcribe(audio_array, identified_language)
        else:
            result = transcribe(audio_array, language)

        processing_time = time.time() - start_time
        return JSONResponse(content={"transcription": result, "processing_time_seconds": processing_time})
    except Exception as e:
        logger.error(f"Error in transcribe_audio_file: {str(e)}", exc_info=True)
        error_details = {
            "error": str(e),
            "traceback": traceback.format_exc()
        }
        processing_time = time.time() - start_time
        return JSONResponse(
            status_code=500,
            content={"message": "An error occurred during transcription", "details": error_details, "processing_time_seconds": processing_time}
        )

@app.post("/synthesize")
async def synthesize_speech(request: TTSRequest, api_key: APIKey = Depends(get_api_key)):
    start_time = time.time()
    logger.info(f"Synthesize request received: text='{request.text}', language='{request.language}', speed={request.speed}")
    try:
        if request.language is None:
            # If no language is provided, default to English
            lang_code = "eng"
        else:
            # Extract the ISO code from the full language name
            lang_code = request.language.split()[0].strip()
        
        # Input validation
        if not request.text:
            raise ValueError("Text cannot be empty")
        if lang_code not in TTS_LANGUAGES:
            raise ValueError(f"Unsupported language: {lang_code}")
        if not 0.5 <= request.speed <= 2.0:
            raise ValueError(f"Speed must be between 0.5 and 2.0, got {request.speed}")
        
        logger.info(f"Calling synthesize function with lang_code: {lang_code}")
        result, filtered_text = synthesize(request.text, lang_code, request.speed)
        logger.info(f"Synthesize function completed. Filtered text: '{filtered_text}'")
        
        if result is None:
            logger.error("Synthesize function returned None")
            raise ValueError("Synthesis failed to produce audio")
        
        sample_rate, audio = result
        logger.info(f"Synthesis result: sample_rate={sample_rate}, audio_shape={audio.shape if isinstance(audio, np.ndarray) else 'not numpy array'}, audio_dtype={audio.dtype if isinstance(audio, np.ndarray) else type(audio)}")
        
        logger.info("Converting audio to numpy array")
        audio = np.array(audio, dtype=np.float32)
        logger.info(f"Converted audio shape: {audio.shape}, dtype: {audio.dtype}")
        
        logger.info("Normalizing audio")
        max_value = np.max(np.abs(audio))
        if max_value == 0:
            logger.warning("Audio array is all zeros")
            raise ValueError("Generated audio is silent (all zeros)")
        audio = audio / max_value
        logger.info(f"Normalized audio range: [{audio.min()}, {audio.max()}]")
        
        logger.info("Converting to int16")
        audio = (audio * 32767).astype(np.int16)
        logger.info(f"Int16 audio shape: {audio.shape}, dtype: {audio.dtype}")
        
        logger.info("Writing audio to buffer")
        buffer = io.BytesIO()
        sf.write(buffer, audio, sample_rate, format='wav')
        buffer.seek(0)
        logger.info(f"Buffer size: {buffer.getbuffer().nbytes} bytes")
        
        # Generate a unique filename
        filename = f"synthesized_audio_{int(time.time())}.wav"
        
        # Upload to S3 without ACL
        try:
            s3_client.upload_fileobj(
                buffer, 
                S3_BUCKET, 
                filename, 
                ExtraArgs={'ContentType': 'audio/wav'}
            )
            logger.info(f"File uploaded successfully to S3: {filename}")
            
            # Generate the public URL with the correct format
            url = f"https://s3.{S3_REGION}.amazonaws.com/{S3_BUCKET}/{filename}"
            logger.info(f"Public URL generated: {url}")
            
            processing_time = time.time() - start_time
            return JSONResponse(content={"audio_url": url, "processing_time_seconds": processing_time})
        
        except NoCredentialsError:
            logger.error("AWS credentials not available or invalid")
            raise HTTPException(status_code=500, detail="Could not upload file to S3: Missing or invalid credentials")
        except Exception as e:
            logger.error(f"Failed to upload to S3: {str(e)}")
            raise HTTPException(status_code=500, detail=f"Could not upload file to S3: {str(e)}")

    except ValueError as ve:
        logger.error(f"ValueError in synthesize_speech: {str(ve)}", exc_info=True)
        processing_time = time.time() - start_time
        return JSONResponse(
            status_code=400,
            content={"message": "Invalid input", "details": str(ve), "processing_time_seconds": processing_time}
        )
    except Exception as e:
        logger.error(f"Unexpected error in synthesize_speech: {str(e)}", exc_info=True)
        error_details = {
            "error": str(e),
            "type": type(e).__name__,
            "traceback": traceback.format_exc()
        }
        processing_time = time.time() - start_time
        return JSONResponse(
            status_code=500,
            content={"message": "An unexpected error occurred during speech synthesis", "details": error_details, "processing_time_seconds": processing_time}
        )

@app.post("/identify")
async def identify_language(request: AudioRequest, api_key: APIKey = Depends(get_api_key)):
    start_time = time.time()
    try:
        input_bytes = base64.b64decode(request.audio)
        audio_array, sample_rate = extract_audio_from_file(input_bytes)
        result = identify(audio_array)
        processing_time = time.time() - start_time
        return JSONResponse(content={"language_identification": result, "processing_time_seconds": processing_time})
    except Exception as e:
        logger.error(f"Error in identify_language: {str(e)}", exc_info=True)
        error_details = {
            "error": str(e),
            "traceback": traceback.format_exc()
        }
        processing_time = time.time() - start_time
        return JSONResponse(
            status_code=500,
            content={"message": "An error occurred during language identification", "details": error_details, "processing_time_seconds": processing_time}
        )

@app.post("/identify_file")
async def identify_language_file(
    file: UploadFile = File(...),
    api_key: APIKey = Depends(get_api_key)
):
    start_time = time.time()
    try:
        contents = await file.read()
        audio_array, sample_rate = extract_audio_from_file(contents)
        result = identify(audio_array)
        processing_time = time.time() - start_time
        return JSONResponse(content={"language_identification": result, "processing_time_seconds": processing_time})
    except Exception as e:
        logger.error(f"Error in identify_language_file: {str(e)}", exc_info=True)
        error_details = {
            "error": str(e),
            "traceback": traceback.format_exc()
        }
        processing_time = time.time() - start_time
        return JSONResponse(
            status_code=500,
            content={"message": "An error occurred during language identification", "details": error_details, "processing_time_seconds": processing_time}
        )

@app.post("/asr_languages")
async def get_asr_languages(request: LanguageRequest, api_key: APIKey = Depends(get_api_key)):
    start_time = time.time()
    try:
        if request.language is None or request.language == "":
            # If no language is provided, return all languages
            matching_languages = ASR_LANGUAGES
        else:
            matching_languages = [lang for lang in ASR_LANGUAGES if lang.lower().startswith(request.language.lower())]
        
        processing_time = time.time() - start_time
        return JSONResponse(content={"languages": matching_languages, "processing_time_seconds": processing_time})
    except Exception as e:
        logger.error(f"Error in get_asr_languages: {str(e)}", exc_info=True)
        error_details = {
            "error": str(e),
            "traceback": traceback.format_exc()
        }
        processing_time = time.time() - start_time
        return JSONResponse(
            status_code=500,
            content={"message": "An error occurred while fetching ASR languages", "details": error_details, "processing_time_seconds": processing_time}
        )

@app.post("/tts_languages")
async def get_tts_languages(request: LanguageRequest, api_key: APIKey = Depends(get_api_key)):
    start_time = time.time()
    try:
        if request.language is None or request.language == "":
            # If no language is provided, return all languages
            matching_languages = TTS_LANGUAGES
        else:
            matching_languages = [lang for lang in TTS_LANGUAGES if lang.lower().startswith(request.language.lower())]
        
        processing_time = time.time() - start_time
        return JSONResponse(content={"languages": matching_languages, "processing_time_seconds": processing_time})
    except Exception as e:
        logger.error(f"Error in get_tts_languages: {str(e)}", exc_info=True)
        error_details = {
            "error": str(e),
            "traceback": traceback.format_exc()
        }
        processing_time = time.time() - start_time
        return JSONResponse(
            status_code=500,
            content={"message": "An error occurred while fetching TTS languages", "details": error_details, "processing_time_seconds": processing_time}
        )

@app.get("/health")
async def health_check():
    return {"status": "ok"}

@app.get("/")
async def root():
    return {
        "message": "Welcome to the MMS Speech Technology API",
        "version": "1.0",
        "endpoints": [
            "/transcribe",
            "/transcribe_file",
            "/synthesize",
            "/identify",
            "/identify_file",
            "/asr_languages",
            "/tts_languages",
            "/health"
        ]
    }