a-ge commited on
Commit
cd1bcc2
·
verified ·
1 Parent(s): 635b1ce

Rename new_agent.py to agent.py

Browse files
Files changed (1) hide show
  1. new_agent.py → agent.py +35 -20
new_agent.py → agent.py RENAMED
@@ -1,25 +1,42 @@
1
  import base64
 
2
  import os
3
  import gradio as gr
4
  import requests
5
  import pandas as pd
 
 
6
  from dotenv import load_dotenv
7
  from smolagents import (
8
  InferenceClientModel,
9
  CodeAgent,
 
10
  VisitWebpageTool,
11
  DuckDuckGoSearchTool,
12
  WikipediaSearchTool,
13
- tool,
14
  )
15
- from langchain_community.document_loaders import ArxivLoader
16
 
17
  load_dotenv()
18
 
19
  token = os.environ["HF_KEY"]
20
- # model_id = "deepseek-ai/DeepSeek-R1-0528"
21
- model_id = "meta-llama/Llama-3.3-70B-Instruct"
22
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  # (Keep Constants as is)
25
  # --- Constants ---
@@ -30,7 +47,9 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
30
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
31
  class BasicAgent:
32
  def __init__(self):
33
- model = InferenceClientModel(model_id=model_id, provider="nebius", token=token)
 
 
34
  self.agent = CodeAgent(
35
  tools=[
36
  VisitWebpageTool(),
@@ -38,6 +57,7 @@ class BasicAgent:
38
  WikipediaSearchTool(),
39
  ],
40
  model=model,
 
41
  additional_authorized_imports=[
42
  "pandas",
43
  "numpy",
@@ -71,27 +91,22 @@ class BasicAgent:
71
  try:
72
  if file_path.endswith(".xlsx"):
73
  df = pd.read_excel(file_path)
74
- file_info = f"\n[Attached Excel file preview]:\n{df.head(500).to_string(index=False)}\n"
75
  elif file_path.endswith(".py"):
76
- with open(file_path, "r", encoding="utf-8") as f:
77
- code = "".join([line for _, line in zip(range(500), f)])
78
- file_info = f"\n[Attached Python file]:\n{code}\n"
79
- elif file_path.endswith(".png"):
80
- with open(file_path, "rb") as img_file:
81
- b64_string = base64.b64encode(img_file.read()).decode("utf-8")
82
- file_info = f"\n[Attached PNG image as base64]:\n{b64_string}\n"
83
  else:
84
  with open(file_path, "r", encoding="utf-8") as f:
85
- content = f.read(2000)
86
- file_info = (
87
- f"\n[Attached file content (first 2000 chars)]:\n{content}\n"
88
- )
89
  except Exception as e:
90
- file_info = f"\n[Could not read attached file: {e}]\n"
91
  answer = self.agent.run(
92
  f"""You are a general AI assistant. You can use the provided tools and websearch for finding answers. Some questions may include attached files like excel, python code, or images. Include them while evaluating for answer. I will ask you a question. Report your thoughts, and finish your answer. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
93
  {question}
94
- {file_info}
95
  """,
96
  )
97
  print(f"Agent returning answer: {answer}")
 
1
  import base64
2
+ from pathlib import Path
3
  import os
4
  import gradio as gr
5
  import requests
6
  import pandas as pd
7
+ from PIL import Image
8
+ from io import BytesIO
9
  from dotenv import load_dotenv
10
  from smolagents import (
11
  InferenceClientModel,
12
  CodeAgent,
13
+ ToolCallingAgent,
14
  VisitWebpageTool,
15
  DuckDuckGoSearchTool,
16
  WikipediaSearchTool,
 
17
  )
18
+ from smolagents.utils import encode_image_base64, make_image_url
19
 
20
  load_dotenv()
21
 
22
  token = os.environ["HF_KEY"]
23
+ # model_id = "meta-llama/Llama-3.3-70B-Instruct"
24
+
25
+ manager_model = "deepseek-ai/DeepSeek-R1-0528"
26
+ web_model = "meta-llama/Llama-3.3-70B-Instruct"
27
+
28
+ web_agent = ToolCallingAgent(
29
+ model=InferenceClientModel(model_id=web_model, provider="nebius", token=token),
30
+ tools=[
31
+ VisitWebpageTool(),
32
+ DuckDuckGoSearchTool(),
33
+ WikipediaSearchTool(),
34
+ ],
35
+ name="web_agent",
36
+ description="Browses the web to find information",
37
+ verbosity_level=0,
38
+ max_steps=10,
39
+ )
40
 
41
  # (Keep Constants as is)
42
  # --- Constants ---
 
47
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
48
  class BasicAgent:
49
  def __init__(self):
50
+ model = InferenceClientModel(
51
+ model_id=manager_model, provider="nebius", token=token
52
+ )
53
  self.agent = CodeAgent(
54
  tools=[
55
  VisitWebpageTool(),
 
57
  WikipediaSearchTool(),
58
  ],
59
  model=model,
60
+ managed_agents=[web_agent],
61
  additional_authorized_imports=[
62
  "pandas",
63
  "numpy",
 
91
  try:
92
  if file_path.endswith(".xlsx"):
93
  df = pd.read_excel(file_path)
94
+ file = df.to_string()
95
  elif file_path.endswith(".py"):
96
+ file = Path(file_path).read_text()
97
+ # elif file_path.endswith(".png"):
98
+ # with open(file_path, "rb") as img_file:
99
+ # image = Image.open(BytesIO(img_file))
100
+ # file = image
 
 
101
  else:
102
  with open(file_path, "r", encoding="utf-8") as f:
103
+ file = f.read(2000)
 
 
 
104
  except Exception as e:
105
+ file = f"\n[Could not read attached file: {e}]\n, Skip the question and move to next one"
106
  answer = self.agent.run(
107
  f"""You are a general AI assistant. You can use the provided tools and websearch for finding answers. Some questions may include attached files like excel, python code, or images. Include them while evaluating for answer. I will ask you a question. Report your thoughts, and finish your answer. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
108
  {question}
109
+ additional_args={"file":{file}}
110
  """,
111
  )
112
  print(f"Agent returning answer: {answer}")