ffreemt commited on
Commit
54a110c
·
1 Parent(s): 20dd971

Update get_model get_gemini_keys

Browse files
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .env-gemini
__pycache__/basic_agent.cpython-312.pyc ADDED
Binary file (6.79 kB). View file
 
__pycache__/get_gemini_keys.cpython-312.pyc ADDED
Binary file (1.65 kB). View file
 
__pycache__/get_model.cpython-312.pyc ADDED
Binary file (2.71 kB). View file
 
app.py CHANGED
@@ -1,17 +1,15 @@
 
 
1
  import os
 
2
  import gradio as gr
3
- import requests
4
- import inspect
5
  import pandas as pd
6
- from ycecream import y
7
-
8
- from smolagents import ToolCollection, CodeAgent, LiteLLMRouterModel, VisitWebpageTool, Tool
9
-
10
  import wikipediaapi
11
-
12
- from mcp import StdioServerParameters
13
-
14
  from basic_agent import BasicAgent
 
 
 
15
 
16
  y.configure(sln=1)
17
 
@@ -37,10 +35,7 @@ class BasicAgent:
37
  # """
38
 
39
  def run_and_submit_all( profile: gr.OAuthProfile | None):
40
- """
41
- Fetches all questions, runs the BasicAgent on them, submits all answers,
42
- and displays the results.
43
- """
44
  # --- Determine HF Space Runtime URL and Repo URL ---
45
  space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
46
 
@@ -211,4 +206,4 @@ if __name__ == "__main__":
211
  print("-"*(60 + len(" App Starting ")) + "\n")
212
 
213
  print("Launching Gradio Interface for Basic Agent Evaluation...")
214
- demo.launch(debug=True, share=False)
 
1
+ # ruff: noqa: F401
2
+ import inspect
3
  import os
4
+
5
  import gradio as gr
 
 
6
  import pandas as pd
7
+ import requests
 
 
 
8
  import wikipediaapi
 
 
 
9
  from basic_agent import BasicAgent
10
+ from mcp import StdioServerParameters
11
+ from smolagents import CodeAgent, LiteLLMRouterModel, Tool, ToolCollection, VisitWebpageTool
12
+ from ycecream import y
13
 
14
  y.configure(sln=1)
15
 
 
35
  # """
36
 
37
  def run_and_submit_all( profile: gr.OAuthProfile | None):
38
+ """Fetch all questions, run the BasicAgent on them, submit all answers, and display the results."""
 
 
 
39
  # --- Determine HF Space Runtime URL and Repo URL ---
40
  space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
41
 
 
206
  print("-"*(60 + len(" App Starting ")) + "\n")
207
 
208
  print("Launching Gradio Interface for Basic Agent Evaluation...")
209
+ demo.launch(debug=True, share=False)
basic_agent.py CHANGED
@@ -1,40 +1,114 @@
 
 
 
 
 
 
 
 
 
1
  import rich
2
  from loguru import logger
3
- import requests
 
 
 
4
 
5
  print = rich.get_console().print
6
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
 
8
  class BasicAgent:
9
- def __init__(self):
 
 
 
 
10
  logger.debug("BasicAgent initialized.")
 
 
 
 
 
 
 
 
 
 
 
11
  def __call__(self, question: str) -> str:
12
  # print(f"Agent received question (first 50 chars): {question[:50]}...")
13
  print(f"Agent received question: {question}...")
14
- fixed_answer = "This is a default answer."
15
- print(f"Agent returning fixed answer: {fixed_answer}")
16
- return fixed_answer
17
 
18
- def main():
19
- try:
20
- agent = BasicAgent()
21
- except Exception as e:
22
- logger.debug(f"Error instantiating agent: {e}")
23
- return f"Error initializing agent: {e}", None
 
 
 
 
 
24
 
 
 
25
  api_url = DEFAULT_API_URL
26
  questions_url = f"{api_url}/questions"
27
- submit_url = f"{api_url}/submit"
28
- username = "mikeee"
29
- repo_name = "final-assignment"
 
 
 
 
30
  space_id = f"{username}/{repo_name}"
31
 
 
32
  # 1. Instantiate Agent ( modify this part to create your agent)
33
  try:
34
- agent = BasicAgent()
 
 
 
 
 
 
 
35
  except Exception as e:
36
  print(f"Error instantiating agent: {e}")
37
  return f"Error initializing agent: {e}", None
 
38
  # In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
39
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
40
  print(agent_code)
@@ -42,20 +116,20 @@ def main():
42
  # 2. Fetch Questions
