uasername commited on
Commit
798b3c8
·
verified ·
1 Parent(s): 0b7ec94

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -11
app.py CHANGED
@@ -18,6 +18,11 @@ from Code_Functions import speak_text
18
  from smolagents.agent_types import AgentText
19
  from smolagents.agent_types import AgentAudio
20
 
 
 
 
 
 
21
  @tool
22
  def lookup_definition(query: str) -> AgentText:
23
  """Fetches the definition of a word from the Dictionary API and returns it as AgentText.
@@ -69,22 +74,15 @@ def text_to_speech(text: str) -> AgentAudio:
69
  AgentAudio: An AgentAudio instance containing the file path to the generated audio.
70
  """
71
  from gtts import gTTS
72
- import os
73
-
74
  AUDIO_OUTPUT_PATH = "/tmp/response.mp3"
75
-
76
  tts = gTTS(text=text, lang='en')
77
  tts.save(AUDIO_OUTPUT_PATH)
78
-
79
- # Read the MP3 bytes directly
80
  with open(AUDIO_OUTPUT_PATH, "rb") as f:
81
  audio_bytes = f.read()
82
-
83
- #return AgentAudio(AUDIO_OUTPUT_PATH)
84
-
85
-
86
- # Return AgentAudio, but store the raw bytes
87
- return AgentAudio(audio_bytes) # Not a path anymore
88
 
89
 
90
  # # Define the audio output path
 
18
  from smolagents.agent_types import AgentText
19
  from smolagents.agent_types import AgentAudio
20
 
21
+ import soundfile
22
+ import io
23
+ import librosa
24
+ import numpy as np
25
+
26
  @tool
27
  def lookup_definition(query: str) -> AgentText:
28
  """Fetches the definition of a word from the Dictionary API and returns it as AgentText.
 
74
  AgentAudio: An AgentAudio instance containing the file path to the generated audio.
75
  """
76
  from gtts import gTTS
 
 
77
  AUDIO_OUTPUT_PATH = "/tmp/response.mp3"
 
78
  tts = gTTS(text=text, lang='en')
79
  tts.save(AUDIO_OUTPUT_PATH)
 
 
80
  with open(AUDIO_OUTPUT_PATH, "rb") as f:
81
  audio_bytes = f.read()
82
+ # Convert the MP3 bytes to a numpy array using librosa.
83
+ # sr=None preserves the original sample rate.
84
+ audio_np, sr = librosa.load(io.BytesIO(audio_bytes), sr=None)
85
+ return AgentAudio(audio_np)
 
 
86
 
87
 
88
  # # Define the audio output path