Added changes to support Google API
Browse files- app.py +39 -46
- requirements.txt +2 -0
app.py
CHANGED
|
@@ -7,14 +7,15 @@ import pandas as pd
|
|
| 7 |
import asyncio
|
| 8 |
import aiohttp
|
| 9 |
|
| 10 |
-
from smolagents import CodeAgent,
|
| 11 |
|
| 12 |
# --- Constants ---
|
| 13 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 14 |
|
| 15 |
# --- Basic Agent Definition ---
|
| 16 |
-
MODEL_ID = "gemini-2.5-pro-preview-06-05"
|
| 17 |
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
|
|
|
| 18 |
class BasicAgent:
|
| 19 |
def __init__(self):
|
| 20 |
print("BasicAgent initialized to use smolagents.")
|
|
@@ -23,11 +24,9 @@ class BasicAgent:
|
|
| 23 |
try:
|
| 24 |
search_tool = DuckDuckGoSearchTool()
|
| 25 |
|
| 26 |
-
llm_model =
|
| 27 |
model_id=MODEL_ID,
|
| 28 |
-
api_base="https://generativelanguage.googleapis.com/v1beta/openai/",
|
| 29 |
api_key=GOOGLE_API_KEY,
|
| 30 |
-
temperature=0.7
|
| 31 |
)
|
| 32 |
self.agent_executor = CodeAgent(
|
| 33 |
model=llm_model,
|
|
@@ -62,13 +61,6 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 62 |
"""
|
| 63 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 64 |
|
| 65 |
-
if profile:
|
| 66 |
-
username= f"{profile.username}"
|
| 67 |
-
print(f"User logged in: {username}")
|
| 68 |
-
else:
|
| 69 |
-
print("User not logged in.")
|
| 70 |
-
return "Please Login to Hugging Face with the button.", None
|
| 71 |
-
|
| 72 |
api_url = DEFAULT_API_URL
|
| 73 |
questions_url = f"{api_url}/questions"
|
| 74 |
submit_url = f"{api_url}/submit"
|
|
@@ -193,38 +185,6 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 193 |
print(status_message)
|
| 194 |
return status_message, pd.DataFrame(results_log)
|
| 195 |
|
| 196 |
-
|
| 197 |
-
# --- Build Gradio Interface using Blocks ---
|
| 198 |
-
with gr.Blocks() as demo:
|
| 199 |
-
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 200 |
-
gr.Markdown(
|
| 201 |
-
"""
|
| 202 |
-
**Instructions:**
|
| 203 |
-
|
| 204 |
-
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 205 |
-
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 206 |
-
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 207 |
-
|
| 208 |
-
---
|
| 209 |
-
**Disclaimers:**
|
| 210 |
-
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).
|
| 211 |
-
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.
|
| 212 |
-
"""
|
| 213 |
-
)
|
| 214 |
-
|
| 215 |
-
gr.LoginButton()
|
| 216 |
-
|
| 217 |
-
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 218 |
-
|
| 219 |
-
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 220 |
-
# Removed max_rows=10 from DataFrame constructor
|
| 221 |
-
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 222 |
-
|
| 223 |
-
run_button.click(
|
| 224 |
-
fn=run_and_submit_all,
|
| 225 |
-
outputs=[status_output, results_table]
|
| 226 |
-
)
|
| 227 |
-
|
| 228 |
if __name__ == "__main__":
|
| 229 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 230 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
|
@@ -236,6 +196,9 @@ if __name__ == "__main__":
|
|
| 236 |
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 237 |
else:
|
| 238 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
| 241 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
|
@@ -243,8 +206,38 @@ if __name__ == "__main__":
|
|
| 243 |
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 244 |
else:
|
| 245 |
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
|
|
|
| 246 |
|
| 247 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 248 |
-
|
| 249 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
import asyncio
|
| 8 |
import aiohttp
|
| 9 |
|
| 10 |
+
from smolagents import CodeAgent, LiteLLMModel, DuckDuckGoSearchTool
|
| 11 |
|
| 12 |
# --- Constants ---
|
| 13 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 14 |
|
| 15 |
# --- Basic Agent Definition ---
|
| 16 |
+
MODEL_ID = "gemini/gemini-2.5-pro-preview-06-05"
|
| 17 |
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 18 |
+
|
| 19 |
class BasicAgent:
|
| 20 |
def __init__(self):
|
| 21 |
print("BasicAgent initialized to use smolagents.")
|
|
|
|
| 24 |
try:
|
| 25 |
search_tool = DuckDuckGoSearchTool()
|
| 26 |
|
| 27 |
+
llm_model = LiteLLMModel(
|
| 28 |
model_id=MODEL_ID,
|
|
|
|
| 29 |
api_key=GOOGLE_API_KEY,
|
|
|
|
| 30 |
)
|
| 31 |
self.agent_executor = CodeAgent(
|
| 32 |
model=llm_model,
|
|
|
|
| 61 |
"""
|
| 62 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
api_url = DEFAULT_API_URL
|
| 65 |
questions_url = f"{api_url}/questions"
|
| 66 |
submit_url = f"{api_url}/submit"
|
|
|
|
| 185 |
print(status_message)
|
| 186 |
return status_message, pd.DataFrame(results_log)
|
| 187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
if __name__ == "__main__":
|
| 189 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 190 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
|
|
|
| 196 |
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 197 |
else:
|
| 198 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 199 |
+
loop = asyncio.get_event_loop()
|
| 200 |
+
loop.run_until_complete(run_and_submit_all(None))
|
| 201 |
+
exit(0)
|
| 202 |
|
| 203 |
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
| 204 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
|
|
|
| 206 |
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 207 |
else:
|
| 208 |
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 209 |
+
exit(0)
|
| 210 |
|
| 211 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
|
|
|
| 212 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 213 |
+
# --- Build Gradio Interface using Blocks ---
|
| 214 |
+
with gr.Blocks() as demo:
|
| 215 |
+
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 216 |
+
gr.Markdown(
|
| 217 |
+
"""
|
| 218 |
+
**Instructions:**
|
| 219 |
+
|
| 220 |
+
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 221 |
+
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 222 |
+
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 223 |
+
|
| 224 |
+
---
|
| 225 |
+
**Disclaimers:**
|
| 226 |
+
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).
|
| 227 |
+
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.
|
| 228 |
+
"""
|
| 229 |
+
)
|
| 230 |
+
|
| 231 |
+
gr.LoginButton()
|
| 232 |
+
|
| 233 |
+
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 234 |
+
|
| 235 |
+
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 236 |
+
# Removed max_rows=10 from DataFrame constructor
|
| 237 |
+
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 238 |
+
|
| 239 |
+
run_button.click(
|
| 240 |
+
fn=run_and_submit_all,
|
| 241 |
+
outputs=[status_output, results_table]
|
| 242 |
+
)
|
| 243 |
+
demo.launch(debug=True, share=False)
|
requirements.txt
CHANGED
|
@@ -4,4 +4,6 @@ torch
|
|
| 4 |
transformers
|
| 5 |
aiohttp
|
| 6 |
smolagents
|
|
|
|
|
|
|
| 7 |
duckduckgo-search
|
|
|
|
| 4 |
transformers
|
| 5 |
aiohttp
|
| 6 |
smolagents
|
| 7 |
+
smolagents[litellm]
|
| 8 |
+
google
|
| 9 |
duckduckgo-search
|