Afrinetwork7
commited on
Commit
•
bc747e6
1
Parent(s):
fc0645c
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
from fastapi import FastAPI, HTTPException
|
2 |
-
from fastapi.responses import JSONResponse
|
3 |
from pydantic import BaseModel
|
4 |
import numpy as np
|
5 |
import io
|
@@ -13,6 +13,11 @@ import magic # For MIME type detection
|
|
13 |
from pydub import AudioSegment
|
14 |
import traceback
|
15 |
from logging.handlers import RotatingFileHandler
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
# Import functions from other modules
|
18 |
from asr import transcribe, ASR_LANGUAGES
|
@@ -33,6 +38,20 @@ logger.addHandler(file_handler)
|
|
33 |
|
34 |
app = FastAPI(title="MMS: Scaling Speech Technology to 1000+ languages")
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
# Define request models
|
37 |
class AudioRequest(BaseModel):
|
38 |
audio: str # Base64 encoded audio or video data
|
@@ -138,17 +157,28 @@ async def synthesize_speech(request: TTSRequest):
|
|
138 |
buffer.seek(0)
|
139 |
logger.info(f"Buffer size: {buffer.getbuffer().nbytes} bytes")
|
140 |
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
}
|
148 |
-
|
149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
|
151 |
-
|
|
|
|
|
152 |
|
153 |
except ValueError as ve:
|
154 |
logger.error(f"ValueError in synthesize_speech: {str(ve)}", exc_info=True)
|
|
|
1 |
from fastapi import FastAPI, HTTPException
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
from pydantic import BaseModel
|
4 |
import numpy as np
|
5 |
import io
|
|
|
13 |
from pydub import AudioSegment
|
14 |
import traceback
|
15 |
from logging.handlers import RotatingFileHandler
|
16 |
+
import os
|
17 |
+
import boto3
|
18 |
+
from botocore.exceptions import NoCredentialsError
|
19 |
+
from urllib.parse import quote
|
20 |
+
import time
|
21 |
|
22 |
# Import functions from other modules
|
23 |
from asr import transcribe, ASR_LANGUAGES
|
|
|
38 |
|
39 |
app = FastAPI(title="MMS: Scaling Speech Technology to 1000+ languages")
|
40 |
|
41 |
+
# S3 Configuration
|
42 |
+
S3_BUCKET = "afri"
|
43 |
+
S3_REGION = "eu-west-3"
|
44 |
+
S3_ACCESS_KEY_ID = os.environ.get("AWS_ACCESS_KEY_ID")
|
45 |
+
S3_SECRET_ACCESS_KEY = os.environ.get("AWS_SECRET_ACCESS_KEY")
|
46 |
+
|
47 |
+
# Initialize S3 client
|
48 |
+
s3_client = boto3.client(
|
49 |
+
's3',
|
50 |
+
aws_access_key_id=S3_ACCESS_KEY_ID,
|
51 |
+
aws_secret_access_key=S3_SECRET_ACCESS_KEY,
|
52 |
+
region_name=S3_REGION
|
53 |
+
)
|
54 |
+
|
55 |
# Define request models
|
56 |
class AudioRequest(BaseModel):
|
57 |
audio: str # Base64 encoded audio or video data
|
|
|
157 |
buffer.seek(0)
|
158 |
logger.info(f"Buffer size: {buffer.getbuffer().nbytes} bytes")
|
159 |
|
160 |
+
# Generate a unique filename
|
161 |
+
filename = f"synthesized_audio_{int(time.time())}.wav"
|
162 |
+
|
163 |
+
# Upload to S3
|
164 |
+
try:
|
165 |
+
s3_client.upload_fileobj(buffer, S3_BUCKET, filename)
|
166 |
+
logger.info(f"File uploaded successfully to S3: {filename}")
|
167 |
+
|
168 |
+
# Generate a presigned URL
|
169 |
+
url = s3_client.generate_presigned_url('get_object',
|
170 |
+
Params={'Bucket': S3_BUCKET,
|
171 |
+
'Key': filename},
|
172 |
+
ExpiresIn=3600) # URL expires in 1 hour
|
173 |
+
|
174 |
+
encoded_url = quote(url, safe=':/?&=')
|
175 |
+
logger.info(f"Presigned URL generated: {encoded_url}")
|
176 |
+
|
177 |
+
return JSONResponse(content={"audio_url": encoded_url})
|
178 |
|
179 |
+
except NoCredentialsError:
|
180 |
+
logger.error("AWS credentials not available")
|
181 |
+
raise HTTPException(status_code=500, detail="Could not upload file to S3")
|
182 |
|
183 |
except ValueError as ve:
|
184 |
logger.error(f"ValueError in synthesize_speech: {str(ve)}", exc_info=True)
|