uasername commited on
Commit
d2dc763
·
verified ·
1 Parent(s): 1aa36e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -48
app.py CHANGED
@@ -1,64 +1,51 @@
1
- from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
- import datetime
3
  import requests
4
- import pytz
5
  import yaml
 
 
 
 
 
6
  from tools.final_answer import FinalAnswerTool
7
- from tools.web_search import DuckDuckGoSearchTool
8
- from tools.visit_webpage import VisitWebpageTool
9
-
10
  from Gradio_UI import GradioUI
11
 
12
- from kokoro import KPipeline
13
- import soundfile as sf
14
- import os
15
- import numpy as np
16
 
17
- import gradio as gr
 
 
 
 
18
 
19
 
20
- # Initialize the Kokoro pipeline
21
- pipeline = KPipeline(lang_code='a') # 'a' stands for American English
22
 
23
  @tool
24
- def text_to_speech_kokoro(text: str, voice: str = 'af_heart', speed: float = 1.0) -> str:
25
- """
26
- Convert text to speech using the Kokoro-82M model.
27
-
28
- Parameters:
29
- text (str):
30
- The text to be converted to speech.
31
- voice (str, optional):
32
- The voice to use for speech synthesis. Defaults to 'af_heart'.
33
- speed (float, optional):
34
- The speed of the speech. Defaults to 1.0.
35
-
36
- Returns:
37
- str: The path to the generated audio file.
38
  """
39
  try:
40
- # Generate speech audio
41
- generator = pipeline(text, voice=voice, speed=speed, split_pattern=r'\n+')
42
- audio_segments = []
43
- for _, _, audio in generator:
44
- audio_segments.append(audio)
45
-
46
- if not audio_segments:
47
- raise ValueError("No audio generated.")
48
-
49
- # Concatenate segments into one audio array
50
- full_audio = np.concatenate(audio_segments)
51
- sample_rate = 24000 # Kokoro outputs at 24 kHz
52
-
53
- # Ensure the tools folder exists and save the file there
54
- os.makedirs("tools", exist_ok=True)
55
- filename = os.path.join("tools", "output.wav")
56
- sf.write(filename, full_audio, sample_rate)
57
-
58
- return filename # Return the file path
59
  except Exception as e:
60
- return f"Error generating speech: {str(e)}"
 
 
 
61
 
 
62
 
63
 
64
  final_answer = FinalAnswerTool()
@@ -96,7 +83,26 @@ agent = CodeAgent(
96
  prompt_templates=prompt_templates
97
  )
98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
 
100
- GradioUI(agent).launch()
101
 
102
 
 
1
+ import os
 
2
  import requests
3
+ import random
4
  import yaml
5
+ import datetime
6
+ import pytz
7
+ import gradio as gr
8
+ from gtts import gTTS # Use Google TTS instead of pyttsx3
9
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
10
  from tools.final_answer import FinalAnswerTool
 
 
 
11
  from Gradio_UI import GradioUI
12
 
13
+ # Define the audio output path
14
+ AUDIO_OUTPUT_PATH = "/tmp/response.mp3"
 
 
15
 
16
+ def speak_text(text):
17
+ """Convert text to speech using gTTS and save as an MP3 file."""
18
+ tts = gTTS(text=text, lang='en')
19
+ tts.save(AUDIO_OUTPUT_PATH)
20
+ return AUDIO_OUTPUT_PATH # Return the file path for Gradio Audio component
21
 
22
 
 
 
23
 
24
  @tool
25
+ def search_dad_jokes(term: str) -> str:
26
+ """A tool that searches for dad jokes containing a specific term.
27
+ Args:
28
+ term: The keyword to search for in dad jokes.
 
 
 
 
 
 
 
 
 
 
29
  """
30
  try:
31
+ headers = {
32
+ "Accept": "application/json",
33
+ "User-Agent": "YourAppName (https://yourappurl.com)"
34
+ }
35
+ response = requests.get(f"https://icanhazdadjoke.com/search?term={term}", headers=headers)
36
+ data = response.json()
37
+ if data['results']:
38
+ jokes = [joke['joke'] for joke in data['results']]
39
+ response_text = f"Found {len(jokes)} jokes:\n" + "\n\n".join(jokes)
40
+ else:
41
+ response_text = f"No jokes found for the term '{term}'."
 
 
 
 
 
 
 
 
42
  except Exception as e:
43
+ response_text = f"Error searching for jokes: {str(e)}"
44
+
45
+ # Generate audio using gTTS
46
+ audio_file = speak_text(response_text)
47
 
48
+ return response_text, audio_file # Return text and audio file path
49
 
50
 
51
  final_answer = FinalAnswerTool()
 
83
  prompt_templates=prompt_templates
84
  )
85
 
86
+ # Gradio interface with text and audio output
87
+ def gradio_define_word(word):
88
+ """Wrapper function for Gradio to call the define_word tool."""
89
+ response_text, audio_file = define_word(word)
90
+ return response_text, audio_file
91
+
92
+ # Define the Gradio UI
93
+ with gr.Blocks() as demo:
94
+ gr.Markdown("### Dictionary Lookup with AI & Text-to-Speech 🎙️")
95
+ with gr.Row():
96
+ input_box = gr.Textbox(label="Enter a word")
97
+ output_text = gr.Textbox(label="Definition")
98
+ output_audio = gr.Audio(label="Audio Pronunciation", type="filepath")
99
+
100
+ btn = gr.Button("Get Definition")
101
+ btn.click(gradio_define_word, inputs=input_box, outputs=[output_text, output_audio])
102
+
103
+ demo.launch()
104
+
105
 
106
+ #GradioUI(agent).launch()
107
 
108