Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,19 @@
|
|
| 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 |
-
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
-
#my first try
|
| 11 |
@tool
|
| 12 |
-
def dog_average_longevity(breed: str)-> str:
|
| 13 |
"""A tool that uses DuckDuckGo to search the web and returns the average lifespan for a breed of dogs.
|
| 14 |
Args:
|
| 15 |
-
breed(str)
|
| 16 |
"""
|
| 17 |
query = f"average lifespan for {breed} dog in years"
|
| 18 |
|
|
@@ -31,28 +32,25 @@ def extract_lifespan(text: str) -> Optional[str]:
|
|
| 31 |
"""
|
| 32 |
Extracts the first occurrence of a lifespan range or single number in years.
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
return: Extracted lifespan range or single value (e.g., '10-12 years' or '13 years') or None.
|
| 37 |
"""
|
| 38 |
-
pattern = r"(\d{1,2}(
|
| 39 |
match = re.search(pattern, text, re.IGNORECASE)
|
| 40 |
|
| 41 |
if match:
|
| 42 |
return match.group(1)
|
| 43 |
return None
|
| 44 |
|
| 45 |
-
|
| 46 |
final_answer = FinalAnswerTool()
|
| 47 |
model = HfApiModel(
|
| 48 |
-
max_tokens=2096,
|
| 49 |
-
temperature=0.5,
|
| 50 |
-
model_id='https://wxknx1kg971u7k1n.us-east-1.aws.endpoints.huggingface.cloud'
|
| 51 |
-
custom_role_conversions=None,
|
| 52 |
)
|
| 53 |
|
| 54 |
-
|
| 55 |
-
# Import tool from Hub
|
| 56 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 57 |
|
| 58 |
with open("prompts.yaml", 'r') as stream:
|
|
@@ -60,7 +58,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 60 |
|
| 61 |
agent = CodeAgent(
|
| 62 |
model=model,
|
| 63 |
-
tools=[final_answer, dog_average_longevity],
|
| 64 |
max_steps=6,
|
| 65 |
verbosity_level=1,
|
| 66 |
grammar=None,
|
|
@@ -70,5 +68,4 @@ agent = CodeAgent(
|
|
| 70 |
prompt_templates=prompt_templates
|
| 71 |
)
|
| 72 |
|
| 73 |
-
|
| 74 |
-
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
+
import re
|
| 7 |
+
from typing import Optional
|
| 8 |
+
from duckduckgo_search import DDGS
|
| 9 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 10 |
from Gradio_UI import GradioUI
|
| 11 |
|
|
|
|
| 12 |
@tool
|
| 13 |
+
def dog_average_longevity(breed: str) -> str:
|
| 14 |
"""A tool that uses DuckDuckGo to search the web and returns the average lifespan for a breed of dogs.
|
| 15 |
Args:
|
| 16 |
+
breed(str): A string representing the breed of the dog (e.g., "Labrador").
|
| 17 |
"""
|
| 18 |
query = f"average lifespan for {breed} dog in years"
|
| 19 |
|
|
|
|
| 32 |
"""
|
| 33 |
Extracts the first occurrence of a lifespan range or single number in years.
|
| 34 |
|
| 35 |
+
:param text: The input text from search results.
|
| 36 |
+
:return: Extracted lifespan range or single value (e.g., '10-12 years' or '13 years') or None.
|
|
|
|
| 37 |
"""
|
| 38 |
+
pattern = r"(\d{1,2}\s?(-|to)\s?\d{1,2}|\d{1,2})\s?(years|yrs)?"
|
| 39 |
match = re.search(pattern, text, re.IGNORECASE)
|
| 40 |
|
| 41 |
if match:
|
| 42 |
return match.group(1)
|
| 43 |
return None
|
| 44 |
|
|
|
|
| 45 |
final_answer = FinalAnswerTool()
|
| 46 |
model = HfApiModel(
|
| 47 |
+
max_tokens=2096,
|
| 48 |
+
temperature=0.5,
|
| 49 |
+
model_id='https://wxknx1kg971u7k1n.us-east-1.aws.endpoints.huggingface.cloud',
|
| 50 |
+
custom_role_conversions=None,
|
| 51 |
)
|
| 52 |
|
| 53 |
+
# Import tool from Hugging Face Hub
|
|
|
|
| 54 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 55 |
|
| 56 |
with open("prompts.yaml", 'r') as stream:
|
|
|
|
| 58 |
|
| 59 |
agent = CodeAgent(
|
| 60 |
model=model,
|
| 61 |
+
tools=[final_answer, dog_average_longevity, image_generation_tool], # Now includes image generation
|
| 62 |
max_steps=6,
|
| 63 |
verbosity_level=1,
|
| 64 |
grammar=None,
|
|
|
|
| 68 |
prompt_templates=prompt_templates
|
| 69 |
)
|
| 70 |
|
| 71 |
+
GradioUI(agent).launch()
|
|
|