Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,35 +4,50 @@ import requests
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
@tool
|
| 12 |
-
def
|
| 13 |
"""
|
| 14 |
-
|
| 15 |
|
| 16 |
Args:
|
| 17 |
-
|
| 18 |
|
| 19 |
Returns:
|
| 20 |
-
bytes: The
|
| 21 |
"""
|
| 22 |
try:
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
-
return f"
|
| 35 |
|
|
|
|
| 36 |
@tool
|
| 37 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 38 |
"""A tool that fetches the current local time in a specified timezone.
|
|
@@ -70,7 +85,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 70 |
|
| 71 |
agent = CodeAgent(
|
| 72 |
model=model,
|
| 73 |
-
tools=[final_answer], ## add your tools here (don't remove final answer)
|
| 74 |
max_steps=6,
|
| 75 |
verbosity_level=1,
|
| 76 |
grammar=None,
|
|
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
+
import os
|
| 8 |
+
import scipy.io.wavfile
|
| 9 |
+
from transformers import BarkModel, BarkTokenizer
|
| 10 |
|
| 11 |
from Gradio_UI import GradioUI
|
| 12 |
|
| 13 |
+
# Load Bark model and tokenizer
|
| 14 |
+
tokenizer = BarkTokenizer.from_pretrained("suno/bark-small")
|
| 15 |
+
model = BarkModel.from_pretrained("suno/bark-small")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# Define the text-to-speech tool
|
| 19 |
@tool
|
| 20 |
+
def text_to_speech(text: str) -> bytes:
|
| 21 |
"""
|
| 22 |
+
Converts text to speech using the Bark model.
|
| 23 |
|
| 24 |
Args:
|
| 25 |
+
text: The text to be converted to speech.
|
| 26 |
|
| 27 |
Returns:
|
| 28 |
+
bytes: The audio data in WAV format.
|
| 29 |
"""
|
| 30 |
try:
|
| 31 |
+
inputs = tokenizer(text, return_tensors="pt")
|
| 32 |
+
audio_array = model.generate(**inputs)
|
| 33 |
+
# Convert the generated audio array to a NumPy array
|
| 34 |
+
audio_array = audio_array.cpu().numpy()
|
| 35 |
+
|
| 36 |
+
# Save the audio array as a WAV file
|
| 37 |
+
rate = 16000 # Bark model sample rate
|
| 38 |
+
filename = "output.wav"
|
| 39 |
+
scipy.io.wavfile.write(filename, rate, audio_array)
|
| 40 |
+
|
| 41 |
+
# Read the WAV file and convert it to bytes
|
| 42 |
+
with open(filename, "rb") as f:
|
| 43 |
+
audio_bytes = f.read()
|
| 44 |
+
|
| 45 |
+
return audio_bytes
|
| 46 |
+
|
| 47 |
except Exception as e:
|
| 48 |
+
return f"Error converting text to speech: {str(e)}".encode('utf-8')
|
| 49 |
|
| 50 |
+
# Define the get current time in timezone tool
|
| 51 |
@tool
|
| 52 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 53 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 85 |
|
| 86 |
agent = CodeAgent(
|
| 87 |
model=model,
|
| 88 |
+
tools=[final_answer, text_to_speech], ## add your tools here (don't remove final answer)
|
| 89 |
max_steps=6,
|
| 90 |
verbosity_level=1,
|
| 91 |
grammar=None,
|