43
  print(f"Fetching questions from: {questions_url}")
44
  try:
45
- response = requests.get(questions_url, timeout=15)
46
  response.raise_for_status()
47
  questions_data = response.json()
48
  if not questions_data:
49
- print("Fetched questions list is empty.")
50
- return "Fetched questions list is empty or invalid format.", None
51
  print(f"Fetched {len(questions_data)} questions.")
 
 
 
 
52
  except requests.exceptions.RequestException as e:
53
  print(f"Error fetching questions: {e}")
54
  return f"Error fetching questions: {e}", None
55
- except requests.exceptions.JSONDecodeError as e:
56
- print(f"Error decoding JSON response from questions endpoint: {e}")
57
- print(f"Response text: {response.text[:500]}")
58
- return f"Error decoding server response for questions: {e}", None
59
  except Exception as e:
60
  print(f"An unexpected error occurred fetching questions: {e}")
61
  return f"An unexpected error occurred fetching questions: {e}", None
@@ -63,8 +137,11 @@ def main():
63
  # 3. Run your Agent
64
  results_log = []
65
  answers_payload = []
 
66
  print(f"Running agent on {len(questions_data)} questions...")
67
- for item in questions_data:
 
 
68
  task_id = item.get("task_id")
69
  question_text = item.get("question")
70
  if not task_id or question_text is None:
@@ -75,20 +152,21 @@ def main():
75
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
76
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
77
  except Exception as e:
78
- print(f"Error running agent on task {task_id}: {e}")
79
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
80
 
81
  if not answers_payload:
82
  print("Agent did not produce any answers to submit.")
83
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
84
 
85
  # 4. Prepare Submission
86
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
87
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
88
  print(status_update)
89
  print(answers_payload)
90
 
91
-
 
92
 
93
  if __name__ == "__main__":
94
- main()
 
1
+ # pylint: disable=line-too-long,missing-module-docstring,missing-class-docstring,missing-function-docstring,broad-exception-caught, unused-variable, too-many-statements,too-many-return-statements,too-many-locals,redefined-builtin,unused-import
2
+ # ruff: noqa: F401
3
+
4
+ import os
5
+ import typing
6
+ from dataclasses import dataclass, field
7
+
8
+ import pandas as pd
9
+ import requests
10
  import rich
11
  from loguru import logger
12
+ import smolagents
13
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, FinalAnswerTool, HfApiModel, VisitWebpageTool
14
+
15
+ from get_model import get_model
16
 
17
  print = rich.get_console().print
18
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
19
+ SPACE_ID = os.getenv("SPACE_ID", "mikeee/final-assignment")
20
+
21
+ AUTHORIZED_IMPORTS = [
22
+ "requests",
23
+ "zipfile",
24
+ "pandas",
25
+ "numpy",
26
+ "sympy",
27
+ "json",
28
+ "bs4",
29
+ "pubchempy",
30
+ "xml",
31
+ "yahoo_finance",
32
+ "Bio",
33
+ "sklearn",
34
+ "scipy",
35
+ "pydub",
36
+ "PIL",
37
+ "chess",
38
+ "PyPDF2",
39
+ "pptx",
40
+ "torch",
41
+ "datetime",
42
+ "fractions",
43
+ "csv",
44
+ "io",
45
+ "glob",
46
+ ]
47
+
48
 
49
+ @dataclass
50
  class BasicAgent:
51
+ model: smolagents.models.Model = HfApiModel()
52
+ tools: list = field(default_factory=lambda: [])
53
+ # def __init__(self):
54
+ def __post_init__(self):
55
+ """Run post_init."""
56
  logger.debug("BasicAgent initialized.")
57
+ self.agent = CodeAgent(
58
+ tools=self.tools,
59
+ model=self.model,
60
+ verbosity_level=3,
61
+ additional_authorized_imports=AUTHORIZED_IMPORTS,
62
+ planning_interval=4,
63
+ )
64
+
65
+ def get_answer(self, question: str):
66
+ return f"ans to {question[:220]}..."
67
+
68
  def __call__(self, question: str) -> str:
69
  # print(f"Agent received question (first 50 chars): {question[:50]}...")
70
  print(f"Agent received question: {question}...")
 
 
 
71
 
72
+ # fixed_answer = "This is a default answer."
73
+ # print(f"Agent returning fixed answer: {fixed_answer}")
74
+ # return fixed_answer
75
+ try:
76
+ # answer = self.get_answer(question)
77
+ answer = self.agent(question)
78
+ except Exception as e:
79
+ logger.error(e)
80
+ answer = str(e)[:10] + "..."
81
+
82
+ return answer
83
 
