Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,96 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
if __name__ == "__main__":
|
| 5 |
-
demo.launch()
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, HfApiModel, load_tool, tool, VisitWebpageTool
|
| 2 |
+
import datetime
|
| 3 |
+
import requests
|
| 4 |
+
import pytz
|
| 5 |
+
import yaml
|
| 6 |
+
from tools.final_answer import FinalAnswerTool
|
| 7 |
+
from duckduckgo_search import DDGS
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
from Gradio_UI import GradioUI
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 14 |
+
@tool
|
| 15 |
+
def my_custom_tool(arg1: str, arg2: int) -> str: # it's important to specify the return type
|
| 16 |
+
"""A tool that does nothing yet
|
| 17 |
+
Args:
|
| 18 |
+
arg1: the first argument
|
| 19 |
+
arg2: the second argument
|
| 20 |
+
"""
|
| 21 |
+
return "What magic will you build ?"
|
| 22 |
+
|
| 23 |
+
@tool
|
| 24 |
+
def get_current_time_in_timezone(timezone: str) -> str:
|
| 25 |
+
"""A tool that fetches the current local time in a specified timezone.
|
| 26 |
+
Args:
|
| 27 |
+
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 28 |
+
"""
|
| 29 |
+
try:
|
| 30 |
+
# Create timezone object
|
| 31 |
+
tz = pytz.timezone(timezone)
|
| 32 |
+
# Get current time in that timezone
|
| 33 |
+
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 34 |
+
return f"The current local time in {timezone} is: {local_time}"
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 37 |
+
|
| 38 |
+
@tool
|
| 39 |
+
def ai_act_search_tool(query: str, max_results: int = 5) -> list:
|
| 40 |
+
"""Searches for highly relevant articles on the AI Act using DuckDuckGo.
|
| 41 |
+
Args:
|
| 42 |
+
query: The search query.
|
| 43 |
+
max_results: The maximum number of results to return.
|
| 44 |
+
Returns:
|
| 45 |
+
A list of URLs to relevant articles.
|
| 46 |
+
"""
|
| 47 |
+
with DDGS() as ddgs:
|
| 48 |
+
search_results = ddgs.text(query + " AI Act", max_results)
|
| 49 |
+
return [{"title": result["title"], "link": result["href"]} for result in search_results] if search_results else ["No relevant articles found."]
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
#@tool
|
| 54 |
+
#def fetch_url(url: str) -> str:
|
| 55 |
+
# """Fetches the content of a given URL and returns the raw text.
|
| 56 |
+
# Args:
|
| 57 |
+
# url: The URL to fetch content from.
|
| 58 |
+
# Returns:
|
| 59 |
+
# The raw text content of the page, or an error message if fetching fails.
|
| 60 |
+
# """
|
| 61 |
+
# response = requests.get(url)
|
| 62 |
+
# return response.text if response.status_code == 200 else "Failed to fetch content."
|
| 63 |
+
|
| 64 |
+
final_answer = FinalAnswerTool()
|
| 65 |
+
visit_webpage = VisitWebpageTool()
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 69 |
+
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 70 |
+
|
| 71 |
+
model = HfApiModel(
|
| 72 |
+
max_tokens=2096,
|
| 73 |
+
temperature=0.5,
|
| 74 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # it is possible that this model may be overloaded
|
| 75 |
+
custom_role_conversions=None,
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
# Import tool from Hub
|
| 79 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 80 |
+
|
| 81 |
+
with open("prompts.yaml", 'r') as stream:
|
| 82 |
+
prompt_templates = yaml.safe_load(stream)
|
| 83 |
+
|
| 84 |
+
agent = CodeAgent(
|
| 85 |
+
model=model,
|
| 86 |
+
tools=[final_answer, image_generation_tool, ai_act_search_tool, visit_webpage], # Added AI Act search tool
|
| 87 |
+
max_steps=6,
|
| 88 |
+
verbosity_level=1,
|
| 89 |
+
grammar=None,
|
| 90 |
+
planning_interval=None,
|
| 91 |
+
name=None,
|
| 92 |
+
description=None,
|
| 93 |
+
prompt_templates=prompt_templates
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
GradioUI(agent).launch()
|