Thanh Vinh Vo
commited on
Commit
·
b4ab959
1
Parent(s):
736a9c9
update
Browse files
app.py
CHANGED
|
@@ -23,64 +23,12 @@ from smolagents import (
|
|
| 23 |
# --- Constants ---
|
| 24 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 25 |
|
| 26 |
-
|
| 27 |
@tool
|
| 28 |
-
def
|
| 29 |
"""
|
| 30 |
-
A tool that downloads
|
| 31 |
-
Args:
|
| 32 |
-
file_name: File name.
|
| 33 |
-
Returns:
|
| 34 |
-
str: Local file path where the image was saved.
|
| 35 |
-
"""
|
| 36 |
-
import requests
|
| 37 |
-
import os
|
| 38 |
-
|
| 39 |
-
url = f"{DEFAULT_API_URL}/files/{file_name}"
|
| 40 |
-
print(f"Fetching image from URL: {url}")
|
| 41 |
-
|
| 42 |
-
# Create downloads directory if it doesn't exist
|
| 43 |
-
downloads_dir = "downloaded_images"
|
| 44 |
-
os.makedirs(downloads_dir, exist_ok=True)
|
| 45 |
-
|
| 46 |
-
response = None
|
| 47 |
-
try:
|
| 48 |
-
response = requests.get(url, timeout=30)
|
| 49 |
-
response.raise_for_status() # Raises an HTTPError for bad responses
|
| 50 |
-
|
| 51 |
-
# Check if response is empty
|
| 52 |
-
if not response.content:
|
| 53 |
-
raise ValueError(f"Empty response received from {url}")
|
| 54 |
-
|
| 55 |
-
# Check content type
|
| 56 |
-
content_type = response.headers.get('content-type', '').lower()
|
| 57 |
-
print(f"Response content-type: {content_type}")
|
| 58 |
-
print(f"Response content length: {len(response.content)} bytes")
|
| 59 |
-
|
| 60 |
-
# Use original filename directly
|
| 61 |
-
local_path = os.path.join(downloads_dir, file_name)
|
| 62 |
-
|
| 63 |
-
# Save the image to local file
|
| 64 |
-
with open(local_path, 'wb') as f:
|
| 65 |
-
f.write(response.content)
|
| 66 |
-
|
| 67 |
-
print(f"Image saved to: {local_path}")
|
| 68 |
-
return local_path
|
| 69 |
-
|
| 70 |
-
except requests.exceptions.RequestException as e:
|
| 71 |
-
raise ValueError(f"Failed to fetch image from {url}: {e}")
|
| 72 |
-
except Exception as e:
|
| 73 |
-
# Print first 200 characters of response content for debugging
|
| 74 |
-
content_preview = response.content[:200] if response and hasattr(response, 'content') else b"No response"
|
| 75 |
-
print(f"Error downloading image. Content preview: {content_preview}")
|
| 76 |
-
raise ValueError(f"Failed to download image from {url}: {e}")
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
@tool
|
| 80 |
-
def get_text_file(file_name: str) -> str:
|
| 81 |
-
"""
|
| 82 |
-
A tool that downloads a text file (such as code) from file name and saves it locally.
|
| 83 |
Args:
|
|
|
|
| 84 |
file_name: File name.
|
| 85 |
Returns:
|
| 86 |
str: Local file path where the text was saved.
|
|
@@ -88,7 +36,7 @@ def get_text_file(file_name: str) -> str:
|
|
| 88 |
import requests
|
| 89 |
import os
|
| 90 |
|
| 91 |
-
url = f"{DEFAULT_API_URL}/files/{
|
| 92 |
print(f"Fetching text file from URL: {url}")
|
| 93 |
|
| 94 |
# Create downloads directory if it doesn't exist
|
|
@@ -134,7 +82,7 @@ class BasicAgent:
|
|
| 134 |
def __init__(self):
|
| 135 |
print("BasicAgent initialized.")
|
| 136 |
self.multimodal_agent = CodeAgent(
|
| 137 |
-
tools=[VisitWebpageTool(), DuckDuckGoSearchTool()],
|
| 138 |
model= OpenAIServerModel(model_id="gpt-4o"),
|
| 139 |
additional_authorized_imports=[
|
| 140 |
"requests",
|
|
@@ -151,19 +99,58 @@ class BasicAgent:
|
|
| 151 |
],
|
| 152 |
name="multimodal_agent",
|
| 153 |
description="""
|
| 154 |
-
|
| 155 |
verbosity_level=0,
|
| 156 |
max_steps=10,
|
| 157 |
)
|
| 158 |
|
| 159 |
-
self.
|
| 160 |
-
tools=[VisitWebpageTool(), DuckDuckGoSearchTool(),
|
| 161 |
model=InferenceClientModel(
|
| 162 |
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 163 |
),
|
| 164 |
-
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
additional_authorized_imports=[
|
| 168 |
"requests",
|
| 169 |
"bs4",
|
|
@@ -180,17 +167,18 @@ class BasicAgent:
|
|
| 180 |
"cv2",
|
| 181 |
"numpy",
|
| 182 |
"chess.engine",
|
| 183 |
-
"html5lib"
|
| 184 |
],
|
|
|
|
| 185 |
max_steps=15,
|
| 186 |
)
|
| 187 |
|
| 188 |
-
def __call__(self, question: str, question_id: str,
|
| 189 |
print(f"Agent received question: {question}")
|
|
|
|
| 190 |
prompt = f"""
|
| 191 |
-
Answer the following question:
|
| 192 |
-
"{question}
|
| 193 |
-
|
| 194 |
1. `pandas` Python package is provided. Please try to use it FIRST when there is need to extract structured data (such as tables) from HTML content.
|
| 195 |
2. `wikipedia` Python package is provided to interact with Wikipedia. Try to work with raw wikipedia HTML content and use `pandas` to parse first.
|
| 196 |
3. `chess` Python package is provided. Please use it when there is need to solve chess problems.
|
|
|
|
| 23 |
# --- Constants ---
|
| 24 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 25 |
|
|
|
|
| 26 |
@tool
|
| 27 |
+
def get_file(question_id: str, file_name: str) -> str:
|
| 28 |
"""
|
| 29 |
+
A tool that downloads a file that was mentioned in a question.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
Args:
|
| 31 |
+
question_id: Question ID.
|
| 32 |
file_name: File name.
|
| 33 |
Returns:
|
| 34 |
str: Local file path where the text was saved.
|
|
|
|
| 36 |
import requests
|
| 37 |
import os
|
| 38 |
|
| 39 |
+
url = f"{DEFAULT_API_URL}/files/{question_id}"
|
| 40 |
print(f"Fetching text file from URL: {url}")
|
| 41 |
|
| 42 |
# Create downloads directory if it doesn't exist
|
|
|
|
| 82 |
def __init__(self):
|
| 83 |
print("BasicAgent initialized.")
|
| 84 |
self.multimodal_agent = CodeAgent(
|
| 85 |
+
tools=[VisitWebpageTool(), DuckDuckGoSearchTool(), get_file],
|
| 86 |
model= OpenAIServerModel(model_id="gpt-4o"),
|
| 87 |
additional_authorized_imports=[
|
| 88 |
"requests",
|
|
|
|
| 99 |
],
|
| 100 |
name="multimodal_agent",
|
| 101 |
description="""
|
| 102 |
+
This agent can reason across audio, vision, and text""",
|
| 103 |
verbosity_level=0,
|
| 104 |
max_steps=10,
|
| 105 |
)
|
| 106 |
|
| 107 |
+
self.code_agent = CodeAgent(
|
| 108 |
+
tools=[VisitWebpageTool(), DuckDuckGoSearchTool(), get_file],
|
| 109 |
model=InferenceClientModel(
|
| 110 |
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 111 |
),
|
| 112 |
+
additional_authorized_imports=[
|
| 113 |
+
"requests",
|
| 114 |
+
"bs4",
|
| 115 |
+
"markdownify",
|
| 116 |
+
"wikipedia",
|
| 117 |
+
"pandas",
|
| 118 |
+
"io",
|
| 119 |
+
"PIL",
|
| 120 |
+
"chess",
|
| 121 |
+
"img2text",
|
| 122 |
+
"chess.pgn",
|
| 123 |
+
"PIL.Image",
|
| 124 |
+
"bytes",
|
| 125 |
+
"cv2",
|
| 126 |
+
"numpy",
|
| 127 |
+
"chess.engine",
|
| 128 |
],
|
| 129 |
+
name="code_agent",
|
| 130 |
+
description="""
|
| 131 |
+
This agent specializes at:
|
| 132 |
+
- Writing code to solve problem.
|
| 133 |
+
- Browse the web to find information.
|
| 134 |
+
- Solving chess problems.
|
| 135 |
+
This agent follow rules below when possible:
|
| 136 |
+
1. `pandas` Python package is provided. Please try to use it FIRST when there is need to extract structured data (such as tables) from HTML content.
|
| 137 |
+
2. `wikipedia` Python package is provided to interact with Wikipedia. Try to work with raw wikipedia HTML content and use `pandas` to parse first.
|
| 138 |
+
3. `chess` Python package is provided. Please use it when there is need to solve chess problems.
|
| 139 |
+
4. Please take the question literally! Do not add any additional information or assumptions.
|
| 140 |
+
|
| 141 |
+
""",
|
| 142 |
+
verbosity_level=0,
|
| 143 |
+
max_steps=10,
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
self.manager_agent = CodeAgent(
|
| 147 |
+
model=InferenceClientModel(
|
| 148 |
+
"Qwen/Qwen2.5-32B-Instruct"
|
| 149 |
+
),
|
| 150 |
+
tools=[get_file, VisitWebpageTool(), DuckDuckGoSearchTool()],
|
| 151 |
+
managed_agents=[
|
| 152 |
+
self.multimodal_agent,
|
| 153 |
+
self.code_agent],
|
| 154 |
additional_authorized_imports=[
|
| 155 |
"requests",
|
| 156 |
"bs4",
|
|
|
|
| 167 |
"cv2",
|
| 168 |
"numpy",
|
| 169 |
"chess.engine",
|
|
|
|
| 170 |
],
|
| 171 |
+
planning_interval=5,
|
| 172 |
max_steps=15,
|
| 173 |
)
|
| 174 |
|
| 175 |
+
def __call__(self, question: str, question_id: str, file_name: str) -> str:
|
| 176 |
print(f"Agent received question: {question}")
|
| 177 |
+
file = f"Mentioned file: {file_name}" if file_name else ""
|
| 178 |
prompt = f"""
|
| 179 |
+
Answer the following question (question_id is {question_id}):):
|
| 180 |
+
"{question}""{file}"
|
| 181 |
+
Please follow hints below:
|
| 182 |
1. `pandas` Python package is provided. Please try to use it FIRST when there is need to extract structured data (such as tables) from HTML content.
|
| 183 |
2. `wikipedia` Python package is provided to interact with Wikipedia. Try to work with raw wikipedia HTML content and use `pandas` to parse first.
|
| 184 |
3. `chess` Python package is provided. Please use it when there is need to solve chess problems.
|