Added smolgents
Browse files- app.py +131 -115
- requirements.txt +4 -1
app.py
CHANGED
|
@@ -1,12 +1,14 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
import requests
|
| 4 |
-
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
|
| 7 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
# (Keep Constants as is)
|
| 10 |
# --- Constants ---
|
| 11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 12 |
|
|
@@ -14,54 +16,49 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 14 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 15 |
class BasicAgent:
|
| 16 |
def __init__(self):
|
| 17 |
-
print("BasicAgent initialized.")
|
| 18 |
-
|
| 19 |
-
|
| 20 |
try:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
except Exception as e:
|
| 28 |
-
print(f"Error
|
| 29 |
raise
|
|
|
|
| 30 |
|
| 31 |
def __call__(self, question: str) -> str:
|
| 32 |
-
print(f"
|
| 33 |
try:
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
]
|
| 39 |
-
text = self.tokenizer.apply_chat_template(
|
| 40 |
-
messages,
|
| 41 |
-
tokenize=False,
|
| 42 |
-
add_generation_prompt=True
|
| 43 |
-
)
|
| 44 |
-
model_inputs = self.tokenizer([text], return_tensors="pt").to(self.device)
|
| 45 |
-
|
| 46 |
-
# Generate a response
|
| 47 |
-
generated_ids = self.model.generate(
|
| 48 |
-
model_inputs.input_ids,
|
| 49 |
-
max_new_tokens=500, # Limit the response length
|
| 50 |
-
do_sample=True,
|
| 51 |
-
temperature=0.7, # Adjust for creativity vs. consistency
|
| 52 |
-
top_p=0.9
|
| 53 |
-
)
|
| 54 |
-
generated_ids = [
|
| 55 |
-
output_id[len(model_inputs.input_ids[0]):] for output_id in generated_ids
|
| 56 |
-
]
|
| 57 |
-
response = self.tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 58 |
-
print(f"Agent returning answer (first 50 chars): {response[:50]}...")
|
| 59 |
-
return response
|
| 60 |
except Exception as e:
|
| 61 |
-
print(f"Error during agent's __call__: {e}")
|
| 62 |
-
return f"Error: Could not process the question. {e}"
|
| 63 |
|
| 64 |
-
def run_and_submit_all(
|
| 65 |
"""
|
| 66 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 67 |
and displays the results.
|
|
@@ -90,96 +87,115 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 90 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 91 |
print(agent_code)
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
results_log = []
|
| 116 |
answers_payload = []
|
| 117 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 118 |
-
|
|
|
|
| 119 |
task_id = item.get("task_id")
|
| 120 |
question_text = item.get("question")
|
| 121 |
if not task_id or question_text is None:
|
| 122 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 123 |
-
|
| 124 |
try:
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
| 128 |
except Exception as e:
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
if not answers_payload:
|
| 133 |
print("Agent did not produce any answers to submit.")
|
| 134 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 135 |
-
|
| 136 |
# 4. Prepare Submission
|
| 137 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 138 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 139 |
print(status_update)
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 145 |
-
response.raise_for_status()
|
| 146 |
-
result_data = response.json()
|
| 147 |
-
final_status = (
|
| 148 |
-
f"Submission Successful!\n"
|
| 149 |
-
f"User: {result_data.get('username')}\n"
|
| 150 |
-
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
| 151 |
-
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 152 |
-
f"Message: {result_data.get('message', 'No message received.')}"
|
| 153 |
-
)
|
| 154 |
-
print("Submission successful.")
|
| 155 |
-
results_df = pd.DataFrame(results_log)
|
| 156 |
-
return final_status, results_df
|
| 157 |
-
except requests.exceptions.HTTPError as e:
|
| 158 |
-
error_detail = f"Server responded with status {e.response.status_code}."
|
| 159 |
try:
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
print(status_message)
|
| 181 |
-
|
| 182 |
-
return status_message, results_df
|
| 183 |
|
| 184 |
|
| 185 |
# --- Build Gradio Interface using Blocks ---
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
+
# Transformers and torch might not be directly needed by BasicAgent if smolgent handles its own LLM
|
| 5 |
+
# from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 6 |
+
# import torch
|
| 7 |
+
import asyncio
|
| 8 |
+
import aiohttp
|
| 9 |
+
|
| 10 |
+
from smolagents import CodeAgent, HfApiModel, DuckDuckGoSearchTool # BaseTool might not be needed if DuckDuckGoSearchTool is used directly
|
| 11 |
|
|
|
|
| 12 |
# --- Constants ---
|
| 13 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 14 |
|
|
|
|
| 16 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 17 |
class BasicAgent:
|
| 18 |
def __init__(self):
|
| 19 |
+
print("BasicAgent initialized to use smolagents.")
|
| 20 |
+
|
| 21 |
+
# --- smolagents Initialization ---
|
| 22 |
try:
|
| 23 |
+
# Use the provided DuckDuckGoSearchTool from smolagents
|
| 24 |
+
# You might need to pass configuration to it if required by the library
|
| 25 |
+
# e.g., API keys if it were a different search tool. For DuckDuckGo, often no key is needed.
|
| 26 |
+
search_tool = DuckDuckGoSearchTool()
|
| 27 |
+
|
| 28 |
+
# Configure the LLM for smolagents
|
| 29 |
+
# HfApiModel is used for Hugging Face Inference API models
|
| 30 |
+
# Ensure you have your HUGGING_FACE_HUB_TOKEN environment variable set if using private/gated models
|
| 31 |
+
# or if the model requires it.
|
| 32 |
+
llm_model = HfApiModel(
|
| 33 |
+
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
self.agent_executor = CodeAgent(
|
| 37 |
+
model=llm_model,
|
| 38 |
+
tools=[search_tool],
|
| 39 |
+
)
|
| 40 |
+
print("smolagents AgentExecutor initialized successfully with WebSearchTool.")
|
| 41 |
+
|
| 42 |
+
except ImportError:
|
| 43 |
+
print("ERROR: smolagents library not found. Please ensure 'smolagents' is installed and import paths are correct.")
|
| 44 |
+
raise
|
| 45 |
except Exception as e:
|
| 46 |
+
print(f"Error initializing smolagents: {e}")
|
| 47 |
raise
|
| 48 |
+
# --- END OF smolagents INITIALIZATION REPLACEMENT SECTION ---
|
| 49 |
|
| 50 |
def __call__(self, question: str) -> str:
|
| 51 |
+
print(f"\nAgent (smolagents) received question (first 100 chars): {question[:100]}...")
|
| 52 |
try:
|
| 53 |
+
response = self.agent_executor.run(question)
|
| 54 |
+
|
| 55 |
+
print(f"Agent (smolagents) returning answer (first 50 chars): {str(response)[:50]}...")
|
| 56 |
+
return str(response) # Ensure the response is a string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
except Exception as e:
|
| 58 |
+
print(f"Error during smolagents agent's __call__: {e}")
|
| 59 |
+
return f"Error: Could not process the question with smolagents. {e}"
|
| 60 |
|
| 61 |
+
async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 62 |
"""
|
| 63 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 64 |
and displays the results.
|
|
|
|
| 87 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 88 |
print(agent_code)
|
| 89 |
|
| 90 |
+
questions_data = []
|
| 91 |
+
async with aiohttp.ClientSession() as session:
|
| 92 |
+
# 2. Fetch Questions
|
| 93 |
+
print(f"Fetching questions from: {questions_url}")
|
| 94 |
+
try:
|
| 95 |
+
async with session.get(questions_url, timeout=aiohttp.ClientTimeout(total=20)) as response:
|
| 96 |
+
response.raise_for_status()
|
| 97 |
+
questions_data = await response.json()
|
| 98 |
+
if not questions_data:
|
| 99 |
+
print("Fetched questions list is empty.")
|
| 100 |
+
return "Fetched questions list is empty or invalid format.", None
|
| 101 |
+
print(f"Fetched {len(questions_data)} questions.")
|
| 102 |
+
except aiohttp.ClientResponseError as e:
|
| 103 |
+
err_msg = f"Error fetching questions (HTTP {e.status}): {e.message}"
|
| 104 |
+
print(err_msg)
|
| 105 |
+
return err_msg, None
|
| 106 |
+
except asyncio.TimeoutError:
|
| 107 |
+
err_msg = "Timeout error fetching questions."
|
| 108 |
+
print(err_msg)
|
| 109 |
+
return err_msg, None
|
| 110 |
+
except aiohttp.ClientError as e: # Includes connection errors, etc.
|
| 111 |
+
err_msg = f"Error fetching questions (ClientError): {e}"
|
| 112 |
+
print(err_msg)
|
| 113 |
+
return err_msg, None
|
| 114 |
+
except Exception as e: # Catch-all for others like JSONDecodeError
|
| 115 |
+
err_msg = f"An unexpected error occurred fetching questions: {e}"
|
| 116 |
+
print(err_msg)
|
| 117 |
+
if 'response' in locals() and hasattr(response, 'text'):
|
| 118 |
+
try:
|
| 119 |
+
response_text_snippet = (await response.text())[:500]
|
| 120 |
+
print(f"Response text snippet: {response_text_snippet}")
|
| 121 |
+
except: pass
|
| 122 |
+
return err_msg, None
|
| 123 |
+
|
| 124 |
+
# 3. Run your Agent Concurrently
|
| 125 |
results_log = []
|
| 126 |
answers_payload = []
|
| 127 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 128 |
+
|
| 129 |
+
async def process_question(item, agent_instance):
|
| 130 |
task_id = item.get("task_id")
|
| 131 |
question_text = item.get("question")
|
| 132 |
if not task_id or question_text is None:
|
| 133 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 134 |
+
return None, None
|
| 135 |
try:
|
| 136 |
+
loop = asyncio.get_event_loop()
|
| 137 |
+
# Run the synchronous agent call in a thread pool executor
|
| 138 |
+
submitted_answer = await loop.run_in_executor(None, agent_instance, question_text)
|
| 139 |
+
return {"task_id": task_id, "submitted_answer": submitted_answer}, \
|
| 140 |
+
{"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer}
|
| 141 |
except Exception as e:
|
| 142 |
+
print(f"Error running agent on task {task_id}: {e}")
|
| 143 |
+
return None, {"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"}
|
| 144 |
+
|
| 145 |
+
tasks = [process_question(item, agent) for item in questions_data]
|
| 146 |
+
processed_results = await asyncio.gather(*tasks)
|
| 147 |
+
|
| 148 |
+
for payload, log_entry in processed_results:
|
| 149 |
+
if payload: answers_payload.append(payload)
|
| 150 |
+
if log_entry: results_log.append(log_entry)
|
| 151 |
|
| 152 |
if not answers_payload:
|
| 153 |
print("Agent did not produce any answers to submit.")
|
| 154 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 155 |
+
return False
|
| 156 |
# 4. Prepare Submission
|
| 157 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 158 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 159 |
print(status_update)
|
| 160 |
|
| 161 |
+
async with aiohttp.ClientSession() as session:
|
| 162 |
+
# 5. Submit
|
| 163 |
+
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
try:
|
| 165 |
+
async with session.post(submit_url, json=submission_data, timeout=aiohttp.ClientTimeout(total=60)) as response:
|
| 166 |
+
response.raise_for_status()
|
| 167 |
+
result_data = await response.json()
|
| 168 |
+
final_status = (
|
| 169 |
+
f"Submission Successful!\n"
|
| 170 |
+
f"User: {result_data.get('username')}\n"
|
| 171 |
+
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
| 172 |
+
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 173 |
+
f"Message: {result_data.get('message', 'No message received.')}"
|
| 174 |
+
)
|
| 175 |
+
print("Submission successful.")
|
| 176 |
+
return final_status, pd.DataFrame(results_log)
|
| 177 |
+
except aiohttp.ClientResponseError as e:
|
| 178 |
+
error_detail = f"Server responded with status {e.status}."
|
| 179 |
+
try:
|
| 180 |
+
# Try to get JSON error detail, then text
|
| 181 |
+
error_body = await e.json()
|
| 182 |
+
error_detail += f" Detail: {error_body.get('detail', str(error_body))}"
|
| 183 |
+
except Exception: # If not JSON or other error
|
| 184 |
+
try:
|
| 185 |
+
error_text = (await response.text())[:500]
|
| 186 |
+
error_detail += f" Response: {error_text}"
|
| 187 |
+
except:
|
| 188 |
+
error_detail += " Could not retrieve error response body."
|
| 189 |
+
status_message = f"Submission Failed: {error_detail}"
|
| 190 |
+
except asyncio.TimeoutError:
|
| 191 |
+
status_message = "Submission Failed: The request timed out."
|
| 192 |
+
except aiohttp.ClientError as e:
|
| 193 |
+
status_message = f"Submission Failed: Network error - {e}"
|
| 194 |
+
except Exception as e:
|
| 195 |
+
status_message = f"An unexpected error occurred during submission: {e}"
|
| 196 |
+
|
| 197 |
print(status_message)
|
| 198 |
+
return status_message, pd.DataFrame(results_log)
|
|
|
|
| 199 |
|
| 200 |
|
| 201 |
# --- Build Gradio Interface using Blocks ---
|
requirements.txt
CHANGED
|
@@ -1,4 +1,7 @@
|
|
| 1 |
gradio
|
| 2 |
requests
|
| 3 |
torch
|
| 4 |
-
transformers
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
requests
|
| 3 |
torch
|
| 4 |
+
transformers
|
| 5 |
+
duckduckgo-search
|
| 6 |
+
aiohttp
|
| 7 |
+
smolgent # Add smolgent
|