Spaces:
Build error
Build error
add logging to debug mysterious 500
Browse files
main.py
CHANGED
|
@@ -2,11 +2,15 @@ from pydantic import BaseModel
|
|
| 2 |
from fastapi import FastAPI, UploadFile, HTTPException
|
| 3 |
from ffmpeg import input, output, run
|
| 4 |
from botocore.exceptions import ClientError
|
|
|
|
| 5 |
import ffmpeg
|
| 6 |
import boto3
|
| 7 |
import json
|
| 8 |
import os
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
# Define your AWS S3 credentials and bucket name
|
|
@@ -29,6 +33,7 @@ class CutRequestInput(BaseModel):
|
|
| 29 |
data: CutRequest
|
| 30 |
|
| 31 |
def download_file(news_name: str, quote_filename: str, new_filename: str = "source.mp3"):
|
|
|
|
| 32 |
s3_directory = f'{aws_env}/{news_name}'
|
| 33 |
s3_object_key = f'{s3_directory}/{quote_filename}'
|
| 34 |
local_file = os.path.join(temp_dir, new_filename)
|
|
@@ -36,37 +41,47 @@ def download_file(news_name: str, quote_filename: str, new_filename: str = "sour
|
|
| 36 |
try:
|
| 37 |
# Ensure the download directory exists
|
| 38 |
os.makedirs(temp_dir, exist_ok=True)
|
|
|
|
| 39 |
|
| 40 |
s3_client.download_file(s3_bucket_name, s3_object_key, local_file)
|
| 41 |
-
|
| 42 |
|
| 43 |
# Return the local_file path
|
| 44 |
return local_file
|
| 45 |
|
| 46 |
except ClientError as e:
|
| 47 |
if e.response['Error']['Code'] == "404":
|
|
|
|
|
|
|
| 48 |
raise HTTPException(status_code=404, detail=f"The file {quote_filename} does not exist in S3.")
|
| 49 |
else:
|
|
|
|
|
|
|
| 50 |
raise HTTPException(status_code=500, detail=f"Failed to download file from S3: {str(e)}")
|
| 51 |
except Exception as e:
|
|
|
|
|
|
|
| 52 |
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
|
| 53 |
|
| 54 |
|
| 55 |
@app.post("/cut-audio")
|
| 56 |
async def cut_audio(request: CutRequestInput):
|
| 57 |
-
|
| 58 |
|
| 59 |
try:
|
| 60 |
|
| 61 |
cut_request = request.data
|
|
|
|
| 62 |
|
| 63 |
-
print(f"Received request: {cut_request}")
|
| 64 |
|
|
|
|
| 65 |
|
| 66 |
download_file(cut_request.news_name, cut_request.quote_filename)
|
| 67 |
for i, segment in enumerate(cut_request.segments):
|
| 68 |
start, end = segment
|
| 69 |
-
|
|
|
|
|
|
|
| 70 |
try:
|
| 71 |
(
|
| 72 |
ffmpeg
|
|
@@ -75,19 +90,22 @@ async def cut_audio(request: CutRequestInput):
|
|
| 75 |
.run()
|
| 76 |
)
|
| 77 |
except ffmpeg.Error as e:
|
|
|
|
|
|
|
| 78 |
raise HTTPException(status_code=500, detail=str(e))
|
| 79 |
try:
|
| 80 |
s3_output_key = f'{aws_env}/{cut_request.news_id}/genBase_segment_{i}.mp3'
|
| 81 |
s3_client.upload_file(output_file, s3_bucket_name, s3_output_key)
|
| 82 |
except Exception as e:
|
|
|
|
|
|
|
| 83 |
raise HTTPException(status_code=500, detail=f"Failed to upload file to S3: {e}")
|
| 84 |
|
| 85 |
return {"message": "Audio cut successfully"}
|
| 86 |
|
| 87 |
-
except
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
raise HTTPException(status_code=422, detail=str(e))
|
| 91 |
|
| 92 |
@app.post("/merge_audio/{output_key}")
|
| 93 |
async def merge_audio(output_key: str):
|
|
|
|
| 2 |
from fastapi import FastAPI, UploadFile, HTTPException
|
| 3 |
from ffmpeg import input, output, run
|
| 4 |
from botocore.exceptions import ClientError
|
| 5 |
+
import logging
|
| 6 |
import ffmpeg
|
| 7 |
import boto3
|
| 8 |
import json
|
| 9 |
import os
|
| 10 |
|
| 11 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 12 |
+
logger = logging.getLogger(__name__)
|
| 13 |
+
|
| 14 |
app = FastAPI()
|
| 15 |
|
| 16 |
# Define your AWS S3 credentials and bucket name
|
|
|
|
| 33 |
data: CutRequest
|
| 34 |
|
| 35 |
def download_file(news_name: str, quote_filename: str, new_filename: str = "source.mp3"):
|
| 36 |
+
logger.debug(f"Downloading file: news_name={news_name}, quote_filename={quote_filename}")
|
| 37 |
s3_directory = f'{aws_env}/{news_name}'
|
| 38 |
s3_object_key = f'{s3_directory}/{quote_filename}'
|
| 39 |
local_file = os.path.join(temp_dir, new_filename)
|
|
|
|
| 41 |
try:
|
| 42 |
# Ensure the download directory exists
|
| 43 |
os.makedirs(temp_dir, exist_ok=True)
|
| 44 |
+
logger.debug(f"Downloading from S3: bucket={s3_bucket_name}, key={s3_object_key}, local_file={local_file}")
|
| 45 |
|
| 46 |
s3_client.download_file(s3_bucket_name, s3_object_key, local_file)
|
| 47 |
+
logger.debug(f"File downloaded and saved as: {local_file}")
|
| 48 |
|
| 49 |
# Return the local_file path
|
| 50 |
return local_file
|
| 51 |
|
| 52 |
except ClientError as e:
|
| 53 |
if e.response['Error']['Code'] == "404":
|
| 54 |
+
logger.error(f"File not found in S3: {s3_object_key}")
|
| 55 |
+
|
| 56 |
raise HTTPException(status_code=404, detail=f"The file {quote_filename} does not exist in S3.")
|
| 57 |
else:
|
| 58 |
+
logger.error(f"S3 download error: {str(e)}")
|
| 59 |
+
|
| 60 |
raise HTTPException(status_code=500, detail=f"Failed to download file from S3: {str(e)}")
|
| 61 |
except Exception as e:
|
| 62 |
+
logger.exception(f"Unexpected error in download_file: {str(e)}")
|
| 63 |
+
|
| 64 |
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
|
| 65 |
|
| 66 |
|
| 67 |
@app.post("/cut-audio")
|
| 68 |
async def cut_audio(request: CutRequestInput):
|
| 69 |
+
logger.debug(f"Received request: {request}")
|
| 70 |
|
| 71 |
try:
|
| 72 |
|
| 73 |
cut_request = request.data
|
| 74 |
+
logger.debug(f"Parsed cut_request: {cut_request}")
|
| 75 |
|
|
|
|
| 76 |
|
| 77 |
+
print(f"Received request: {cut_request}")
|
| 78 |
|
| 79 |
download_file(cut_request.news_name, cut_request.quote_filename)
|
| 80 |
for i, segment in enumerate(cut_request.segments):
|
| 81 |
start, end = segment
|
| 82 |
+
logger.debug(f"Processing segment {i}: start={start}, end={end}")
|
| 83 |
+
|
| 84 |
+
output_file = f"/tmp/cut_quote_{i}{cut_request.quote_filename}"
|
| 85 |
try:
|
| 86 |
(
|
| 87 |
ffmpeg
|
|
|
|
| 90 |
.run()
|
| 91 |
)
|
| 92 |
except ffmpeg.Error as e:
|
| 93 |
+
logger.error(f"FFMPEG Error: {str(e)}")
|
| 94 |
+
|
| 95 |
raise HTTPException(status_code=500, detail=str(e))
|
| 96 |
try:
|
| 97 |
s3_output_key = f'{aws_env}/{cut_request.news_id}/genBase_segment_{i}.mp3'
|
| 98 |
s3_client.upload_file(output_file, s3_bucket_name, s3_output_key)
|
| 99 |
except Exception as e:
|
| 100 |
+
logger.error(f"S3 Upload Error: {str(e)}")
|
| 101 |
+
|
| 102 |
raise HTTPException(status_code=500, detail=f"Failed to upload file to S3: {e}")
|
| 103 |
|
| 104 |
return {"message": "Audio cut successfully"}
|
| 105 |
|
| 106 |
+
except Exception as e:
|
| 107 |
+
logger.exception("Unexpected error in cut_audio")
|
| 108 |
+
raise HTTPException(status_code=500, detail=f"Unexpected error: {str(e)}")
|
|
|
|
| 109 |
|
| 110 |
@app.post("/merge_audio/{output_key}")
|
| 111 |
async def merge_audio(output_key: str):
|