Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ import requests
|
|
| 3 |
from pydub import AudioSegment
|
| 4 |
import os
|
| 5 |
import tempfile
|
| 6 |
-
import json
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
|
@@ -27,12 +26,18 @@ def home():
|
|
| 27 |
<h3>Usage</h3>
|
| 28 |
<p>Send a POST request to <code>/get_audio_duration</code> with JSON like:</p>
|
| 29 |
<pre>
|
| 30 |
-
|
| 31 |
-
"
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
</pre>
|
| 34 |
<p>Example response: <code>{"duration_seconds": 10.5}</code></p>
|
| 35 |
-
<p>Test it with curl: <code>curl -X POST https://eldhos98-audio-duration-n8n.hf.space/get_audio_duration -H "Content-Type: application/json" -d '
|
| 36 |
</body>
|
| 37 |
</html>
|
| 38 |
"""
|
|
@@ -42,24 +47,10 @@ def home():
|
|
| 42 |
@app.route('/get_audio_duration', methods=['POST'])
|
| 43 |
def get_audio_duration():
|
| 44 |
try:
|
| 45 |
-
# Get JSON input
|
| 46 |
data = request.get_json()
|
| 47 |
-
|
| 48 |
-
return jsonify({"error": "Input must be a non-empty array"}), 400
|
| 49 |
|
| 50 |
-
# Extract the 'data' field from the first object
|
| 51 |
-
data_str = data[0].get('data')
|
| 52 |
-
if not data_str:
|
| 53 |
-
return jsonify({"error": "No 'data' field provided in the input"}), 400
|
| 54 |
-
|
| 55 |
-
# Parse the stringified JSON in 'data'
|
| 56 |
-
try:
|
| 57 |
-
inner_data = json.loads(data_str)
|
| 58 |
-
except json.JSONDecodeError:
|
| 59 |
-
return jsonify({"error": "Invalid JSON in 'data' field"}), 400
|
| 60 |
-
|
| 61 |
-
# Extract the audio URL
|
| 62 |
-
audio_url = inner_data.get('audio', {}).get('url')
|
| 63 |
if not audio_url:
|
| 64 |
return jsonify({"error": "No audio URL provided"}), 400
|
| 65 |
|
|
@@ -68,7 +59,7 @@ def get_audio_duration():
|
|
| 68 |
# Download the audio file
|
| 69 |
response = requests.get(audio_url)
|
| 70 |
if response.status_code != 200:
|
| 71 |
-
return
|
| 72 |
|
| 73 |
# Write audio content to temporary file
|
| 74 |
temp_file.write(response.content)
|
|
|
|
| 3 |
from pydub import AudioSegment
|
| 4 |
import os
|
| 5 |
import tempfile
|
|
|
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
|
|
|
| 26 |
<h3>Usage</h3>
|
| 27 |
<p>Send a POST request to <code>/get_audio_duration</code> with JSON like:</p>
|
| 28 |
<pre>
|
| 29 |
+
{
|
| 30 |
+
"audio": {
|
| 31 |
+
"url": "https://example.com/audio.mp3",
|
| 32 |
+
"content_type": "audio/mpeg",
|
| 33 |
+
"file_name": "audio.mp3",
|
| 34 |
+
"file_size": 12345
|
| 35 |
+
},
|
| 36 |
+
"seed": 123
|
| 37 |
+
}
|
| 38 |
</pre>
|
| 39 |
<p>Example response: <code>{"duration_seconds": 10.5}</code></p>
|
| 40 |
+
<p>Test it with curl: <code>curl -X POST https://eldhos98-audio-duration-n8n.hf.space/get_audio_duration -H "Content-Type: application/json" -d '{"audio":{"url":"https://v3.fal.media/files/kangaroo/dPK1FYLC1TNxY5W4QO0AO_output.mp3","content_type":"audio/mpeg","file_name":"output.mp3","file_size":426781},"seed":1322097652}'</code></p>
|
| 41 |
</body>
|
| 42 |
</html>
|
| 43 |
"""
|
|
|
|
| 47 |
@app.route('/get_audio_duration', methods=['POST'])
|
| 48 |
def get_audio_duration():
|
| 49 |
try:
|
| 50 |
+
# Get JSON input
|
| 51 |
data = request.get_json()
|
| 52 |
+
audio_url = data.get('audio', {}).get('url')
|
|
|
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
if not audio_url:
|
| 55 |
return jsonify({"error": "No audio URL provided"}), 400
|
| 56 |
|
|
|
|
| 59 |
# Download the audio file
|
| 60 |
response = requests.get(audio_url)
|
| 61 |
if response.status_code != 200:
|
| 62 |
+
return jupytext({"error": "Failed to download audio file"}), 400
|
| 63 |
|
| 64 |
# Write audio content to temporary file
|
| 65 |
temp_file.write(response.content)
|