ffreemt commited on
Commit
4d96293
·
1 Parent(s): 48ec86e

Update llama4 models

Browse files
__pycache__/basic_agent.cpython-312.pyc CHANGED
Binary files a/__pycache__/basic_agent.cpython-312.pyc and b/__pycache__/basic_agent.cpython-312.pyc differ
 
__pycache__/get_model.cpython-312.pyc CHANGED
Binary files a/__pycache__/get_model.cpython-312.pyc and b/__pycache__/get_model.cpython-312.pyc differ
 
app.py CHANGED
@@ -7,12 +7,13 @@ import pandas as pd
7
  import requests
8
  import rich
9
  import wikipediaapi
10
- from basic_agent import BasicAgent
11
- from get_model import get_model
12
  from mcp import StdioServerParameters
13
  from smolagents import DuckDuckGoSearchTool, FinalAnswerTool, Tool, ToolCollection, VisitWebpageTool
14
  from ycecream import y
15
 
 
 
 
16
  y.configure(sln=1)
17
  print = rich.get_console().print
18
 
@@ -37,13 +38,14 @@ class BasicAgent:
37
  return fixed_answer
38
  # """
39
 
40
- def run_and_submit_all( profile: gr.OAuthProfile | None):
 
41
  """Fetch all questions, run the BasicAgent on them, submit all answers, and display the results."""
42
  # --- Determine HF Space Runtime URL and Repo URL ---
43
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
44
 
45
  if profile:
46
- username= f"{profile.username}"
47
  print(f"User logged in: {username}")
48
  else:
49
  print("User not logged in.")
@@ -56,12 +58,13 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
56
  # 1. Instantiate Agent ( modify this part to create your agent)
57
  try:
58
  agent = BasicAgent(
59
- model=get_model(cat="gemini"),
 
60
  tools=[
61
  DuckDuckGoSearchTool(),
62
  VisitWebpageTool(),
63
- FinalAnswerTool(),
64
- ]
65
  )
66
  except Exception as e:
67
  print(f"Error instantiating agent: {e}")
@@ -77,17 +80,17 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
77
  response.raise_for_status()
78
  questions_data = response.json()
79
  if not questions_data:
80
- print("Fetched questions list is empty.")
81
- return "Fetched questions list is empty or invalid format.", None
82
  print(f"Fetched {len(questions_data)} questions.")
83
 
84
  except requests.exceptions.RequestException as e:
85
  print(f"Error fetching questions: {e}")
86
  return f"Error fetching questions: {e}", None
87
  except requests.exceptions.JSONDecodeError as e:
88
- print(f"Error decoding JSON response from questions endpoint: {e}")
89
- print(f"Response text: {response.text[:500]}")
90
- return f"Error decoding server response for questions: {e}", None
91
  except Exception as e:
92
  print(f"An unexpected error occurred fetching questions: {e}")
93
  return f"An unexpected error occurred fetching questions: {e}", None
@@ -107,8 +110,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
107
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
108
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
109
  except Exception as e:
110
- print(f"Error running agent on task {task_id}: {e}")
111
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
112
 
113
  if not answers_payload:
114
  print("Agent did not produce any answers to submit.")
@@ -125,13 +128,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
125
  response = requests.post(submit_url, json=submission_data, timeout=60)
126
  response.raise_for_status()
127
  result_data = response.json()
128
- final_status = (
129
- f"Submission Successful!\n"
130
- f"User: {result_data.get('username')}\n"
131
- f"Overall Score: {result_data.get('score', 'N/A')}% "
132
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
133
- f"Message: {result_data.get('message', 'No message received.')}"
134
- )
135
  print("Submission successful.")
136
  results_df = pd.DataFrame(results_log)
137
  return final_status, results_df
@@ -177,7 +174,10 @@ with gr.Blocks() as demo:
177
  ---
178
  **Disclaimers:**
179
  Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
180
- This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
 
 
 
