Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,51 @@
|
|
1 |
-
|
2 |
-
import datetime
|
3 |
import requests
|
4 |
-
import
|
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 |
-
|
13 |
-
|
14 |
-
import os
|
15 |
-
import numpy as np
|
16 |
|
17 |
-
|
|
|
|
|
|
|
|
|
18 |
|
19 |
|
20 |
-
# Initialize the Kokoro pipeline
|
21 |
-
pipeline = KPipeline(lang_code='a') # 'a' stands for American English
|
22 |
|
23 |
@tool
|
24 |
-
def
|
25 |
-
"""
|
26 |
-
|
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 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
if
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
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 |
-
|
|
|
|
|
|
|
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 |
|