Spaces:
Build error
Build error
expand request to involve news name and quote name
Browse files
main.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
from pydantic import BaseModel
|
| 2 |
from fastapi import FastAPI, UploadFile, HTTPException
|
| 3 |
from ffmpeg import input, output, run
|
|
|
|
| 4 |
import ffmpeg
|
| 5 |
import boto3
|
| 6 |
import os
|
|
@@ -18,44 +19,43 @@ temp_dir = '/tmp/'
|
|
| 18 |
s3_client = boto3.client('s3', aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key, region_name=aws_region)
|
| 19 |
|
| 20 |
class CutRequest(BaseModel):
|
|
|
|
|
|
|
| 21 |
segments: list[tuple[float, float]]
|
| 22 |
|
| 23 |
-
def download_file(news_name: str,quote_filename: str):
|
| 24 |
-
# Define a function to download a single file
|
| 25 |
s3_directory = f'{aws_env}/{news_name}'
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
try:
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
s3_client.download_file(s3_bucket_name, s3_object_key, local_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
-
raise HTTPException(status_code=500, detail=f"
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
# def download_files(output_key: str):
|
| 36 |
|
| 37 |
-
# # Temporary directory to store uploaded audio files
|
| 38 |
-
# s3_directory = output_key
|
| 39 |
|
| 40 |
-
|
| 41 |
-
# try:
|
| 42 |
-
# for input_file in input_files:
|
| 43 |
-
# s3_object_key = f'{s3_directory}/{input_file}'
|
| 44 |
-
# local_file = temp_dir + input_file
|
| 45 |
-
# s3_client.download_file(s3_bucket_name, s3_object_key, local_file)
|
| 46 |
-
# except Exception as e:
|
| 47 |
-
# raise HTTPException(status_code=500, detail=f"Failed to download files from S3: {e}")
|
| 48 |
-
|
| 49 |
-
@app.post("/cut-audio/{output_key}")
|
| 50 |
def cut_audio(request: CutRequest, output_key: str):
|
| 51 |
-
|
| 52 |
for i, (start, end) in enumerate(request.segments):
|
| 53 |
output_file = f"/tmp/cut_quote_{i}.mp3"
|
| 54 |
-
# output_file = f"/tmp/genBase_segment_{i}.mp3"
|
| 55 |
try:
|
| 56 |
(
|
| 57 |
ffmpeg
|
| 58 |
-
.input('/tmp/
|
| 59 |
.output(output_file)
|
| 60 |
.run()
|
| 61 |
)
|
|
@@ -69,8 +69,6 @@ def cut_audio(request: CutRequest, output_key: str):
|
|
| 69 |
|
| 70 |
return {"message": "Audio cut successfully"}
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
@app.post("/merge_audio/{output_key}")
|
| 75 |
async def merge_audio(output_key: str):
|
| 76 |
s3_directory = output_key
|
|
|
|
| 1 |
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 os
|
|
|
|
| 19 |
s3_client = boto3.client('s3', aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key, region_name=aws_region)
|
| 20 |
|
| 21 |
class CutRequest(BaseModel):
|
| 22 |
+
news_name: str
|
| 23 |
+
quote_filename: str
|
| 24 |
segments: list[tuple[float, float]]
|
| 25 |
|
| 26 |
+
def download_file(news_name: str, quote_filename: str, new_filename: str = "source.mp3"):
|
|
|
|
| 27 |
s3_directory = f'{aws_env}/{news_name}'
|
| 28 |
+
s3_object_key = f'{s3_directory}/{quote_filename}'
|
| 29 |
+
local_file = os.path.join(temp_dir, new_filename)
|
| 30 |
+
|
| 31 |
try:
|
| 32 |
+
# Ensure the download directory exists
|
| 33 |
+
os.makedirs(temp_dir, exist_ok=True)
|
| 34 |
+
|
| 35 |
s3_client.download_file(s3_bucket_name, s3_object_key, local_file)
|
| 36 |
+
print(f"File downloaded and saved as: {local_file}")
|
| 37 |
+
|
| 38 |
+
# Return the local_file path
|
| 39 |
+
return local_file
|
| 40 |
+
|
| 41 |
+
except ClientError as e:
|
| 42 |
+
if e.response['Error']['Code'] == "404":
|
| 43 |
+
raise HTTPException(status_code=404, detail=f"The file {quote_filename} does not exist in S3.")
|
| 44 |
+
else:
|
| 45 |
+
raise HTTPException(status_code=500, detail=f"Failed to download file from S3: {str(e)}")
|
| 46 |
except Exception as e:
|
| 47 |
+
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
|
|
|
|
|
|
|
|
|
|
| 48 |
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
@app.post("/cut-audio/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
def cut_audio(request: CutRequest, output_key: str):
|
| 52 |
+
download_file(request.news_name,request.quote_filename)
|
| 53 |
for i, (start, end) in enumerate(request.segments):
|
| 54 |
output_file = f"/tmp/cut_quote_{i}.mp3"
|
|
|
|
| 55 |
try:
|
| 56 |
(
|
| 57 |
ffmpeg
|
| 58 |
+
.input('/tmp/source.mp3', ss=start, to=end)
|
| 59 |
.output(output_file)
|
| 60 |
.run()
|
| 61 |
)
|
|
|
|
| 69 |
|
| 70 |
return {"message": "Audio cut successfully"}
|
| 71 |
|
|
|
|
|
|
|
| 72 |
@app.post("/merge_audio/{output_key}")
|
| 73 |
async def merge_audio(output_key: str):
|
| 74 |
s3_directory = output_key
|