Spaces:
Sleeping
Sleeping
first try
Browse files
agent.py
ADDED
File without changes
|
app.py
CHANGED
@@ -1,24 +1,33 @@
|
|
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 |
-
|
|
|
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 BasicAgent on them, submits all answers,
|
@@ -45,8 +54,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
45 |
print(f"Error instantiating agent: {e}")
|
46 |
return f"Error initializing agent: {e}", None
|
47 |
# In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
|
48 |
-
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
49 |
-
print(agent_code)
|
50 |
|
51 |
# 2. Fetch Questions
|
52 |
print(f"Fetching questions from: {questions_url}")
|
@@ -72,6 +81,32 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
72 |
# 3. Run your Agent
|
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")
|
@@ -80,7 +115,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
80 |
print(f"Skipping item with missing task_id or question: {item}")
|
81 |
continue
|
82 |
try:
|
83 |
-
|
|
|
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 |
+
from smolagents import CodeAgent, InferenceClientModel, tool,ToolCallingAgent , PythonInterpreterTool,DuckDuckGoSearchTool,VisitWebpageTool
|
5 |
import inspect
|
6 |
import pandas as pd
|
7 |
|
8 |
# (Keep Constants as is)
|
9 |
# --- Constants ---
|
10 |
+
key = os.environ['access_token']
|
11 |
+
|
12 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
13 |
|
14 |
# --- Basic Agent Definition ---
|
15 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
16 |
class BasicAgent:
|
17 |
def __init__(self):
|
18 |
+
model = InferenceClientModel(
|
19 |
+
model_id="Qwen/Qwen3-32B",
|
20 |
+
token=key)
|
21 |
+
interpreter = PythonInterpreterTool()
|
22 |
+
self.agentic = ToolCallingAgent(model=model,tools = [interpreter,DuckDuckGoSearchTool(),VisitWebpageTool()])
|
23 |
print("BasicAgent initialized.")
|
24 |
def __call__(self, question: str) -> str:
|
25 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
26 |
+
answer = self.agentic.run(question)
|
27 |
+
fixed_answer = answer
|
28 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
29 |
return fixed_answer
|
30 |
+
|
31 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
32 |
"""
|
33 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
|
54 |
print(f"Error instantiating agent: {e}")
|
55 |
return f"Error initializing agent: {e}", None
|
56 |
# In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
|
57 |
+
# agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
58 |
+
# print(agent_code)
|
59 |
|
60 |
# 2. Fetch Questions
|
61 |
print(f"Fetching questions from: {questions_url}")
|
|
|
81 |
# 3. Run your Agent
|
82 |
results_log = []
|
83 |
answers_payload = []
|
84 |
+
sytem_prompt = """You are an intelligent machine capable of reasoning and solving complex problems by breaking them down into smaller, manageable subtasks.
|
85 |
+
|
86 |
+
Your thought process follows this structured approach:
|
87 |
+
|
88 |
+
Understand the main concept or problem.
|
89 |
+
|
90 |
+
Check if additional context or clarification is needed.
|
91 |
+
|
92 |
+
Decompose the problem into relevant subtasks, if necessary.
|
93 |
+
|
94 |
+
Use the available data to explore at least three potential solution strategies.
|
95 |
+
|
96 |
+
Evaluate and select the most effective solution.
|
97 |
+
|
98 |
+
Clearly explain the reasoning and methodology behind the chosen approach.
|
99 |
+
|
100 |
+
Apply the selected solution using the data to resolve the original problem.
|
101 |
+
|
102 |
+
You can use tools [interpreter] for calculation by generating a python code for calculus
|
103 |
+
Here re others tool at your disposal [DuckDuckGoSearchTool,VisitWebpageTool]
|
104 |
+
|
105 |
+
here is the task
|
106 |
+
{}
|
107 |
+
"""
|
108 |
+
|
109 |
+
|
110 |
print(f"Running agent on {len(questions_data)} questions...")
|
111 |
for item in questions_data:
|
112 |
task_id = item.get("task_id")
|
|
|
115 |
print(f"Skipping item with missing task_id or question: {item}")
|
116 |
continue
|
117 |
try:
|
118 |
+
prompt = sytem_prompt.format(question_text)
|
119 |
+
submitted_answer = agent(prompt)
|
120 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
121 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
122 |
except Exception as e:
|
requirements.txt
CHANGED
@@ -1,2 +1,6 @@
|
|
1 |
gradio
|
2 |
-
requests
|
|
|
|
|
|
|
|
|
|
1 |
gradio
|
2 |
+
requests
|
3 |
+
smolagents
|
4 |
+
mistralai
|
5 |
+
duckduckgo-search
|
6 |
+
markdownify
|
tools.py
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
from smolagents import CodeAgent, InferenceClientModel, tool
|
2 |
+
|
3 |
+
|