84
+
85
+ def main():
86
  api_url = DEFAULT_API_URL
87
  questions_url = f"{api_url}/questions"
88
+ submit_url = f"{api_url}/submit" # noqa
89
+
90
+ # username = "mikeee"
91
+ # repo_name = "final-assignment"
92
+
93
+ username, _, repo_name = SPACE_ID.partition("/")
94
+
95
  space_id = f"{username}/{repo_name}"
96
 
97
+ model = get_model(cat="gemini")
98
  # 1. Instantiate Agent ( modify this part to create your agent)
99
  try:
100
+ # agent = BasicAgent()
101
+ agent = BasicAgent(
102
+ model=model,
103
+ tools=[
104
+ DuckDuckGoSearchTool(),
105
+ VisitWebpageTool(),
106
+ ]
107
+ )
108
  except Exception as e:
109
  print(f"Error instantiating agent: {e}")
110
  return f"Error initializing agent: {e}", None
111
+
112
  # In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
113
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
114
  print(agent_code)
 
116
  # 2. Fetch Questions
117
  print(f"Fetching questions from: {questions_url}")
118
  try:
119
+ response = requests.get(questions_url, timeout=30)
120
  response.raise_for_status()
121
  questions_data = response.json()
122
  if not questions_data:
123
+ print("Fetched questions list is empty.")
124
+ return "Fetched questions list is empty or invalid format.", None
125
  print(f"Fetched {len(questions_data)} questions.")
126
+ except requests.exceptions.JSONDecodeError as e:
127
+ print(f"Error decoding JSON response from questions endpoint: {e}")
128
+ print(f"Response text: {response.text[:500]}")
129
+ return f"Error decoding server response for questions: {e}", None
130
  except requests.exceptions.RequestException as e:
131
  print(f"Error fetching questions: {e}")
132
  return f"Error fetching questions: {e}", None
 
 
 
 
133
  except Exception as e:
134
  print(f"An unexpected error occurred fetching questions: {e}")
135
  return f"An unexpected error occurred fetching questions: {e}", None
 
137
  # 3. Run your Agent
138
  results_log = []
139
  answers_payload = []
140
+
141
  print(f"Running agent on {len(questions_data)} questions...")
142
+
143
+ # for item in questions_data:
144
+ for item in questions_data[-1:]:
145
  task_id = item.get("task_id")
146
  question_text = item.get("question")
147
  if not task_id or question_text is None:
 
152
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
153
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
154
  except Exception as e:
155
+ print(f"Error running agent on task {task_id}: {e}")
156
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
157
 
158
  if not answers_payload:
159
  print("Agent did not produce any answers to submit.")
160
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
161
 
162
  # 4. Prepare Submission
163
+ submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload} # noqa
164
  status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
165
  print(status_update)
166
  print(answers_payload)
167
 
168
+ agent.agent.visualize()
169
+ return None, None
170
 
171
  if __name__ == "__main__":
