civerson916 commited on
Commit
39f4053
·
verified ·
1 Parent(s): 01f6ec7

Update app.py

Browse files

Incorporating the code agent from the first assignment

Files changed (1) hide show
  1. app.py +54 -3
app.py CHANGED
@@ -16,14 +16,65 @@ 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
  class BasicAgent:
20
  def __init__(self):
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  print("BasicAgent initialized.")
22
  def __call__(self, question: str) -> str:
23
  print(f"Agent received question (first 50 chars): {question[:50]}...")
24
- fixed_answer = "This is a default answer."
25
- print(f"Agent returning fixed answer: {fixed_answer}")
26
- return fixed_answer
 
27
 
28
  def run_and_submit_all( profile: gr.OAuthProfile | None):
29
  """
 
16
 
17
  # --- Basic Agent Definition ---
18
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
19
+ final_answer = FinalAnswerTool()
20
+
21
+ model = LiteLLMModel(
22
+ model_id="gemini/gemini-2.0-flash",
23
+ temperature=0.7,
24
+ api_key=os.environ.get("GEMINI_KEY")
25
+ )
26
+
27
+ # Import tool from Hub
28
+ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
29
+
30
+ @tool
31
+ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
32
+ #Keep this format for the description / args / args description but feel free to modify the tool
33
+ """A tool that provides web search via duckduckgo
34
+ Args:
35
+ arg1: the first argument
36
+ arg2: the second argument
37
+ """
38
+ search_tool = DuckDuckGoSearchTool()
39
+ return search_tool(arg1)
40
+
41
+ @tool
42
+ def get_current_time_in_timezone(timezone: str) -> str:
43
+ """A tool that fetches the current local time in a specified timezone.
44
+ Args:
45
+ timezone: A string representing a valid timezone (e.g., 'America/New_York').
46
+ """
47
+ try:
48
+ # Create timezone object
49
+ tz = pytz.timezone(timezone)
50
+ # Get current time in that timezone
51
+ local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
52
+ return f"The current local time in {timezone} is: {local_time}"
53
+ except Exception as e:
54
+ return f"Error fetching time for timezone '{timezone}': {str(e)}"
55
+
56
  class BasicAgent:
57
  def __init__(self):
58
+ with open("prompts.yaml", 'r') as stream:
59
+ prompt_templates = yaml.safe_load(stream)
60
+ self.agent = CodeAgent(
61
+ model=model,
62
+ tools=[final_answer, my_custom_tool],
63
+ max_steps=6,
64
+ verbosity_level=1,
65
+ grammar=None,
66
+ planning_interval=None,
67
+ name=None,
68
+ description=None,
69
+ prompt_templates=prompt_templates
70
+ )
71
  print("BasicAgent initialized.")
72
  def __call__(self, question: str) -> str:
73
  print(f"Agent received question (first 50 chars): {question[:50]}...")
74
+ # fixed_answer = "This is a default answer."
75
+ final_answer = self.agent.run(question)
76
+ print(f"Agent returning fixed answer: {final_answer}")
77
+ return final_answer
78
 
79
  def run_and_submit_all( profile: gr.OAuthProfile | None):
80
  """