uasername commited on
Commit
dbfe74a
·
verified ·
1 Parent(s): 13a73d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -1
app.py CHANGED
@@ -18,6 +18,38 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
18
  """
19
  return "What magic will you build ?"
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  @tool
23
  def search_dad_jokes(term: str) -> str:
@@ -98,7 +130,7 @@ with open("prompts.yaml", 'r') as stream:
98
 
99
  agent = CodeAgent(
100
  model=model,
101
- tools=[final_answer, image_generation_tool, get_current_time_in_timezone, get_random_cocktail, search_dad_jokes], ## add your tools here (don't remove final answer)
102
  max_steps=6,
103
  verbosity_level=1,
104
  grammar=None,
 
18
  """
19
  return "What magic will you build ?"
20
 
21
+ @tool
22
+ def text_to_speech_kokoro(text: str, voice: str = 'af_heart', lang_code: str = 'a') -> str:
23
+ """A tool that converts text to speech using the Kokoro-82M model.
24
+ Args:
25
+ text: The text to be converted to speech.
26
+ voice: The voice to use for speech synthesis (default is 'af_heart').
27
+ lang_code: The language code corresponding to the voice (default is 'a' for American English).
28
+ """
29
+ try:
30
+ # Import necessary libraries
31
+ from kokoro import KPipeline
32
+ import soundfile as sf
33
+ import os
34
+
35
+ # Initialize the Kokoro pipeline
36
+ pipeline = KPipeline(lang_code=lang_code)
37
+
38
+ # Generate speech audio
39
+ generator = pipeline(text, voice=voice, speed=1, split_pattern=r'\n+')
40
+ audio_files = []
41
+
42
+ # Save each audio segment to a file
43
+ for i, (gs, ps, audio) in enumerate(generator):
44
+ filename = f'output_{i}.wav'
45
+ sf.write(filename, audio, 24000)
46
+ audio_files.append(filename)
47
+
48
+ return f"Generated {len(audio_files)} audio file(s): {', '.join(audio_files)}"
49
+ except Exception as e:
50
+ return f"Error generating speech: {str(e)}"
51
+
52
+
53
 
54
  @tool
55
  def search_dad_jokes(term: str) -> str:
 
130
 
131
  agent = CodeAgent(
132
  model=model,
133
+ tools=[final_answer, image_generation_tool, get_current_time_in_timezone, get_random_cocktail, search_dad_jokes, text_to_speech_kokoro], ## add your tools here (don't remove final answer)
134
  max_steps=6,
135
  verbosity_level=1,
136
  grammar=None,