172
+ main()
get_gemini_keys.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Get gemini keys."""
2
+ import re
3
+ from pathlib import Path
4
+
5
+ import rich
6
+ import yaml
7
+ from dotenv import dotenv_values
8
+
9
+
10
+ def get_gemini_keys(file=r".env-gemini", dotenv=False):
11
+ """Get gemini keys."""
12
+ if Path(file).name.startswith(".env"):
13
+ dotenv = True
14
+
15
+ if isinstance(dotenv, bool) or isinstance(dotenv, float):
16
+ dotenv = bool(dotenv)
17
+
18
+ if dotenv is True:
19
+ try:
20
+ keys = yaml.load(dotenv_values(file).get("GEMINI_API_KEYS"), yaml.Loader)
21
+ except Exception as e:
22
+ print(e)
23
+ return []
24
+ return keys
25
+
26
+ try:
27
+ text = Path(file).read_text()
28
+ # return re.findall(r"AIzaSy[A-Z][\w-]+", text)
29
+ return re.findall(r"AIzaSy[A-Z][\w-]{32}", text)
30
+ except Exception as e:
31
+ print(e)
32
+ return []
33
+
34
+ if __name__ == "__main__":
35
+ rich.get_console().print(get_gemini_keys())
get_model.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Create and return a model."""
2
+
3
+ import os
4
+ import re
5
+ from platform import node
6
+
7
+ from get_gemini_keys import get_gemini_keys
8
+ from loguru import logger
9
+ from smolagents import HfApiModel, LiteLLMRouterModel
10
+
11
+
12
+ def get_model(cat: str = "hf", provider=None, model_id=None):
13
+ """
14
+ Create and return a model.
15
+
16
+ Args:
17
+ cat: category
18
+ provider: for HfApiModel (cat='hf')
19
+ model_id: model name
20
+
21
+ if no gemini_api_keys, return HfApiModel()
22
+
23
+ """
24
+ if cat.lower() in ["gemini"]:
25
+ # get gemini_api_keys
26
+ # dedup
27
+ _ = re.findall(r"AIzaSy[A-Z][\w-]{32}", os.getenv("GEMINI_API_KEYS", ""))
28
+ gemini_api_keys = dict.fromkeys(get_gemini_keys() + _)
29
+
30
+ # assert gemini_api_keys, "No GEMINI_API_KEYS, set env var GEMINI_API_KEYS or put them in .env-gemini and try again."
31
+ if not gemini_api_keys:
32
+ logger.warning(
33
+ "cat='gemini' but no GEMINI_API_KEYS found, "
34
+ " returning HfApiModel()..."
35
+ " Set env var GEMINI_API_KEYS and/or .env-gemini "
36
+ " with free space gemini-api-keys if you want to try 'gemini' "
37
+ )
38
+ return HfApiModel()
39
+
40
+ # setup proxy for gemini and for golay (local)
41
+ if "golay" in node():
42
+ os.environ.update(
43
+ HTTPS_PROXY="http://localhost:8081",
44
+ HTTP_PROXY="http://localhost:8081",
45
+ ALL_PROXY="http://localhost:8081",
46
+ NO_PROXY="localhost,127.0.0.1,oracle",
47
+ )
48
+
49
+ if model_id is None:
50
+ model_id = "gemini-2.5-flash-preview-04-17"
51
+
52
+ # model_id = "gemini-2.5-flash-preview-04-17"
53
+ llm_loadbalancer_model_list_gemini = []
54
+ for api_key in gemini_api_keys:
55
+ llm_loadbalancer_model_list_gemini.append(
56
+ {
57
+ "model_name": "model-group-1",
58
+ "litellm_params": {
59
+ "model": f"gemini/{model_id}",
60
+ "api_key": api_key,
61
+ },
62
+ },
63
+ )
64
+
65
+ model_id = "deepseek-ai/DeepSeek-V3"
66
+ llm_loadbalancer_model_list_siliconflow = [
67
+ {
68
+ "model_name": "model-group-2",
69
+ "litellm_params": {
70
+ "model": f"openai/{model_id}",
71
+ "api_key": os.getenv("SILICONFLOW_API_KEY"),
72
+ "api_base": "https://api.siliconflow.cn/v1",
73
+ },
74
+ }
75
+ ]
76
+
77
+ fallbacks = []
78
+ model_list = llm_loadbalancer_model_list_gemini
79
+ if os.getenv("SILICONFLOW_API_KEY"):
80
+ fallbacks = [{"model-group-1": "model-group-2"}]
81
+ model_list += llm_loadbalancer_model_list_siliconflow
82
+
83
+ model = LiteLLMRouterModel(
84
+ model_id="model-group-1",
85
+ model_list=model_list,
86
+ client_kwargs={
87
+ "routing_strategy": "simple-shuffle",
88
+ "num_retries": 3,
89
+ # "retry_after": 130, # waits min s before retrying request
90
+ "fallbacks": fallbacks,
91
+ },
92
+ )
93
+ return model
94
+
95
+ # if cat.lower() in ["hf"]: default
96
+ return HfApiModel(provider=provider, model_id=model_id)
requirements.txt CHANGED
@@ -5,5 +5,15 @@ loguru
5
  ycecream
6
  mcp[cli]
7
  mcpadapt
 
 
 
 
 
 
8
  smolagents@git+https://github.com/huggingface/smolagents.git
9
  wikipedia-api
 
 
 
 
 
5
  ycecream
6
  mcp[cli]
7
  mcpadapt
8
+
9
+ # for smolagents.LiteLLMModel, smolagents.LiteLLMRouterModel (LiteLLMRouterModel does not seem to work)
10
+ litellm
11
+
12
+ # for smolagents.OpenAIServerModel
13
+ openai
14
  smolagents@git+https://github.com/huggingface/smolagents.git
15
  wikipedia-api
16
+
17
+ yaml
18
+ rich
19
+ python-dotenv