Spaces:
Sleeping
Sleeping
download attached files
Browse files
agent.py
CHANGED
|
@@ -1,8 +1,10 @@
|
|
| 1 |
import math
|
| 2 |
import os
|
|
|
|
| 3 |
|
| 4 |
import numexpr
|
| 5 |
from langchain_community.document_loaders import ArxivLoader
|
|
|
|
| 6 |
from smolagents import (
|
| 7 |
CodeAgent,
|
| 8 |
GoogleSearchTool,
|
|
@@ -12,6 +14,7 @@ from smolagents import (
|
|
| 12 |
)
|
| 13 |
|
| 14 |
hf_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
@tool
|
|
@@ -56,9 +59,31 @@ def arxiv_search_tool(query: str) -> str:
|
|
| 56 |
return "No content found"
|
| 57 |
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
tools = [
|
| 60 |
calculator,
|
| 61 |
arxiv_search_tool,
|
|
|
|
| 62 |
GoogleSearchTool(provider="serper"),
|
| 63 |
WikipediaSearchTool(),
|
| 64 |
]
|
|
|
|
| 1 |
import math
|
| 2 |
import os
|
| 3 |
+
import re
|
| 4 |
|
| 5 |
import numexpr
|
| 6 |
from langchain_community.document_loaders import ArxivLoader
|
| 7 |
+
import requests
|
| 8 |
from smolagents import (
|
| 9 |
CodeAgent,
|
| 10 |
GoogleSearchTool,
|
|
|
|
| 14 |
)
|
| 15 |
|
| 16 |
hf_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 17 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 18 |
|
| 19 |
|
| 20 |
@tool
|
|
|
|
| 59 |
return "No content found"
|
| 60 |
|
| 61 |
|
| 62 |
+
@tool
|
| 63 |
+
def download_attached_files(task_id: str) -> str:
|
| 64 |
+
"""
|
| 65 |
+
Get the first attached file if exists and save it. Returns the filename.
|
| 66 |
+
Args:
|
| 67 |
+
task_id: this id of the task
|
| 68 |
+
"""
|
| 69 |
+
api_url = DEFAULT_API_URL
|
| 70 |
+
file_url = f"{api_url}/files/{task_id}"
|
| 71 |
+
response = requests.get(file_url, timeout=15)
|
| 72 |
+
if response.status_code == 200:
|
| 73 |
+
cd = response.headers.get("content-disposition")
|
| 74 |
+
if cd:
|
| 75 |
+
fname = re.findall("filename=(.+)", cd)
|
| 76 |
+
if len(fname) > 0:
|
| 77 |
+
with open(fname[0], "wb") as file:
|
| 78 |
+
file.write(response.content)
|
| 79 |
+
return f"{fname[0]} saved "
|
| 80 |
+
return "no file attached to this task"
|
| 81 |
+
|
| 82 |
+
|
| 83 |
tools = [
|
| 84 |
calculator,
|
| 85 |
arxiv_search_tool,
|
| 86 |
+
download_attached_files,
|
| 87 |
GoogleSearchTool(provider="serper"),
|
| 88 |
WikipediaSearchTool(),
|
| 89 |
]
|
app.py
CHANGED
|
@@ -16,10 +16,10 @@ class BasicAgent:
|
|
| 16 |
def __init__(self):
|
| 17 |
print("BasicAgent initialized.")
|
| 18 |
|
| 19 |
-
def __call__(self, question: str) -> str:
|
| 20 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 21 |
# fixed_answer = "This is a default answer."
|
| 22 |
-
answer = dexter.run(question)
|
| 23 |
print(f"Agent returning fixed answer: {answer}")
|
| 24 |
return answer
|
| 25 |
|
|
@@ -85,7 +85,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 85 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 86 |
continue
|
| 87 |
try:
|
| 88 |
-
submitted_answer = agent(question_text)
|
| 89 |
answers_payload.append(
|
| 90 |
{"task_id": task_id, "submitted_answer": submitted_answer}
|
| 91 |
)
|
|
@@ -220,4 +220,3 @@ if __name__ == "__main__":
|
|
| 220 |
|
| 221 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 222 |
demo.launch(debug=True, share=False)
|
| 223 |
-
|
|
|
|
| 16 |
def __init__(self):
|
| 17 |
print("BasicAgent initialized.")
|
| 18 |
|
| 19 |
+
def __call__(self, question: str, task_id: str) -> str:
|
| 20 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 21 |
# fixed_answer = "This is a default answer."
|
| 22 |
+
answer = dexter.run(f"task_id: {task_id}" + "\n" + question)
|
| 23 |
print(f"Agent returning fixed answer: {answer}")
|
| 24 |
return answer
|
| 25 |
|
|
|
|
| 85 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 86 |
continue
|
| 87 |
try:
|
| 88 |
+
submitted_answer = agent(question_text, task_id)
|
| 89 |
answers_payload.append(
|
| 90 |
{"task_id": task_id, "submitted_answer": submitted_answer}
|
| 91 |
)
|
|
|
|
| 220 |
|
| 221 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 222 |
demo.launch(debug=True, share=False)
|
|
|