onurio commited on
Commit
1453b79
·
1 Parent(s): 5e4fff7

do not decode into utf

Browse files
Files changed (2) hide show
  1. handler.py +1 -1
  2. test_api.py +35 -0
handler.py CHANGED
@@ -32,7 +32,7 @@ class EndpointHandler():
32
 
33
 
34
  # Convert audio data to base64 string
35
- audio_base64 = base64.b64encode(audio_data.tobytes()).decode("utf-8")
36
 
37
  # Create JSON response
38
  response = {
 
32
 
33
 
34
  # Convert audio data to base64 string
35
+ audio_base64 = base64.b64encode(audio_data.tobytes())
36
 
37
  # Create JSON response
38
  response = {
test_api.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import base64
3
+ import soundfile as sf
4
+
5
+ API_URL = "https://ooj7l6a226z6pn8j.us-east-1.aws.endpoints.huggingface.cloud"
6
+ headers = {
7
+ "Accept" : "application/json",
8
+ "Authorization": "Bearer token",
9
+ "Content-Type": "application/json"
10
+ }
11
+
12
+ def query(payload):
13
+ response = requests.post(API_URL, headers=headers, json=payload)
14
+ return response.json()
15
+
16
+ response = query({
17
+ "inputs": "deep bass hip hop with trumpet",
18
+ "parameters": {}
19
+ })
20
+
21
+ # Extract audio data and sampling rate from the JSON response
22
+ audio_base64 = response["audio_base64"]
23
+ sampling_rate = response["sampling_rate"]
24
+
25
+ # Decode the base64-encoded audio data
26
+ audio_binary = base64.b64decode(audio_base64)
27
+
28
+ # Write the audio data to a WAV file
29
+ output_file_path = "output.wav" # Specify the file path
30
+ sf.write(output_file_path, audio_binary, sampling_rate)
31
+
32
+ # Write the audio data to a file
33
+ output_file_path = "output.wav" # Specify the file path
34
+ with open(output_file_path, "wb") as f:
35
+ f.write(audio_binary)