Spaces:
Runtime error
Runtime error
File size: 6,194 Bytes
020caac 16d170b 020caac 16d170b 020caac 16d170b 0ae933c 16d170b 020caac 9874d5f 0ae933c 020caac 12c8c7d 020caac |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# agent.py
import os
import json
import pandas as pd
from smolagents import (
CodeAgent,
LiteLLMModel,
DuckDuckGoSearchTool,
FinalAnswerTool,
VisitWebpageTool,
WikipediaSearchTool,
WebSearchTool,
tool,
OpenAIServerModel
)
from langchain_community.document_loaders import ArxivLoader
from dotenv import load_dotenv
import requests
import yaml
load_dotenv()
# Custom tools
@tool
def arxiv_search(query: str) -> str:
"""
Search Arxiv for a query and return up to 3 documents.
Args:
query (str): The search query to run on Arxiv.
Returns:
str: Formatted Arxiv document summaries.
"""
search_docs = ArxivLoader(query=query, load_max_docs=3).load()
return "\n\n---\n\n".join([
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content[:1000]}\n</Document>'
for doc in search_docs
])
@tool
def extract_text_from_image(image_path: str) -> str:
"""
Extract text from an image using pytesseract.
Args:
image_path (str): Path to the image file.
Returns:
str: Extracted text or error message.
"""
try:
import pytesseract
from PIL import Image
image = Image.open(image_path)
text = pytesseract.image_to_string(image)
return f"Extracted text from image:\n\n{text}"
except ImportError:
return "Error: pytesseract is not installed."
except Exception as e:
return f"Error extracting text: {str(e)}"
@tool
def read_python_file(file_name: str) -> str:
"""
Read a Python (.py) file and return its content.
Args:
file_name (str): The file name of the Python script to read.
Returns:
str: The contents of the file as a string.
"""
base_path = "data/question_files"
with open(os.path.join(base_path, file_name), "r") as f:
return f.read()
@tool
def get_youtube_transcript(video_id:str)-> str:
"""
Retrieves the transcript for a given YouTube video.
Args:
video_id: The ID of the YouTube video.
Returns:
A list of dictionaries, where each dictionary represents a transcript segment
and contains 'text' and 'start' keys, or None if no transcript is found.
"""
try:
transcript = YouTubeTranscriptApi.get_transcript(video_id)
return transcript
except Exception as e:
# print(f"Error getting transcript: {e}")
return f"Error getting transcript: {e}"
@tool
def read_excel_file(file_name: str) -> str:
"""
Read an Excel (.xlsx) file and return its tabular content as a string.
Args:
file_name (str): The Excel file to read.
Returns:
str: The content of the Excel file in plain text format.
"""
base_path = "data/question_files"
df = pd.read_excel(os.path.join(base_path, file_name))
return df.to_string()
# Model and agent setup
API_KEY = os.getenv("OPENAI_API_KEY_AG")
MODEL_ID = "openai/gpt-4.1-nano"
model = OpenAIServerModel(model_id="gpt-4.1-nano", api_key=API_KEY)
agent = CodeAgent(
model=model,
tools=[
WebSearchTool(),
VisitWebpageTool(),
WikipediaSearchTool(),
arxiv_search,
FinalAnswerTool(),
extract_text_from_image,
read_excel_file,
read_python_file,
get_youtube_transcript,
],
planning_interval=3,
max_steps=10,
verbosity_level=-1,
additional_authorized_imports=[
"pandas", "numpy", "requests", "os", "math", "sympy", "scipy",
"markdownify", "unicodedata", "stat", "datetime", "random", "itertools",
"statistics", "queue", "time", "collections", "re"
],
add_base_tools=True
)
def fetch_questions():
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
try:
response = requests.get(f"{DEFAULT_API_URL}/questions")
response.raise_for_status()
data = response.json()
return data
except Exception as e:
print(f"Error fetching questions: {e}")
return []
def fetch_file(task_id: str, file_name: str):
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
try:
response = requests.get(f"{DEFAULT_API_URL}/files/{task_id}")
response.raise_for_status()
os.makedirs("data/question_files", exist_ok=True)
path = f"data/question_files/{file_name}"
with open(path, "wb") as f:
f.write(response.content)
return path
except Exception as e:
print(f"Error fetching file: {e}")
return None
def run_agent_on_question(q):
if q.get("file_name"):
file_path = fetch_file(q["task_id"], q["file_name"])
prompt = f"""You are a general AI assistant. Use tools and web search as needed.
Question: {q['question']}
file_path: {file_path}
YOUR FINAL ANSWER should be a number OR few words OR comma-separated values. Follow instructions strictly."""
else:
prompt = f"""You are a general AI assistant. Use tools and web search as needed.
Question: {q['question']}
YOUR FINAL ANSWER should be a number OR few words OR comma-separated values. Follow instructions strictly."""
output = agent.run(prompt)
# Optional: Extract final answer if embedded in logs
if isinstance(output, str):
return output.strip()
elif isinstance(output, dict):
return output.get("final_answer", str(output))
else:
return str(output)
def submit_answers(answers):
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
request_payload = {
"username": "GoReed",
"agent_code": "test_answers_agent_code",
"answers": answers
}
try:
response = requests.post(
f"{DEFAULT_API_URL}/submit",
json=request_payload # β
FIXED
)
response.raise_for_status()
print("β
Submission success:", response.json())
except requests.exceptions.HTTPError as http_err:
print(f"β HTTP Error: {http_err}")
print("Response text:", response.text)
except Exception as e:
print(f"β Submission Error: {e}")
|