Sudheer Tripathi commited on
Commit ·
3f8a628
1
Parent(s): 81917a3
feat: agent with uvlock setup
Browse files- .gitignore +3 -0
- .python-version +1 -0
- agent.py +57 -0
- app.py +12 -15
- pyproject.toml +14 -0
- tools.py +13 -0
- uv.lock +0 -0
.gitignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.env
|
| 2 |
+
__pycache__/
|
| 3 |
+
questions.log
|
.python-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
3.13
|
agent.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
+
from smolagents import CodeAgent, InferenceClientModel, DuckDuckGoSearchTool
|
| 5 |
+
from tools import GetCurrentDateTool
|
| 6 |
+
|
| 7 |
+
model = InferenceClientModel(
|
| 8 |
+
model_id="Qwen/Qwen3-Next-80B-A3B-Thinking"
|
| 9 |
+
#model_id="Qwen/Qwen3.5-9B"
|
| 10 |
+
)
|
| 11 |
+
search_tool = DuckDuckGoSearchTool()
|
| 12 |
+
date_tool=GetCurrentDateTool()
|
| 13 |
+
|
| 14 |
+
QUESTIONS_LOG = Path(__file__).parent / "questions.log"
|
| 15 |
+
|
| 16 |
+
class LoggingCodeAgent(CodeAgent):
|
| 17 |
+
def run(self, task, *args, **kwargs):
|
| 18 |
+
try:
|
| 19 |
+
with QUESTIONS_LOG.open("a", encoding="utf-8") as f:
|
| 20 |
+
f.write(f"[{datetime.now().isoformat()}] Q: {task}\n")
|
| 21 |
+
except Exception as e:
|
| 22 |
+
print(f"Failed to log question: {e}")
|
| 23 |
+
answer = super().run(task, *args, **kwargs)
|
| 24 |
+
try:
|
| 25 |
+
with QUESTIONS_LOG.open("a", encoding="utf-8") as f:
|
| 26 |
+
f.write(f"[{datetime.now().isoformat()}] A: {answer}\n")
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print(f"Failed to log answer: {e}")
|
| 29 |
+
return answer
|
| 30 |
+
|
| 31 |
+
codeAgent = LoggingCodeAgent(
|
| 32 |
+
max_steps=10,
|
| 33 |
+
model=model,
|
| 34 |
+
tools=[ search_tool, date_tool ],
|
| 35 |
+
additional_authorized_imports=[
|
| 36 |
+
'time',
|
| 37 |
+
'itertools',
|
| 38 |
+
'random',
|
| 39 |
+
'statistics',
|
| 40 |
+
'stat',
|
| 41 |
+
'datetime',
|
| 42 |
+
'queue',
|
| 43 |
+
'unicodedata',
|
| 44 |
+
'math',
|
| 45 |
+
'collections',
|
| 46 |
+
're'
|
| 47 |
+
]
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# response = agent.run(
|
| 51 |
+
# """
|
| 52 |
+
# First use python to get current date. Then answer questions.
|
| 53 |
+
# Who is the president of India as of today ?
|
| 54 |
+
# """
|
| 55 |
+
# )
|
| 56 |
+
|
| 57 |
+
# print(response, date_tool)
|
app.py
CHANGED
|
@@ -1,27 +1,19 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
-
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
| 6 |
|
|
|
|
|
|
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
-
# --- Basic Agent Definition ---
|
| 12 |
-
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
-
class BasicAgent:
|
| 14 |
-
def __init__(self):
|
| 15 |
-
print("BasicAgent initialized.")
|
| 16 |
-
def __call__(self, question: str) -> str:
|
| 17 |
-
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 18 |
-
fixed_answer = "This is a default answer."
|
| 19 |
-
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 20 |
-
return fixed_answer
|
| 21 |
-
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
| 24 |
-
Fetches all questions, runs the
|
| 25 |
and displays the results.
|
| 26 |
"""
|
| 27 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
|
@@ -40,7 +32,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 40 |
|
| 41 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 42 |
try:
|
| 43 |
-
agent =
|
| 44 |
except Exception as e:
|
| 45 |
print(f"Error instantiating agent: {e}")
|
| 46 |
return f"Error initializing agent: {e}", None
|
|
@@ -73,14 +65,19 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 73 |
results_log = []
|
| 74 |
answers_payload = []
|
| 75 |
print(f"Running agent on {len(questions_data)} questions...")
|
|
|
|
| 76 |
for item in questions_data:
|
| 77 |
task_id = item.get("task_id")
|
| 78 |
question_text = item.get("question")
|
|
|
|
| 79 |
if not task_id or question_text is None:
|
| 80 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 81 |
continue
|
|
|
|
|
|
|
|
|
|
| 82 |
try:
|
| 83 |
-
submitted_answer = agent(question_text)
|
| 84 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 85 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 86 |
except Exception as e:
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
from agent import codeAgent
|
| 7 |
|
| 8 |
+
# Load variables from .env into the environment
|
| 9 |
+
load_dotenv()
|
| 10 |
# (Keep Constants as is)
|
| 11 |
# --- Constants ---
|
| 12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 15 |
"""
|
| 16 |
+
Fetches all questions, runs the agent on them, submits all answers,
|
| 17 |
and displays the results.
|
| 18 |
"""
|
| 19 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
|
|
|
| 32 |
|
| 33 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 34 |
try:
|
| 35 |
+
agent = codeAgent
|
| 36 |
except Exception as e:
|
| 37 |
print(f"Error instantiating agent: {e}")
|
| 38 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 65 |
results_log = []
|
| 66 |
answers_payload = []
|
| 67 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 68 |
+
# questions_data = questions_data[5:10] # Limit to first few questions for testing; remove or adjust as needed
|
| 69 |
for item in questions_data:
|
| 70 |
task_id = item.get("task_id")
|
| 71 |
question_text = item.get("question")
|
| 72 |
+
file = item.get("file_name")
|
| 73 |
if not task_id or question_text is None:
|
| 74 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 75 |
continue
|
| 76 |
+
if file != "":
|
| 77 |
+
print(f"Skipping task as files aren't accessible right now: Task: {task_id}, File: {file}")
|
| 78 |
+
continue
|
| 79 |
try:
|
| 80 |
+
submitted_answer = agent.run(question_text)
|
| 81 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 82 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 83 |
except Exception as e:
|
pyproject.toml
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "agents-course-final-assignment"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
description = "Add your description here"
|
| 5 |
+
readme = "README.md"
|
| 6 |
+
requires-python = ">=3.10"
|
| 7 |
+
dependencies = [
|
| 8 |
+
"ddgs>=9.14.1",
|
| 9 |
+
"gradio[oauth]>=4.44.1",
|
| 10 |
+
"huggingface-hub>=0.24,<1.0",
|
| 11 |
+
"python-dotenv>=1.2.2",
|
| 12 |
+
"requests>=2.32.5",
|
| 13 |
+
"smolagents>=1.24.0",
|
| 14 |
+
]
|
tools.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import Tool
|
| 2 |
+
import datetime
|
| 3 |
+
|
| 4 |
+
class GetCurrentDateTool(Tool):
|
| 5 |
+
name="get_current_date"
|
| 6 |
+
description="""
|
| 7 |
+
This tool returns the current date in YYYY-MM-DD format
|
| 8 |
+
"""
|
| 9 |
+
inputs={}
|
| 10 |
+
output_type = "string"
|
| 11 |
+
def forward(self) -> str:
|
| 12 |
+
return datetime.datetime.date()
|
| 13 |
+
|
uv.lock
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|