wlchee commited on
Commit
f94ee4d
·
verified ·
1 Parent(s): 405700e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -14
app.py CHANGED
@@ -4,38 +4,75 @@ import requests
4
  import inspect
5
  import pytz
6
  import datetime
 
7
 
8
  import pandas as pd
9
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
10
 
 
11
  # (Keep Constants as is)
12
  # --- Constants ---
13
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
14
 
15
  # --- Basic Agent Definition ---
16
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  class BasicAgent:
18
  def __init__(self):
19
  model = HfApiModel(
20
  max_tokens=2096,
21
- temperature=0.5,
22
  model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
23
  custom_role_conversions=None,
24
  )
25
 
26
- def get_current_time_in_timezone(timezone: str) -> str:
27
- """A tool that fetches the current local time in a specified timezone.
28
- Args:
29
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
30
- """
31
- try:
32
- # Create timezone object
33
- tz = pytz.timezone(timezone)
34
- # Get current time in that timezone
35
- local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
36
- return f"The current local time in {timezone} is: {local_time}"
37
- except Exception as e:
38
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
 
 
39
 
40
  def __call__(self, question: str) -> str:
41
  print(f"Agent received question (first 50 chars): {question[:50]}...")
 
4
  import inspect
5
  import pytz
6
  import datetime
7
+ import wikipedia
8
 
9
  import pandas as pd
10
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
11
 
12
+
13
  # (Keep Constants as is)
14
  # --- Constants ---
15
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
16
 
17
  # --- Basic Agent Definition ---
18
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
19
+
20
+ @tool
21
+ def get_current_time_in_timezone(timezone: str) -> str:
22
+ """Fetches the current local time in a specified timezone.
23
+ Args:
24
+ timezone: A string representing a valid timezone (e.g., 'America/New_York').
25
+ """
26
+ try:
27
+ tz = pytz.timezone(timezone)
28
+ local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
29
+ return f"The current local time in {timezone} is: {local_time}"
30
+ except Exception as e:
31
+ return f"Error fetching time for timezone '{timezone}': {str(e)}"
32
+
33
+
34
+ @tool
35
+ def wikipedia_search(query: str, sentences: int = 2) -> str:
36
+ """Searches Wikipedia for information.
37
+ Args:
38
+ query: The search term.
39
+ sentences: Number of summary sentences to return (default: 2).
40
+ """
41
+ try:
42
+ return wikipedia.summary(query, sentences=sentences)
43
+ except wikipedia.exceptions.DisambiguationError as e:
44
+ return f"Disambiguation error: {e.options}"
45
+ except wikipedia.exceptions.PageError:
46
+ return "No Wikipedia page found for this query."
47
+ except Exception as e:
48
+ return f"Wikipedia search failed: {str(e)}"
49
+
50
+
51
+
52
  class BasicAgent:
53
  def __init__(self):
54
  model = HfApiModel(
55
  max_tokens=2096,
56
+ temperature=0.3,
57
  model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
58
  custom_role_conversions=None,
59
  )
60
 
61
+ self.search_tool = DuckDuckGoSearchTool()
62
+ self.tools = {
63
+ "current_time": get_current_time_in_timezone,
64
+ "wikipedia": wikipedia_search,
65
+ "search": self.search_tool
66
+ }
67
+ self.agent = CodeAgent(
68
+ model=self.model,
69
+ tools=self.tools,
70
+ system_prompt="You are a helpful AI assistant with access to various tools. "
71
+ "Analyze questions carefully and use the appropriate tools when needed. "
72
+ "Provide clear, concise, and accurate answers."
73
+ )
74
+
75
+
76
 
77
  def __call__(self, question: str) -> str:
78
  print(f"Agent received question (first 50 chars): {question[:50]}...")