181
  """
182
  )
183
 
@@ -189,16 +189,13 @@ with gr.Blocks() as demo:
189
  # Removed max_rows=10 from DataFrame constructor
190
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
191
 
192
- run_button.click(
193
- fn=run_and_submit_all,
194
- outputs=[status_output, results_table]
195
- )
196
 
197
  if __name__ == "__main__":
198
- print("\n" + "-"*30 + " App Starting " + "-"*30)
199
  # Check for SPACE_HOST and SPACE_ID at startup for information
200
  space_host_startup = os.getenv("SPACE_HOST")
201
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
202
 
203
  if space_host_startup:
204
  print(f"✅ SPACE_HOST found: {space_host_startup}")
@@ -206,14 +203,14 @@ if __name__ == "__main__":
206
  else:
207
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
208
 
209
- if space_id_startup: # Print repo URLs if SPACE_ID is found
210
  print(f"✅ SPACE_ID found: {space_id_startup}")
211
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
212
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
213
  else:
214
  print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
215
 
216
- print("-"*(60 + len(" App Starting ")) + "\n")
217
 
218
  print("Launching Gradio Interface for Basic Agent Evaluation...")
219
  demo.launch(debug=True, share=False)
 
7
  import requests
8
  import rich
9
  import wikipediaapi
 
 
10
  from mcp import StdioServerParameters
11
  from smolagents import DuckDuckGoSearchTool, FinalAnswerTool, Tool, ToolCollection, VisitWebpageTool
12
  from ycecream import y
13
 
14
+ from basic_agent import BasicAgent
15
+ from get_model import get_model
16
+
17
  y.configure(sln=1)
18
  print = rich.get_console().print
19
 
 
38
  return fixed_answer
39
  # """
40
 
41
+
42
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
43
  """Fetch all questions, run the BasicAgent on them, submit all answers, and display the results."""
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
 
47
  if profile:
48
+ username = f"{profile.username}"
49
  print(f"User logged in: {username}")
50
  else:
51
  print("User not logged in.")
 
58
  # 1. Instantiate Agent ( modify this part to create your agent)
59
  try:
60
  agent = BasicAgent(
61
+ # model=get_model(cat="gemini"),
62
+ model=get_model(cat="llama"),
63
  tools=[
64
  DuckDuckGoSearchTool(),
65
  VisitWebpageTool(),
66
+ # FinalAnswerTool(),
67
+ ],
68
  )
69
  except Exception as e:
70
  print(f"Error instantiating agent: {e}")
 
80
  response.raise_for_status()
81
  questions_data = response.json()
82
  if not questions_data:
83
+ print("Fetched questions list is empty.")
84
+ return "Fetched questions list is empty or invalid format.", None
85
  print(f"Fetched {len(questions_data)} questions.")
86
 
87
  except requests.exceptions.RequestException as e:
88
  print(f"Error fetching questions: {e}")
89
  return f"Error fetching questions: {e}", None
90
  except requests.exceptions.JSONDecodeError as e:
91
+ print(f"Error decoding JSON response from questions endpoint: {e}")
92
+ print(f"Response text: {response.text[:500]}")
93
+ return f"Error decoding server response for questions: {e}", None
94
  except Exception as e:
95
  print(f"An unexpected error occurred fetching questions: {e}")
96
  return f"An unexpected error occurred fetching questions: {e}", None
 
110
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
111
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
112
  except Exception as e:
113
+ print(f"Error running agent on task {task_id}: {e}")
114
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
115
 
116
  if not answers_payload:
117
  print("Agent did not produce any answers to submit.")
 
128
  response = requests.post(submit_url, json=submission_data, timeout=60)
129
  response.raise_for_status()
130
  result_data = response.json()
131
+ final_status = f"Submission Successful!\nUser: {result_data.get('username')}\nOverall Score: {result_data.get('score', 'N/A')}% ({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\nMessage: {result_data.get('message', 'No message received.')}"
 
 
 
 
 
 
132
  print("Submission successful.")
133
  results_df = pd.DataFrame(results_log)
134
  return final_status, results_df
 
174
  ---
175
  **Disclaimers:**
176
  Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
