Spaces:
Runtime error
Runtime error
integrate json_stripper into /cut_audio
Browse files
list.txt
CHANGED
@@ -1,2 +1,2 @@
|
|
1 |
file '/tmp/genBase.mp3'
|
2 |
-
file '/tmp/quote.mp3'
|
|
|
1 |
file '/tmp/genBase.mp3'
|
2 |
+
file '/tmp/quote.mp3'
|
main.py
CHANGED
@@ -24,7 +24,7 @@ class CutRequest(BaseModel):
|
|
24 |
news_name: str
|
25 |
news_id: int
|
26 |
|
27 |
-
async def json_stripper(
|
28 |
raw_body = await request.body()
|
29 |
try:
|
30 |
# Decode the raw body
|
@@ -39,10 +39,7 @@ async def json_stripper(strigified_json):
|
|
39 |
# Parse the JSON
|
40 |
body = json.loads(body_str)
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
print(cut_request)
|
45 |
-
return {"message": "Request processed successfully"}
|
46 |
except json.JSONDecodeError as e:
|
47 |
print(f"JSON Decode Error: {e}")
|
48 |
raise HTTPException(status_code=400, detail=f"Invalid JSON: {str(e)}")
|
@@ -75,10 +72,19 @@ def download_file(news_name: str, quote_filename: str, new_filename: str = "sour
|
|
75 |
|
76 |
|
77 |
@app.post("/cut-audio")
|
78 |
-
def cut_audio(request: CutRequest):
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
output_file = f"/tmp/cut_quote_{i}.mp3"
|
83 |
try:
|
84 |
(
|
@@ -90,7 +96,7 @@ def cut_audio(request: CutRequest):
|
|
90 |
except ffmpeg.Error as e:
|
91 |
raise HTTPException(status_code=500, detail=str(e))
|
92 |
try:
|
93 |
-
s3_output_key = f'{aws_env}/{
|
94 |
s3_client.upload_file(output_file, s3_bucket_name, s3_output_key)
|
95 |
except Exception as e:
|
96 |
raise HTTPException(status_code=500, detail=f"Failed to upload file to S3: {e}")
|
|
|
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
|
|
|
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)}")
|
|
|
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 = CutRequest(**body)
|
82 |
+
except ValueError as e:
|
83 |
+
raise HTTPException(status_code=400, detail=str(e))
|
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 |
(
|
|
|
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}")
|