Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,6 @@
|
|
1 |
-
|
|
|
|
|
2 |
from fastapi.responses import JSONResponse
|
3 |
from pydantic import BaseModel
|
4 |
import numpy as np
|
@@ -13,7 +15,6 @@ from pydub import AudioSegment
|
|
13 |
from moviepy.editor import VideoFileClip
|
14 |
import traceback
|
15 |
from logging.handlers import RotatingFileHandler
|
16 |
-
import os
|
17 |
import boto3
|
18 |
from botocore.exceptions import NoCredentialsError
|
19 |
import time
|
@@ -44,6 +45,10 @@ S3_REGION = os.environ.get("S3_REGION")
|
|
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',
|
@@ -62,6 +67,11 @@ class TTSRequest(BaseModel):
|
|
62 |
language: str
|
63 |
speed: float
|
64 |
|
|
|
|
|
|
|
|
|
|
|
65 |
def extract_audio_from_file(input_bytes):
|
66 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.tmp') as temp_file:
|
67 |
temp_file.write(input_bytes)
|
@@ -114,7 +124,7 @@ def extract_audio_from_file(input_bytes):
|
|
114 |
os.unlink(temp_file_path)
|
115 |
|
116 |
@app.post("/transcribe")
|
117 |
-
async def transcribe_audio(request: AudioRequest):
|
118 |
start_time = time.time()
|
119 |
try:
|
120 |
input_bytes = base64.b64decode(request.audio)
|
@@ -142,8 +152,37 @@ async def transcribe_audio(request: AudioRequest):
|
|
142 |
content={"message": "An error occurred during transcription", "details": error_details, "processing_time_seconds": processing_time}
|
143 |
)
|
144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
@app.post("/synthesize")
|
146 |
-
async def synthesize_speech(request: TTSRequest):
|
147 |
start_time = time.time()
|
148 |
logger.info(f"Synthesize request received: text='{request.text}', language='{request.language}', speed={request.speed}")
|
149 |
try:
|
@@ -241,7 +280,7 @@ async def synthesize_speech(request: TTSRequest):
|
|
241 |
logger.info("Synthesize request completed")
|
242 |
|
243 |
@app.post("/identify")
|
244 |
-
async def identify_language(request: AudioRequest):
|
245 |
start_time = time.time()
|
246 |
try:
|
247 |
input_bytes = base64.b64decode(request.audio)
|
@@ -261,8 +300,29 @@ async def identify_language(request: AudioRequest):
|
|
261 |
content={"message": "An error occurred during language identification", "details": error_details, "processing_time_seconds": processing_time}
|
262 |
)
|
263 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
264 |
@app.get("/asr_languages")
|
265 |
-
async def get_asr_languages():
|
266 |
start_time = time.time()
|
267 |
try:
|
268 |
processing_time = time.time() - start_time
|
@@ -280,7 +340,7 @@ async def get_asr_languages():
|
|
280 |
)
|
281 |
|
282 |
@app.get("/tts_languages")
|
283 |
-
async def get_tts_languages():
|
284 |
start_time = time.time()
|
285 |
try:
|
286 |
processing_time = time.time() - start_time
|
|
|
1 |
+
import os
|
2 |
+
from fastapi import FastAPI, HTTPException, File, UploadFile, Depends, Security
|
3 |
+
from fastapi.security.api_key import APIKeyHeader, APIKey
|
4 |
from fastapi.responses import JSONResponse
|
5 |
from pydantic import BaseModel
|
6 |
import numpy as np
|
|
|
15 |
from moviepy.editor import VideoFileClip
|
16 |
import traceback
|
17 |
from logging.handlers import RotatingFileHandler
|
|
|
18 |
import boto3
|
19 |
from botocore.exceptions import NoCredentialsError
|
20 |
import time
|
|
|
45 |
S3_ACCESS_KEY_ID = os.environ.get("AWS_ACCESS_KEY_ID")
|
46 |
S3_SECRET_ACCESS_KEY = os.environ.get("AWS_SECRET_ACCESS_KEY")
|
47 |
|
48 |
+
# API Key Configuration
|
49 |
+
API_KEY = os.environ.get("API_KEY")
|
50 |
+
api_key_header = APIKeyHeader(name="X-API-Key", auto_error=False)
|
51 |
+
|
52 |
# Initialize S3 client
|
53 |
s3_client = boto3.client(
|
54 |
's3',
|
|
|
67 |
language: str
|
68 |
speed: float
|
69 |
|
70 |
+
async def get_api_key(api_key_header: str = Security(api_key_header)):
|
71 |
+
if api_key_header == API_KEY:
|
72 |
+
return api_key_header
|
73 |
+
raise HTTPException(status_code=403, detail="Could not validate credentials")
|
74 |
+
|
75 |
def extract_audio_from_file(input_bytes):
|
76 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.tmp') as temp_file:
|
77 |
temp_file.write(input_bytes)
|
|
|
124 |
os.unlink(temp_file_path)
|
125 |
|
126 |
@app.post("/transcribe")
|
127 |
+
async def transcribe_audio(request: AudioRequest, api_key: APIKey = Depends(get_api_key)):
|
128 |
start_time = time.time()
|
129 |
try:
|
130 |
input_bytes = base64.b64decode(request.audio)
|
|
|
152 |
content={"message": "An error occurred during transcription", "details": error_details, "processing_time_seconds": processing_time}
|
153 |
)
|
154 |
|
155 |
+
@app.post("/transcribe_file")
|
156 |
+
async def transcribe_audio_file(file: UploadFile = File(...), language: str = "", api_key: APIKey = Depends(get_api_key)):
|
157 |
+
start_time = time.time()
|
158 |
+
try:
|
159 |
+
contents = await file.read()
|
160 |
+
audio_array, sample_rate = extract_audio_from_file(contents)
|
161 |
+
|
162 |
+
# Ensure audio_array is float32
|
163 |
+
audio_array = audio_array.astype(np.float32)
|
164 |
+
|
165 |
+
# Resample if necessary
|
166 |
+
if sample_rate != ASR_SAMPLING_RATE:
|
167 |
+
audio_array = librosa.resample(audio_array, orig_sr=sample_rate, target_sr=ASR_SAMPLING_RATE)
|
168 |
+
|
169 |
+
result = transcribe(audio_array, language)
|
170 |
+
processing_time = time.time() - start_time
|
171 |
+
return JSONResponse(content={"transcription": result, "processing_time_seconds": processing_time})
|
172 |
+
except Exception as e:
|
173 |
+
logger.error(f"Error in transcribe_audio_file: {str(e)}", exc_info=True)
|
174 |
+
error_details = {
|
175 |
+
"error": str(e),
|
176 |
+
"traceback": traceback.format_exc()
|
177 |
+
}
|
178 |
+
processing_time = time.time() - start_time
|
179 |
+
return JSONResponse(
|
180 |
+
status_code=500,
|
181 |
+
content={"message": "An error occurred during transcription", "details": error_details, "processing_time_seconds": processing_time}
|
182 |
+
)
|
183 |
+
|
184 |
@app.post("/synthesize")
|
185 |
+
async def synthesize_speech(request: TTSRequest, api_key: APIKey = Depends(get_api_key)):
|
186 |
start_time = time.time()
|
187 |
logger.info(f"Synthesize request received: text='{request.text}', language='{request.language}', speed={request.speed}")
|
188 |
try:
|
|
|
280 |
logger.info("Synthesize request completed")
|
281 |
|
282 |
@app.post("/identify")
|
283 |
+
async def identify_language(request: AudioRequest, api_key: APIKey = Depends(get_api_key)):
|
284 |
start_time = time.time()
|
285 |
try:
|
286 |
input_bytes = base64.b64decode(request.audio)
|
|
|
300 |
content={"message": "An error occurred during language identification", "details": error_details, "processing_time_seconds": processing_time}
|
301 |
)
|
302 |
|
303 |
+
@app.post("/identify_file")
|
304 |
+
async def identify_language_file(file: UploadFile = File(...), api_key: APIKey = Depends(get_api_key)):
|
305 |
+
start_time = time.time()
|
306 |
+
try:
|
307 |
+
contents = await file.read()
|
308 |
+
audio_array, sample_rate = extract_audio_from_file(contents)
|
309 |
+
result = identify(audio_array)
|
310 |
+
processing_time = time.time() - start_time
|
311 |
+
return JSONResponse(content={"language_identification": result, "processing_time_seconds": processing_time})
|
312 |
+
except Exception as e:
|
313 |
+
logger.error(f"Error in identify_language_file: {str(e)}", exc_info=True)
|
314 |
+
error_details = {
|
315 |
+
"error": str(e),
|
316 |
+
"traceback": traceback.format_exc()
|
317 |
+
}
|
318 |
+
processing_time = time.time() - start_time
|
319 |
+
return JSONResponse(
|
320 |
+
status_code=500,
|
321 |
+
content={"message": "An error occurred during language identification", "details": error_details, "processing_time_seconds": processing_time}
|
322 |
+
)
|
323 |
+
|
324 |
@app.get("/asr_languages")
|
325 |
+
async def get_asr_languages(api_key: APIKey = Depends(get_api_key)):
|
326 |
start_time = time.time()
|
327 |
try:
|
328 |
processing_time = time.time() - start_time
|
|
|
340 |
)
|
341 |
|
342 |
@app.get("/tts_languages")
|
343 |
+
async def get_tts_languages(api_key: APIKey = Depends(get_api_key)):
|
344 |
start_time = time.time()
|
345 |
try:
|
346 |
processing_time = time.time() - start_time
|