177
+ This space provides a basic setup and is intentionally sub-optimal to encourage you
178
+ to develop your own, more robust solution. For instance for the delay process of the
179
+ submit button, a solution could be to cache the answers and submit in a seperate
180
+ action or even to answer the questions in async.
181
  """
182
  )
183
 
 
189
  # Removed max_rows=10 from DataFrame constructor
190
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
191
 
192
+ run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
 
 
 
193
 
194
  if __name__ == "__main__":
195
+ print("\n" + "-" * 30 + " App Starting " + "-" * 30)
196
  # Check for SPACE_HOST and SPACE_ID at startup for information
197
  space_host_startup = os.getenv("SPACE_HOST")
198
+ space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
199
 
200
  if space_host_startup:
201
  print(f"✅ SPACE_HOST found: {space_host_startup}")
 
203
  else:
204
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
205
 
206
+ if space_id_startup: # Print repo URLs if SPACE_ID is found
207
  print(f"✅ SPACE_ID found: {space_id_startup}")
208
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
209
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
210
  else:
211
  print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
212
 
213
+ print("-" * (60 + len(" App Starting ")) + "\n")
214
 
215
  print("Launching Gradio Interface for Basic Agent Evaluation...")
216
  demo.launch(debug=True, share=False)
get_gemini_keys.py CHANGED
@@ -11,7 +11,7 @@ from loguru import logger
11
  def get_gemini_keys(file=r".env-gemini", dotenv=False):
12
  """Get gemini keys."""
13
  if not Path(file).exists():
14
- logger.debug(f"{file} does not exit, returing [] ")
15
  return []
16
 
17
  if Path(file).name.startswith(".env"):
 
11
  def get_gemini_keys(file=r".env-gemini", dotenv=False):
12
  """Get gemini keys."""
13
  if not Path(file).exists():
14
+ logger.debug(f"{file} does not exit, returning [] ")
15
  return []
16
 
17
  if Path(file).name.startswith(".env"):
get_model.py CHANGED
@@ -1,12 +1,14 @@
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):
@@ -14,13 +16,26 @@ def get_model(cat: str = "hf", provider=None, model_id=None):
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
@@ -33,15 +48,6 @@ def get_model(cat: str = "hf", provider=None, model_id=None):
33
  logger.info(" set gemini but return HfApiModel()")
34
  return HfApiModel()
35
 
36
- # setup proxy for gemini and for golay (local)
37
- if "golay" in node():
38
- os.environ.update(
39
- HTTPS_PROXY="http://localhost:8081",
40
- HTTP_PROXY="http://localhost:8081",
41
- ALL_PROXY="http://localhost:8081",
42
- NO_PROXY="localhost,127.0.0.1,oracle",
43
- )
44
-
45
  if model_id is None:
46
  model_id = "gemini-2.5-flash-preview-04-17"
47
 
@@ -69,27 +75,27 @@ def get_model(cat: str = "hf", provider=None, model_id=None):
69
  },
70
  },
71
  ]
72
-
73
  # gemma-3-27b-it
74
  llm_loadbalancer_model_list_gemma = [
75
  {
76
  "model_name": "model-group-3",
77
  "litellm_params": {
78
- "model": f"gemini/gemma-3-27b-it",
79
  "api_key": os.getenv("GEMINI_API_KEY") },
80
  },
81
  ]
82
-
83
  fallbacks = []
84
  model_list = llm_loadbalancer_model_list_gemini
85
  if os.getenv("SILICONFLOW_API_KEY"):
86
  fallbacks = [{"model-group-1": "model-group-2"}]
87
  model_list += llm_loadbalancer_model_list_siliconflow
88
-
89
  model_list += llm_loadbalancer_model_list_gemma
90
  fallbacks13 = [{"model-group-1": "model-group-3"}]
91
  fallbacks31 = [{"model-group-3": "model-group-1"}]
92
-
93
  model = LiteLLMRouterModel(
94
  model_id="model-group-1",
95
  model_list=model_list,
@@ -108,6 +114,24 @@ def get_model(cat: str = "hf", provider=None, model_id=None):
108
 
109
  return model
110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  logger.info(" default return default HfApiModel(provider=None, model_id=None)")
112
  # if cat.lower() in ["hf"]: default
113
  return HfApiModel(provider=provider, model_id=model_id)
 
1
  """Create and return a model."""
2
+ # ruff: noqa: F841
3
 
4
  import os
5
  import re
6
  from platform import node
7
 
 
8
  from loguru import logger
9
+ from smolagents import HfApiModel, LiteLLMRouterModel, OpenAIServerModel
10
+
11
+ from get_gemini_keys import get_gemini_keys
12
 
13
 
14
  def get_model(cat: str = "hf", provider=None, model_id=None):
 
16
  Create and return a model.
17
 
18
  Args:
19
+ cat: category, hf, gemin, llama (default and fallback: hf)
20
  provider: for HfApiModel (cat='hf')
21
  model_id: model name
22
 
23
  if no gemini_api_keys, return HfApiModel()
24
 
25
  """
