Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,14 +9,52 @@ from Gradio_UI import GradioUI
|
|
| 9 |
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
"""A tool that does nothing yet
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
arg2: the second argument
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
@@ -51,7 +89,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 51 |
|
| 52 |
agent = CodeAgent(
|
| 53 |
model=model,
|
| 54 |
-
tools=[final_answer],
|
| 55 |
max_steps=6,
|
| 56 |
verbosity_level=1,
|
| 57 |
grammar=None,
|
|
|
|
| 9 |
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
| 12 |
+
def find_capitals(country: str)-> str:
|
| 13 |
+
"""Finds the capital of a given country.
|
|
|
|
| 14 |
Args:
|
| 15 |
+
country: A string representing a valid country (e.g., 'Japan').
|
|
|
|
| 16 |
"""
|
| 17 |
+
try:
|
| 18 |
+
search_tool = DuckDuckGoSearchTool()
|
| 19 |
+
search_results = search_tool.run(f"capital of {country}")
|
| 20 |
+
|
| 21 |
+
for result in search_results:
|
| 22 |
+
# More robust regex parsing
|
| 23 |
+
patterns = [
|
| 24 |
+
r"(?:capital of|capital city of) " + re.escape(country) + r" is (.+?)\.", #Handles "capital of X is Y."
|
| 25 |
+
r"(.+?) is the (?:capital of|capital city of) " + re.escape(country) + r"\.", #Handles "Y is the capital of X."
|
| 26 |
+
r"Capital: (.+)", #Handles "Capital: Y"
|
| 27 |
+
r"Capital City: (.+)", #Handles "Capital City: Y"
|
| 28 |
+
r"(.+) \(capital\)", #Handles "Y (capital)"
|
| 29 |
+
r"(.+?) - Capital City", #Handles "Y - Capital City"
|
| 30 |
+
r"(.+?) – Capital", #Handles "Y – Capital"
|
| 31 |
+
r"^" + re.escape(country) + r" - (.+?) \(capital\)", #Handles "X - Y (capital)"
|
| 32 |
+
r"^" + re.escape(country) + r" - (.+?) Capital", #Handles "X - Y Capital"
|
| 33 |
+
r"^" + re.escape(country) + r" - (.+?) – Capital", #Handles "X - Y – Capital"
|
| 34 |
+
r"^" + re.escape(country) + r" : (.+?) Capital", #Handles "X : Y Capital"
|
| 35 |
+
r"^" + re.escape(country) + r" : (.+?) – Capital", #Handles "X : Y – Capital"
|
| 36 |
+
r"^" + re.escape(country) + r" : (.+?) \(capital\)", #Handles "X : Y (capital)"
|
| 37 |
+
|
| 38 |
+
]
|
| 39 |
+
for pattern in patterns:
|
| 40 |
+
match = re.search(pattern, result, re.IGNORECASE)
|
| 41 |
+
if match:
|
| 42 |
+
capital = match.group(1).strip()
|
| 43 |
+
return f"The capital of {country} is {capital}."
|
| 44 |
+
|
| 45 |
+
#If no clear pattern is found, try to extract based on the country name.
|
| 46 |
+
parts = result.split(country)
|
| 47 |
+
if len(parts) > 1:
|
| 48 |
+
potential_capital = parts[0].strip()
|
| 49 |
+
# Additional check to avoid false positives (e.g., if the country name appears later in the text).
|
| 50 |
+
if "capital" in result.lower() or "city" in result.lower():
|
| 51 |
+
return f"The capital of {country} is {potential_capital}."
|
| 52 |
+
|
| 53 |
+
return f"Could not find the capital of {country}."
|
| 54 |
+
|
| 55 |
+
except Exception as e:
|
| 56 |
+
return f"An error occurred: {e}"
|
| 57 |
+
|
| 58 |
|
| 59 |
@tool
|
| 60 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 89 |
|
| 90 |
agent = CodeAgent(
|
| 91 |
model=model,
|
| 92 |
+
tools=[final_answer],[get_current_time_in_timezone],[find_capitals]## add your tools here (don't remove final answer)
|
| 93 |
max_steps=6,
|
| 94 |
verbosity_level=1,
|
| 95 |
grammar=None,
|