Spaces:
Build error
Build error
remove json_stripper
Browse files
main.py
CHANGED
|
@@ -4,6 +4,7 @@ from ffmpeg import input, output, run
|
|
| 4 |
from botocore.exceptions import ClientError
|
| 5 |
import ffmpeg
|
| 6 |
import boto3
|
|
|
|
| 7 |
import os
|
| 8 |
|
| 9 |
app = FastAPI()
|
|
@@ -24,29 +25,6 @@ class CutRequest(BaseModel):
|
|
| 24 |
news_name: str
|
| 25 |
news_id: int
|
| 26 |
|
| 27 |
-
async def json_stripper(request: CutRequest):
|
| 28 |
-
raw_body = await request.body()
|
| 29 |
-
try:
|
| 30 |
-
# Decode the raw body
|
| 31 |
-
body_str = raw_body.decode('utf-8')
|
| 32 |
-
|
| 33 |
-
# Remove any surrounding quotes if present
|
| 34 |
-
body_str = body_str.strip('"')
|
| 35 |
-
|
| 36 |
-
# Unescape the JSON string
|
| 37 |
-
body_str = body_str.replace('\\"', '"')
|
| 38 |
-
|
| 39 |
-
# Parse the JSON
|
| 40 |
-
body = json.loads(body_str)
|
| 41 |
-
|
| 42 |
-
return body
|
| 43 |
-
except json.JSONDecodeError as e:
|
| 44 |
-
print(f"JSON Decode Error: {e}")
|
| 45 |
-
raise HTTPException(status_code=400, detail=f"Invalid JSON: {str(e)}")
|
| 46 |
-
except Exception as e:
|
| 47 |
-
print(f"Error: {e}")
|
| 48 |
-
raise HTTPException(status_code=400, detail=str(e))
|
| 49 |
-
|
| 50 |
def download_file(news_name: str, quote_filename: str, new_filename: str = "source.mp3"):
|
| 51 |
s3_directory = f'{aws_env}/{news_name}'
|
| 52 |
s3_object_key = f'{s3_directory}/{quote_filename}'
|
|
@@ -72,36 +50,33 @@ def download_file(news_name: str, quote_filename: str, new_filename: str = "sour
|
|
| 72 |
|
| 73 |
|
| 74 |
@app.post("/cut-audio")
|
| 75 |
-
async def cut_audio(request: CutRequest):
|
| 76 |
-
# Use json_stripper to parse the request body
|
| 77 |
-
body = await json_stripper(request)
|
| 78 |
-
|
| 79 |
-
# Validate the body against CutRequest model
|
| 80 |
try:
|
| 81 |
-
cut_request =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
except ValueError as e:
|
| 83 |
-
raise HTTPException(status_code=
|
| 84 |
-
|
| 85 |
-
# Now you can use cut_request in your function
|
| 86 |
-
download_file(cut_request.news_name, cut_request.quote_filename)
|
| 87 |
-
for i, (start, end) in enumerate(cut_request.segments):
|
| 88 |
-
output_file = f"/tmp/cut_quote_{i}.mp3"
|
| 89 |
-
try:
|
| 90 |
-
(
|
| 91 |
-
ffmpeg
|
| 92 |
-
.input('/tmp/source.mp3', ss=start, to=end)
|
| 93 |
-
.output(output_file)
|
| 94 |
-
.run()
|
| 95 |
-
)
|
| 96 |
-
except ffmpeg.Error as e:
|
| 97 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 98 |
-
try:
|
| 99 |
-
s3_output_key = f'{aws_env}/{cut_request.news_id}/genBase_segment_{i}.mp3'
|
| 100 |
-
s3_client.upload_file(output_file, s3_bucket_name, s3_output_key)
|
| 101 |
-
except Exception as e:
|
| 102 |
-
raise HTTPException(status_code=500, detail=f"Failed to upload file to S3: {e}")
|
| 103 |
-
|
| 104 |
-
return {"message": "Audio cut successfully"}
|
| 105 |
|
| 106 |
@app.post("/merge_audio/{output_key}")
|
| 107 |
async def merge_audio(output_key: str):
|
|
|
|
| 4 |
from botocore.exceptions import ClientError
|
| 5 |
import ffmpeg
|
| 6 |
import boto3
|
| 7 |
+
import json
|
| 8 |
import os
|
| 9 |
|
| 10 |
app = FastAPI()
|
|
|
|
| 25 |
news_name: str
|
| 26 |
news_id: int
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
def download_file(news_name: str, quote_filename: str, new_filename: str = "source.mp3"):
|
| 29 |
s3_directory = f'{aws_env}/{news_name}'
|
| 30 |
s3_object_key = f'{s3_directory}/{quote_filename}'
|
|
|
|
| 50 |
|
| 51 |
|
| 52 |
@app.post("/cut-audio")
|
| 53 |
+
async def cut_audio(request: CutRequest = Body(...)):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
try:
|
| 55 |
+
cut_request = request
|
| 56 |
+
|
| 57 |
+
download_file(cut_request.news_name, cut_request.quote_filename)
|
| 58 |
+
for i, segment in enumerate(cut_request.segments):
|
| 59 |
+
start, end = segment
|
| 60 |
+
output_file = f"/tmp/cut_quote_{i}.mp3"
|
| 61 |
+
try:
|
| 62 |
+
(
|
| 63 |
+
ffmpeg
|
| 64 |
+
.input('/tmp/source.mp3', ss=start, to=end)
|
| 65 |
+
.output(output_file)
|
| 66 |
+
.run()
|
| 67 |
+
)
|
| 68 |
+
except ffmpeg.Error as e:
|
| 69 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 70 |
+
try:
|
| 71 |
+
s3_output_key = f'{aws_env}/{cut_request.news_id}/genBase_segment_{i}.mp3'
|
| 72 |
+
s3_client.upload_file(output_file, s3_bucket_name, s3_output_key)
|
| 73 |
+
except Exception as e:
|
| 74 |
+
raise HTTPException(status_code=500, detail=f"Failed to upload file to S3: {e}")
|
| 75 |
+
|
| 76 |
+
return {"message": "Audio cut successfully"}
|
| 77 |
+
|
| 78 |
except ValueError as e:
|
| 79 |
+
raise HTTPException(status_code=422, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
@app.post("/merge_audio/{output_key}")
|
| 82 |
async def merge_audio(output_key: str):
|