sam9407287 commited on
Commit
ca94c97
·
verified ·
1 Parent(s): bae8644

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -17
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
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
 
 
 
 
 
11
  @tool
12
- def generate_image(prompt: str) -> bytes:
13
  """
14
- Generates an image from a text prompt using a Hugging Face model.
15
 
16
  Args:
17
- prompt: The text prompt to generate the image from. A detailed and descriptive prompt will yield better results. Consider including details about the style, composition, and subject of the image you want. For example: "A photorealistic image of a cat wearing a hat, sitting in a field of flowers, under a blue sky."
18
 
19
  Returns:
20
- bytes: The image data in bytes format, or an error message as bytes.
21
  """
22
  try:
23
- payload = {
24
- "inputs": prompt,
25
- }
26
- response = requests.post(API_URL, headers=headers, json=payload)
27
- response.raise_for_status()
28
-
29
- image = response.content
30
- return image
31
- except requests.exceptions.RequestException as e:
32
- return f"Error generating image: {str(e)}".encode('utf-8')
 
 
 
 
 
 
33
  except Exception as e:
34
- return f"An unexpected error occurred: {str(e)}".encode('utf-8')
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,