Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,19 +3,18 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
|
|
|
|
| 6 |
from smolagents.agents import ToolCallingAgent
|
| 7 |
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
|
| 10 |
def create_agent():
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
# You specify the tool names as strings!
|
| 15 |
agent = ToolCallingAgent(
|
| 16 |
tools=["wikipedia", "duckduckgo", "web_search"],
|
| 17 |
-
model=
|
| 18 |
-
model_kwargs={"repo_id": "HuggingFaceH4/zephyr-7b-beta"},
|
| 19 |
)
|
| 20 |
return agent
|
| 21 |
|
|
@@ -23,48 +22,35 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 23 |
space_id = os.getenv("SPACE_ID")
|
| 24 |
if profile:
|
| 25 |
username = f"{profile.username}"
|
| 26 |
-
print(f"User logged in: {username}")
|
| 27 |
else:
|
| 28 |
-
print("User not logged in.")
|
| 29 |
return "Please Login to Hugging Face with the button.", None
|
| 30 |
|
| 31 |
api_url = DEFAULT_API_URL
|
| 32 |
questions_url = f"{api_url}/questions"
|
| 33 |
submit_url = f"{api_url}/submit"
|
| 34 |
|
| 35 |
-
# Create agent with all relevant tools
|
| 36 |
try:
|
| 37 |
agent = create_agent()
|
| 38 |
except Exception as e:
|
| 39 |
return f"Error initializing agent: {e}", None
|
| 40 |
|
| 41 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 42 |
-
print(agent_code)
|
| 43 |
|
| 44 |
-
# Fetch Questions
|
| 45 |
-
print(f"Fetching questions from: {questions_url}")
|
| 46 |
try:
|
| 47 |
response = requests.get(questions_url, timeout=15)
|
| 48 |
response.raise_for_status()
|
| 49 |
questions_data = response.json()
|
| 50 |
if not questions_data:
|
| 51 |
-
print("Fetched questions list is empty.")
|
| 52 |
return "Fetched questions list is empty or invalid format.", None
|
| 53 |
-
print(f"Fetched {len(questions_data)} questions.")
|
| 54 |
-
except requests.exceptions.RequestException as e:
|
| 55 |
-
return f"Error fetching questions: {e}", None
|
| 56 |
except Exception as e:
|
| 57 |
-
return f"
|
| 58 |
|
| 59 |
-
# Run the agent on all questions
|
| 60 |
results_log = []
|
| 61 |
answers_payload = []
|
| 62 |
-
print(f"Running agent on {len(questions_data)} questions...")
|
| 63 |
for item in questions_data:
|
| 64 |
task_id = item.get("task_id")
|
| 65 |
question_text = item.get("question")
|
| 66 |
if not task_id or question_text is None:
|
| 67 |
-
print(f"Skipping item with missing task_id or question: {item}")
|
| 68 |
continue
|
| 69 |
try:
|
| 70 |
submitted_answer = agent(question_text)
|
|
@@ -81,11 +67,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 81 |
"agent_code": agent_code,
|
| 82 |
"answers": answers_payload
|
| 83 |
}
|
| 84 |
-
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 85 |
-
print(status_update)
|
| 86 |
|
| 87 |
-
# Submit answers
|
| 88 |
-
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 89 |
try:
|
| 90 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 91 |
response.raise_for_status()
|
|
@@ -104,17 +86,14 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 104 |
results_df = pd.DataFrame(results_log)
|
| 105 |
return status_message, results_df
|
| 106 |
|
| 107 |
-
# --- Gradio UI ---
|
| 108 |
with gr.Blocks() as demo:
|
| 109 |
gr.Markdown("# SmolAgent Evaluation Runner")
|
| 110 |
gr.Markdown(
|
| 111 |
"""
|
| 112 |
**Instructions:**
|
| 113 |
-
|
| 114 |
- Clone and modify this space to improve your agent logic as you see fit.
|
| 115 |
- Log in to your Hugging Face account with the button below.
|
| 116 |
- Click 'Run Evaluation & Submit All Answers' to begin.
|
| 117 |
-
|
| 118 |
Disclaimer: Submission may take a while depending on the number of questions and agent speed.
|
| 119 |
"""
|
| 120 |
)
|
|
@@ -132,20 +111,17 @@ if __name__ == "__main__":
|
|
| 132 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 133 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 134 |
space_id_startup = os.getenv("SPACE_ID")
|
| 135 |
-
|
| 136 |
if space_host_startup:
|
| 137 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 138 |
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 139 |
else:
|
| 140 |
print("ℹ️ SPACE_HOST not found (running locally?)")
|
| 141 |
-
|
| 142 |
if space_id_startup:
|
| 143 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 144 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 145 |
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 146 |
else:
|
| 147 |
print("ℹ️ SPACE_ID not found")
|
| 148 |
-
|
| 149 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 150 |
print("Launching Gradio Interface for SmolAgent Evaluation...")
|
| 151 |
demo.launch(debug=True, share=False)
|
|
|
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
|
| 6 |
+
from smolagents.models import InferenceClientModel
|
| 7 |
from smolagents.agents import ToolCallingAgent
|
| 8 |
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
def create_agent():
|
| 12 |
+
# Create the model object for Hugging Face inference API
|
| 13 |
+
model = InferenceClientModel(repo_id="HuggingFaceH4/zephyr-7b-beta")
|
| 14 |
+
# Compose the agent, registering tool names (do not import them directly)
|
|
|
|
| 15 |
agent = ToolCallingAgent(
|
| 16 |
tools=["wikipedia", "duckduckgo", "web_search"],
|
| 17 |
+
model=model
|
|
|
|
| 18 |
)
|
| 19 |
return agent
|
| 20 |
|
|
|
|
| 22 |
space_id = os.getenv("SPACE_ID")
|
| 23 |
if profile:
|
| 24 |
username = f"{profile.username}"
|
|
|
|
| 25 |
else:
|
|
|
|
| 26 |
return "Please Login to Hugging Face with the button.", None
|
| 27 |
|
| 28 |
api_url = DEFAULT_API_URL
|
| 29 |
questions_url = f"{api_url}/questions"
|
| 30 |
submit_url = f"{api_url}/submit"
|
| 31 |
|
|
|
|
| 32 |
try:
|
| 33 |
agent = create_agent()
|
| 34 |
except Exception as e:
|
| 35 |
return f"Error initializing agent: {e}", None
|
| 36 |
|
| 37 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
|
|
|
| 38 |
|
|
|
|
|
|
|
| 39 |
try:
|
| 40 |
response = requests.get(questions_url, timeout=15)
|
| 41 |
response.raise_for_status()
|
| 42 |
questions_data = response.json()
|
| 43 |
if not questions_data:
|
|
|
|
| 44 |
return "Fetched questions list is empty or invalid format.", None
|
|
|
|
|
|
|
|
|
|
| 45 |
except Exception as e:
|
| 46 |
+
return f"Error fetching questions: {e}", None
|
| 47 |
|
|
|
|
| 48 |
results_log = []
|
| 49 |
answers_payload = []
|
|
|
|
| 50 |
for item in questions_data:
|
| 51 |
task_id = item.get("task_id")
|
| 52 |
question_text = item.get("question")
|
| 53 |
if not task_id or question_text is None:
|
|
|
|
| 54 |
continue
|
| 55 |
try:
|
| 56 |
submitted_answer = agent(question_text)
|
|
|
|
| 67 |
"agent_code": agent_code,
|
| 68 |
"answers": answers_payload
|
| 69 |
}
|
|
|
|
|
|
|
| 70 |
|
|
|
|
|
|
|
| 71 |
try:
|
| 72 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 73 |
response.raise_for_status()
|
|
|
|
| 86 |
results_df = pd.DataFrame(results_log)
|
| 87 |
return status_message, results_df
|
| 88 |
|
|
|
|
| 89 |
with gr.Blocks() as demo:
|
| 90 |
gr.Markdown("# SmolAgent Evaluation Runner")
|
| 91 |
gr.Markdown(
|
| 92 |
"""
|
| 93 |
**Instructions:**
|
|
|
|
| 94 |
- Clone and modify this space to improve your agent logic as you see fit.
|
| 95 |
- Log in to your Hugging Face account with the button below.
|
| 96 |
- Click 'Run Evaluation & Submit All Answers' to begin.
|
|
|
|
| 97 |
Disclaimer: Submission may take a while depending on the number of questions and agent speed.
|
| 98 |
"""
|
| 99 |
)
|
|
|
|
| 111 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 112 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 113 |
space_id_startup = os.getenv("SPACE_ID")
|
|
|
|
| 114 |
if space_host_startup:
|
| 115 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 116 |
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 117 |
else:
|
| 118 |
print("ℹ️ SPACE_HOST not found (running locally?)")
|
|
|
|
| 119 |
if space_id_startup:
|
| 120 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 121 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 122 |
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 123 |
else:
|
| 124 |
print("ℹ️ SPACE_ID not found")
|
|
|
|
| 125 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 126 |
print("Launching Gradio Interface for SmolAgent Evaluation...")
|
| 127 |
demo.launch(debug=True, share=False)
|