26
+ if cat.lower() in ["hf"]:
27
+ logger.info(" usiing HfApiModel, make sure you set HF_TOKEN")
28
+ return HfApiModel(provider=provider, model_id=model_id)
29
+
30
+ # setup proxy for gemini and for golay (local tetsin)
31
+ if "golay" in node() and cat.lower() in ["gemini", "llama"]:
32
+ os.environ.update(
33
+ HTTPS_PROXY="http://localhost:8081",
34
+ HTTP_PROXY="http://localhost:8081",
35
+ ALL_PROXY="http://localhost:8081",
36
+ NO_PROXY="localhost,127.0.0.1",
37
+ )
38
+
39
  if cat.lower() in ["gemini"]:
40
  # get gemini_api_keys
41
  # dedup
 
48
  logger.info(" set gemini but return HfApiModel()")
49
  return HfApiModel()
50
 
 
 
 
 
 
 
 
 
 
51
  if model_id is None:
52
  model_id = "gemini-2.5-flash-preview-04-17"
53
 
 
75
  },
76
  },
77
  ]
78
+
79
  # gemma-3-27b-it
80
  llm_loadbalancer_model_list_gemma = [
81
  {
82
  "model_name": "model-group-3",
83
  "litellm_params": {
84
+ "model": "gemini/gemma-3-27b-it",
85
  "api_key": os.getenv("GEMINI_API_KEY") },
86
  },
87
  ]
88
+
89
  fallbacks = []
90
  model_list = llm_loadbalancer_model_list_gemini
91
  if os.getenv("SILICONFLOW_API_KEY"):
92
  fallbacks = [{"model-group-1": "model-group-2"}]
93
  model_list += llm_loadbalancer_model_list_siliconflow
94
+
95
  model_list += llm_loadbalancer_model_list_gemma
96
  fallbacks13 = [{"model-group-1": "model-group-3"}]
97
  fallbacks31 = [{"model-group-3": "model-group-1"}]
98
+
99
  model = LiteLLMRouterModel(
100
  model_id="model-group-1",
101
  model_list=model_list,
 
114
 
115
  return model
116
 
117
+ if cat.lower() in ["llama"]:
118
+ api_key = os.getenv("LLAMA_API_KEY")
119
+ if api_key is None:
120
+ logger.warning(" LLAMA_API_EY not set, using HfApiModel(), make sure you set HF_TOKEN")
121
+ return HfApiModel()
122
+
123
+ # default model_id
124
+ if model_id is None:
125
+ model_id = "Llama-4-Maverick-17B-128E-Instruct-FP8"
126
+ model_id = "Llama-4-Scout-17B-16E-Instruct-FP8"
127
+ model_llama = OpenAIServerModel(
128
+ model_id,
129
+ api_base="https://api.llama.com/compat/v1",
130
+ api_key=api_key,
131
+ # temperature=0.,
132
+ )
133
+ return model_llama
134
+
135
  logger.info(" default return default HfApiModel(provider=None, model_id=None)")
136
  # if cat.lower() in ["hf"]: default
137
  return HfApiModel(provider=provider, model_id=model_id)