Spaces:
Runtime error
Runtime error
try out
Browse files- .gitignore +1 -0
- Dockerfile +14 -0
- main.py +59 -0
- requirements.txt +22 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.venv/
|
Dockerfile
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.10
|
2 |
+
|
3 |
+
RUN apt-get update && \
|
4 |
+
apt install -y ffmpeg --no-recommends
|
5 |
+
|
6 |
+
WORKDIR /code
|
7 |
+
|
8 |
+
COPY ./requirements.txt /code/requirements.txt
|
9 |
+
|
10 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
11 |
+
|
12 |
+
COPY . .
|
13 |
+
|
14 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, UploadFile, HTTPException
|
2 |
+
from ffmpeg import input, output, run
|
3 |
+
import boto3
|
4 |
+
|
5 |
+
app = FastAPI()
|
6 |
+
|
7 |
+
# Define your AWS S3 credentials and bucket name
|
8 |
+
aws_access_key_id = 'YOUR_AWS_ACCESS_KEY_ID'
|
9 |
+
aws_secret_access_key = 'YOUR_AWS_SECRET_ACCESS_KEY'
|
10 |
+
s3_bucket_name = 'YOUR_S3_BUCKET_NAME'
|
11 |
+
|
12 |
+
# Initialize an S3 client
|
13 |
+
s3_client = boto3.client('s3', aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key)
|
14 |
+
|
15 |
+
@app.post("/merge_audio/{output_key}")
|
16 |
+
async def merge_audio(output_key: str):
|
17 |
+
# Temporary directory to store uploaded audio files
|
18 |
+
temp_dir = '/tmp/'
|
19 |
+
|
20 |
+
# Local paths for the fixed input audio files
|
21 |
+
gen_base_file = temp_dir + 'genBase.mp3'
|
22 |
+
quote_file = temp_dir + 'quote.mp3'
|
23 |
+
|
24 |
+
# Download files from S3 based on the provided key
|
25 |
+
s3_directory = 'your-s3-directory/' + output_key
|
26 |
+
s3_gen_base_key = f'{s3_directory}/genBase.mp3'
|
27 |
+
s3_quote_key = f'{s3_directory}/quote.mp3'
|
28 |
+
|
29 |
+
try:
|
30 |
+
s3_client.download_file(s3_bucket_name, s3_gen_base_key, gen_base_file)
|
31 |
+
s3_client.download_file(s3_bucket_name, s3_quote_key, quote_file)
|
32 |
+
except Exception as e:
|
33 |
+
raise HTTPException(status_code=500, detail=f"Failed to download files from S3: {e}")
|
34 |
+
|
35 |
+
# Output file name
|
36 |
+
output_file = temp_dir + 'output.mp3'
|
37 |
+
|
38 |
+
# Use python-ffmpeg to merge audio clips
|
39 |
+
input_args = [input(gen_base_file), input(quote_file)]
|
40 |
+
output_args = output(output_file, acodec='copy')
|
41 |
+
|
42 |
+
try:
|
43 |
+
run(*input_args, output_args, overwrite_output=True)
|
44 |
+
except Exception as e:
|
45 |
+
raise HTTPException(status_code=500, detail=f"FFmpeg Error: {e}")
|
46 |
+
|
47 |
+
# Upload the merged file back to S3
|
48 |
+
s3_output_key = f'{s3_directory}/output.mp3'
|
49 |
+
try:
|
50 |
+
s3_client.upload_file(output_file, s3_bucket_name, s3_output_key)
|
51 |
+
except Exception as e:
|
52 |
+
raise HTTPException(status_code=500, detail=f"Failed to upload file to S3: {e}")
|
53 |
+
|
54 |
+
return {"message": "Audio clips successfully merged and saved in S3"}
|
55 |
+
|
56 |
+
if __name__ == "__main__":
|
57 |
+
import uvicorn
|
58 |
+
uvicorn.run(app, host="0.0.0.0", port=7680)
|
59 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
annotated-types==0.6.0
|
2 |
+
anyio==4.2.0
|
3 |
+
boto3==1.34.16
|
4 |
+
botocore==1.34.16
|
5 |
+
click==8.1.7
|
6 |
+
exceptiongroup==1.2.0
|
7 |
+
fastapi==0.108.0
|
8 |
+
h11==0.14.0
|
9 |
+
idna==3.6
|
10 |
+
jmespath==1.0.1
|
11 |
+
pydantic==2.5.3
|
12 |
+
pydantic_core==2.14.6
|
13 |
+
pyee==11.1.0
|
14 |
+
python-dateutil==2.8.2
|
15 |
+
python-ffmpeg==2.0.10
|
16 |
+
s3transfer==0.10.0
|
17 |
+
six==1.16.0
|
18 |
+
sniffio==1.3.0
|
19 |
+
starlette==0.32.0.post1
|
20 |
+
typing_extensions==4.9.0
|
21 |
+
urllib3==2.0.7
|
22 |
+
uvicorn==0.25.0
|