Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,6 +15,37 @@ from tools.visit_webpage import VisitWebpageTool
|
|
| 15 |
from Code_Functions import speak_text
|
| 16 |
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# # Define the audio output path
|
| 20 |
# AUDIO_OUTPUT_PATH = "/tmp/response.mp3"
|
|
@@ -27,33 +58,34 @@ from Code_Functions import speak_text
|
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
-
@tool
|
| 31 |
-
def search_dad_jokes(term: str) -> str:
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
|
| 54 |
-
|
| 55 |
|
| 56 |
|
|
|
|
| 57 |
final_answer = FinalAnswerTool()
|
| 58 |
web_search_tool = DuckDuckGoSearchTool()
|
| 59 |
visit_webpage_tool = VisitWebpageTool()
|
|
@@ -79,7 +111,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 79 |
|
| 80 |
agent = CodeAgent(
|
| 81 |
model=model,
|
| 82 |
-
tools=[visit_webpage_tool, web_search_tool, final_answer, image_generation_tool,
|
| 83 |
max_steps=6,
|
| 84 |
verbosity_level=1,
|
| 85 |
grammar=None,
|
|
|
|
| 15 |
from Code_Functions import speak_text
|
| 16 |
|
| 17 |
|
| 18 |
+
from smolagents.agent_types import AgentText
|
| 19 |
+
|
| 20 |
+
@tool
|
| 21 |
+
def lookup_definition(query: str) -> AgentText:
|
| 22 |
+
"""Fetches the definition of a word from the Dictionary API and returns it as AgentText."""
|
| 23 |
+
url = f"https://api.dictionaryapi.dev/api/v2/entries/en/{query}"
|
| 24 |
+
try:
|
| 25 |
+
response = requests.get(url)
|
| 26 |
+
response.raise_for_status()
|
| 27 |
+
data = response.json()
|
| 28 |
+
|
| 29 |
+
if not data:
|
| 30 |
+
response_text = "No definition found."
|
| 31 |
+
else:
|
| 32 |
+
word = data[0].get("word", "Unknown word")
|
| 33 |
+
origin = data[0].get("origin", "Origin not available")
|
| 34 |
+
definitions = [
|
| 35 |
+
f"({meaning.get('partOfSpeech', 'Unknown')}) {definition['definition']}"
|
| 36 |
+
for meaning in data[0].get("meanings", [])
|
| 37 |
+
for definition in meaning.get("definitions", [])
|
| 38 |
+
]
|
| 39 |
+
if not definitions:
|
| 40 |
+
response_text = f"Word: {word}\nOrigin: {origin}\nNo definitions found."
|
| 41 |
+
else:
|
| 42 |
+
response_text = f"Word: {word}\nOrigin: {origin}\nDefinitions:\n- " + "\n- ".join(definitions)
|
| 43 |
+
except requests.RequestException as e:
|
| 44 |
+
response_text = f"Error fetching definition: {str(e)}"
|
| 45 |
+
|
| 46 |
+
return AgentText(response_text)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
|
| 50 |
# # Define the audio output path
|
| 51 |
# AUDIO_OUTPUT_PATH = "/tmp/response.mp3"
|
|
|
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
+
# @tool
|
| 62 |
+
# def search_dad_jokes(term: str) -> str:
|
| 63 |
+
# """A tool that searches for dad jokes containing a specific term.
|
| 64 |
+
# Args:
|
| 65 |
+
# term: The keyword to search for in dad jokes.
|
| 66 |
+
# """
|
| 67 |
+
# try:
|
| 68 |
+
# headers = {
|
| 69 |
+
# "Accept": "application/json",
|
| 70 |
+
# "User-Agent": "YourAppName (https://yourappurl.com)"
|
| 71 |
+
# }
|
| 72 |
+
# response = requests.get(f"https://icanhazdadjoke.com/search?term={term}", headers=headers)
|
| 73 |
+
# data = response.json()
|
| 74 |
+
# if data['results']:
|
| 75 |
+
# jokes = [joke['joke'] for joke in data['results']]
|
| 76 |
+
# response_text = f"Found {len(jokes)} jokes:\n" + "\n\n".join(jokes)
|
| 77 |
+
# else:
|
| 78 |
+
# response_text = f"No jokes found for the term '{term}'."
|
| 79 |
+
# except Exception as e:
|
| 80 |
+
# response_text = f"Error searching for jokes: {str(e)}"
|
| 81 |
|
| 82 |
+
# # Generate audio using gTTS
|
| 83 |
+
# audio_file = speak_text(response_text)
|
| 84 |
|
| 85 |
+
# return response_text, audio_file # Return text and audio file path
|
| 86 |
|
| 87 |
|
| 88 |
+
lookup_definition_tool = lookup_definition
|
| 89 |
final_answer = FinalAnswerTool()
|
| 90 |
web_search_tool = DuckDuckGoSearchTool()
|
| 91 |
visit_webpage_tool = VisitWebpageTool()
|
|
|
|
| 111 |
|
| 112 |
agent = CodeAgent(
|
| 113 |
model=model,
|
| 114 |
+
tools=[visit_webpage_tool, web_search_tool, final_answer, image_generation_tool, lookup_definition_tool, ## add your tools here (don't remove final answer)
|
| 115 |
max_steps=6,
|
| 116 |
verbosity_level=1,
|
| 117 |
grammar=None,
|