uasername commited on
Commit
8b67e5c
·
verified ·
1 Parent(s): 35bea51

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -53
app.py CHANGED
@@ -80,46 +80,47 @@ def text_to_speech(text: str) -> AgentAudio:
80
  return AgentAudio(AUDIO_OUTPUT_PATH)
81
 
82
 
83
- # # Define the audio output path
84
- # AUDIO_OUTPUT_PATH = "/tmp/response.mp3"
85
-
86
- # def speak_text(text):
87
- # """Convert text to speech using gTTS and save as an MP3 file."""
88
- # tts = gTTS(text=text, lang='en')
89
- # tts.save(AUDIO_OUTPUT_PATH)
90
- # return AUDIO_OUTPUT_PATH # Return the file path for Gradio Audio component
91
-
92
-
93
-
94
- # @tool
95
- # def search_dad_jokes(term: str) -> str:
96
- # """A tool that searches for dad jokes containing a specific term.
97
- # Args:
98
- # term: The keyword to search for in dad jokes.
99
- # """
100
- # try:
101
- # headers = {
102
- # "Accept": "application/json",
103
- # "User-Agent": "YourAppName (https://yourappurl.com)"
104
- # }
105
- # response = requests.get(f"https://icanhazdadjoke.com/search?term={term}", headers=headers)
106
- # data = response.json()
107
- # if data['results']:
108
- # jokes = [joke['joke'] for joke in data['results']]
109
- # response_text = f"Found {len(jokes)} jokes:\n" + "\n\n".join(jokes)
110
- # else:
111
- # response_text = f"No jokes found for the term '{term}'."
112
- # except Exception as e:
113
- # response_text = f"Error searching for jokes: {str(e)}"
114
-
115
- # # Generate audio using gTTS
116
- # audio_file = speak_text(response_text)
117
-
118
- # return response_text, audio_file # Return text and audio file path
119
 
120
 
121
  lookup_definition_tool = lookup_definition
122
  text_to_speech_tool = text_to_speech
 
123
 
124
  final_answer = FinalAnswerTool()
125
  web_search_tool = DuckDuckGoSearchTool()
@@ -146,7 +147,7 @@ with open("prompts.yaml", 'r') as stream:
146
 
147
  agent = CodeAgent(
148
  model=model,
149
- tools=[visit_webpage_tool, web_search_tool, final_answer, image_generation_tool, lookup_definition_tool, text_to_speech_tool], ## add your tools here (don't remove final answer)
150
  max_steps=6,
151
  verbosity_level=1,
152
  grammar=None,
@@ -156,29 +157,29 @@ agent = CodeAgent(
156
  prompt_templates=prompt_templates
157
  )
158
 
159
- # #Gradio interface with text and audio output
160
- # def gradio_search_jokes(word):
161
- # """Wrapper function for Gradio to call search_dad_jokes and generate audio."""
162
- # response_text, audio_file = search_dad_jokes(word) # Ensure search_dad_jokes returns (text, file path)
163
- # return response_text, audio_file
164
 
165
 
166
 
167
- # #Define the Gradio UI
168
- # with gr.Blocks() as demo:
169
- # gr.Markdown("### Dad Jokes Finder with AI & Text-to-Speech 🎙️")
170
 
171
- # with gr.Row():
172
- # input_box = gr.Textbox(label="Enter a word")
173
- # output_text = gr.Textbox(label="Jokes Found")
174
- # output_audio = gr.Audio(label="Audio Pronunciation", type="filepath")
175
 
176
- # btn = gr.Button("Get Jokes")
177
- # btn.click(gradio_search_jokes, inputs=input_box, outputs=[output_text, output_audio])
178
 
179
- # demo.launch()
180
 
181
 
182
- GradioUI(agent).launch()
183
 
184
 
 
80
  return AgentAudio(AUDIO_OUTPUT_PATH)
81
 
82
 
83
+ # Define the audio output path
84
+ AUDIO_OUTPUT_PATH = "/tmp/response.mp3"
85
+
86
+ def speak_text(text):
87
+ """Convert text to speech using gTTS and save as an MP3 file."""
88
+ tts = gTTS(text=text, lang='en')
89
+ tts.save(AUDIO_OUTPUT_PATH)
90
+ return AUDIO_OUTPUT_PATH # Return the file path for Gradio Audio component
91
+
92
+
93
+
94
+ @tool
95
+ def search_dad_jokes(term: str) -> str:
96
+ """A tool that searches for dad jokes containing a specific term.
97
+ Args:
98
+ term: The keyword to search for in dad jokes.
99
+ """
100
+ try:
101
+ headers = {
102
+ "Accept": "application/json",
103
+ "User-Agent": "YourAppName (https://yourappurl.com)"
104
+ }
105
+ response = requests.get(f"https://icanhazdadjoke.com/search?term={term}", headers=headers)
106
+ data = response.json()
107
+ if data['results']:
108
+ jokes = [joke['joke'] for joke in data['results']]
109
+ response_text = f"Found {len(jokes)} jokes:\n" + "\n\n".join(jokes)
110
+ else:
111
+ response_text = f"No jokes found for the term '{term}'."
112
+ except Exception as e:
113
+ response_text = f"Error searching for jokes: {str(e)}"
114
+
115
+ # Generate audio using gTTS
116
+ audio_file = speak_text(response_text)
117
+
118
+ return response_text, audio_file # Return text and audio file path
119
 
120
 
121
  lookup_definition_tool = lookup_definition
122
  text_to_speech_tool = text_to_speech
123
+ dad_jokes_tool = search_dad_jokes
124
 
125
  final_answer = FinalAnswerTool()
126
  web_search_tool = DuckDuckGoSearchTool()
 
147
 
148
  agent = CodeAgent(
149
  model=model,
150
+ tools=[visit_webpage_tool, web_search_tool, final_answer, image_generation_tool, lookup_definition_tool, text_to_speech_tool, dad_jokes_tool], ## add your tools here (don't remove final answer)
151
  max_steps=6,
152
  verbosity_level=1,
153
  grammar=None,
 
157
  prompt_templates=prompt_templates
158
  )
159
 
160
+ #Gradio interface with text and audio output
161
+ def gradio_search_jokes(word):
162
+ """Wrapper function for Gradio to call search_dad_jokes and generate audio."""
163
+ response_text, audio_file = search_dad_jokes(word) # Ensure search_dad_jokes returns (text, file path)
164
+ return response_text, audio_file
165
 
166
 
167
 
168
+ #Define the Gradio UI
169
+ with gr.Blocks() as demo:
170
+ gr.Markdown("### Dad Jokes Finder with AI & Text-to-Speech 🎙️")
171
 
172
+ with gr.Row():
173
+ input_box = gr.Textbox(label="Enter a word")
174
+ output_text = gr.Textbox(label="Jokes Found")
175
+ output_audio = gr.Audio(label="Audio Pronunciation", type="filepath")
176
 
177
+ btn = gr.Button("Get Jokes")
178
+ btn.click(gradio_search_jokes, inputs=input_box, outputs=[output_text, output_audio])
179
 
180
+ demo.launch()
181
 
182
 
183
+ # GradioUI(agent).launch()
184
 
185