MarcoM003 commited on
Commit
d09fe2b
·
verified ·
1 Parent(s): 563539b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -32
app.py CHANGED
@@ -1,57 +1,81 @@
1
- from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
 
 
 
2
  import yaml
3
  from tools.final_answer import FinalAnswerTool
4
- from tools.visit_webpage import VisitWebpageTool
5
- from typing import Optional
6
- from PIL import Image
7
- from io import BytesIO
8
 
9
  from Gradio_UI import GradioUI
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  @tool
12
- def gimme_a_meme(subreddit: str = 'me_irl') -> Image:
13
- """A tool that grabs a random meme from a given subreddit
14
  Args:
15
- subreddit: a string representing a valid subreddit (e.g., 'dankmemes', 'memes', 'me_irl') default value: 'me_irl'
16
  """
17
  try:
18
- import requests
19
- url = f"https://meme-api.com/gimme/{subreddit}"
20
- response = requests.get(url)
21
- response.raise_for_status()
 
 
 
22
 
23
- meme_data = response.json()
24
- image_url = meme_data.get("url")
25
- image_response = requests.get(image_url)
26
- image_response.raise_for_status()
27
 
28
- img = Image.open(BytesIO(image_response.content))
29
- return img
30
- except requests.exceptions.RequestException as e:
31
- print(f"Error fetching meme: {e}")
32
- return None
33
 
 
 
34
 
35
- final_answer = FinalAnswerTool()
36
- visit_webpage = VisitWebpageTool()
 
 
 
 
37
 
38
  model = HfApiModel(
39
- max_tokens=2096,
40
- temperature=0.1,
41
- #model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
42
- model_id="https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud",
43
- custom_role_conversions=None
44
  )
45
 
46
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
 
47
 
48
  with open("prompts.yaml", 'r') as stream:
49
  prompt_templates = yaml.safe_load(stream)
50
-
51
  agent = CodeAgent(
52
  model=model,
53
- tools=[final_answer, DuckDuckGoSearchTool(), gimme_a_meme, image_generation_tool, visit_webpage],
54
- max_steps=4,
55
  verbosity_level=1,
56
  grammar=None,
57
  planning_interval=None,
 
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
+ # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
+ # @tool
12
+ # def search_ddgs(query:str, max_results:int=5)-> list: #it's import to specify the return type
13
+ # #Keep this format for the description / args / args description but feel free to modify the tool
14
+ # """A tool that searches the internet to answer user's question and returns top research results.
15
+
16
+ # Args:
17
+ # query: The search query
18
+ # max_results: Number of results to return. Maximum number is 5.
19
+
20
+ # Returns:
21
+ # List of dictionaries with title, URL, and description.
22
+ # """
23
+ # results = []
24
+ # with DuckDuckGoSearchTool as ddgs:
25
+ # for result in ddgs.text(query, max_results = max_results):
26
+ # results.append({
27
+ # "title": result["title"],
28
+ # "url": result["href"],
29
+ # "snippet":result["body"]
30
+ # })
31
+
32
+ # return results
33
+
34
  @tool
35
+ def get_current_time_in_timezone(timezone: str) -> str:
36
+ """A tool that fetches the current local time in a specified timezone.
37
  Args:
38
+ timezone: A string representing a valid timezone (e.g., 'America/New_York').
39
  """
40
  try:
41
+ # Create timezone object
42
+ tz = pytz.timezone(timezone)
43
+ # Get current time in that timezone
44
+ local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
45
+ return f"The current local time in {timezone} is: {local_time}"
46
+ except Exception as e:
47
+ return f"Error fetching time for timezone '{timezone}': {str(e)}"
48
 
 
 
 
 
49
 
50
+ final_answer = FinalAnswerTool()
 
 
 
 
51
 
52
+ # Import tool from Hub
53
+ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
54
 
55
+ # Search tool
56
+ search_tool = DuckDuckGoSearchTool()
57
+ # search_tool = DuckDuckGoSearchTool(max_results=3, output_format="json")
58
+
59
+ # 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:
60
+ # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
61
 
62
  model = HfApiModel(
63
+ max_tokens=2096,
64
+ temperature=0.5,
65
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
66
+ custom_role_conversions=None,
 
67
  )
68
 
69
+
70
+
71
 
72
  with open("prompts.yaml", 'r') as stream:
73
  prompt_templates = yaml.safe_load(stream)
74
+
75
  agent = CodeAgent(
76
  model=model,
77
+ tools=[final_answer, get_current_time_in_timezone, search_tool, image_generation_tool], ## add your tools here (don't remove final answer)
78
+ max_steps=6,
79
  verbosity_level=1,
80
  grammar=None,
81
  planning_interval=None,