onurio commited on
Commit
93e94bb
·
1 Parent(s): 1453b79
Files changed (5) hide show
  1. __pycache__/handler.cpython-311.pyc +0 -0
  2. handler.py +2 -6
  3. output.wav +0 -0
  4. test.py +11 -2
  5. test_api.py +11 -6
__pycache__/handler.cpython-311.pyc CHANGED
Binary files a/__pycache__/handler.cpython-311.pyc and b/__pycache__/handler.cpython-311.pyc differ
 
handler.py CHANGED
@@ -11,7 +11,7 @@ class EndpointHandler():
11
  def __init__(self, path=""):
12
  # load the optimized model
13
  # create inference pipeline
14
- self.pipeline = pipeline("text-to-audio", "facebook/musicgen-stereo-large", device="cuda", torch_dtype=torch.float16)
15
 
16
  def generate_audio(self, text: str):
17
  # Here you can implement your audio generation logic
@@ -30,13 +30,9 @@ class EndpointHandler():
30
 
31
  audio_data, sampling_rate = self.generate_audio(input)
32
 
33
-
34
- # Convert audio data to base64 string
35
- audio_base64 = base64.b64encode(audio_data.tobytes())
36
-
37
  # Create JSON response
38
  response = {
39
- "audio_base64": audio_base64,
40
  "sampling_rate": sampling_rate
41
  }
42
 
 
11
  def __init__(self, path=""):
12
  # load the optimized model
13
  # create inference pipeline
14
+ self.pipeline = pipeline("text-to-audio", "facebook/musicgen-stereo-large", device="mps", torch_dtype=torch.float16)
15
 
16
  def generate_audio(self, text: str):
17
  # Here you can implement your audio generation logic
 
30
 
31
  audio_data, sampling_rate = self.generate_audio(input)
32
 
 
 
 
 
33
  # Create JSON response
34
  response = {
35
+ "audio_data": audio_data,
36
  "sampling_rate": sampling_rate
37
  }
38
 
output.wav ADDED
Binary file (648 kB). View file
 
test.py CHANGED
@@ -1,4 +1,7 @@
1
  from handler import EndpointHandler
 
 
 
2
 
3
  # init handler
4
  my_handler = EndpointHandler(path=".")
@@ -10,5 +13,11 @@ payload = {"inputs": "Lowfi hiphop with deep bass"}
10
  pred=my_handler(payload)
11
 
12
 
13
- with open("generated_audio.wav", "wb") as f:
14
- f.write(pred)
 
 
 
 
 
 
 
1
  from handler import EndpointHandler
2
+ import soundfile as sf
3
+ import numpy as np
4
+
5
 
6
  # init handler
7
  my_handler = EndpointHandler(path=".")
 
13
  pred=my_handler(payload)
14
 
15
 
16
+ audio_data = pred["audio_data"]
17
+ sampling_rate = pred["sampling_rate"]
18
+
19
+ # Write the audio data to a WAV file
20
+ output_file_path = "output.wav" # Specify the file path
21
+ sf.write(output_file_path, audio_data, sampling_rate)
22
+
23
+ print("Audio file saved successfully.")
test_api.py CHANGED
@@ -1,8 +1,9 @@
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",
@@ -18,18 +19,22 @@ response = query({
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)
 
1
  import requests
2
  import base64
3
  import soundfile as sf
4
+ import numpy as np
5
 
6
+ API_URL = "https://gij43le0roc2pmst.us-east-1.aws.endpoints.huggingface.cloud"
7
  headers = {
8
  "Accept" : "application/json",
9
  "Authorization": "Bearer token",
 
19
  "parameters": {}
20
  })
21
 
22
+ print(response)
23
+
24
  # Extract audio data and sampling rate from the JSON response
25
  audio_base64 = response["audio_base64"]
26
  sampling_rate = response["sampling_rate"]
27
 
28
  # Decode the base64-encoded audio data
29
  audio_binary = base64.b64decode(audio_base64)
30
+ # Convert binary audio data to a NumPy array
31
+ audio_np = np.frombuffer(audio_binary, dtype=np.int16)
32
+
33
+ # Print the shape of the audio data
34
+ print("Shape of audio data:", audio_np.shape)
35
 
36
  # Write the audio data to a WAV file
37
  output_file_path = "output.wav" # Specify the file path
38
+ sf.write(output_file_path, audio_np, sampling_rate)
39
 
40
+ print("Audio file saved successfully.")