Spaces:
Sleeping
Sleeping
Upload 6 files
Browse files- agent.py +25 -8
- app.py +8 -1
- system_prompt.txt +5 -0
agent.py
CHANGED
@@ -30,6 +30,15 @@ from tools import (
|
|
30 |
SimpleOCRTool,
|
31 |
)
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
# ---------------------------------------------------------------------------
|
34 |
# Model selection helper
|
35 |
# ---------------------------------------------------------------------------
|
@@ -45,13 +54,21 @@ def _select_model():
|
|
45 |
from smolagents import InferenceClientModel
|
46 |
hf_model_id = os.getenv("HF_MODEL", "HuggingFaceH4/zephyr-7b-beta")
|
47 |
hf_token = os.getenv("HF_API_KEY")
|
48 |
-
return InferenceClientModel(
|
|
|
|
|
|
|
|
|
49 |
|
50 |
if provider == "openai":
|
51 |
from smolagents import OpenAIServerModel
|
52 |
openai_model_id = os.getenv("OPENAI_MODEL", "gpt-3.5-turbo")
|
53 |
openai_token = os.getenv("OPENAI_API_KEY")
|
54 |
-
return OpenAIServerModel(
|
|
|
|
|
|
|
|
|
55 |
|
56 |
raise ValueError(
|
57 |
f"Unsupported MODEL_PROVIDER: {provider!r}. "
|
@@ -78,16 +95,16 @@ class GAIAAgent(CodeAgent):
|
|
78 |
# Convenience so the object itself can be *called* directly
|
79 |
def __call__(self, question: str, **kwargs: Any) -> str:
|
80 |
steps = self.run(question, **kwargs)
|
81 |
-
|
82 |
-
|
83 |
-
return steps.strip()
|
84 |
-
print("DEBUG: steps is a", type(steps))
|
85 |
last_step = None
|
86 |
for step in steps:
|
87 |
last_step = step
|
88 |
-
|
|
|
|
|
89 |
answer = getattr(last_step, "answer", None)
|
90 |
-
print("DEBUG: answer =", repr(answer))
|
91 |
if answer is not None:
|
92 |
return str(answer).strip()
|
93 |
return str(last_step).strip()
|
|
|
30 |
SimpleOCRTool,
|
31 |
)
|
32 |
|
33 |
+
|
34 |
+
# ---------------------------------------------------------------------------
|
35 |
+
# Load the system prompt from system_prompt.txt (located in the same directory)
|
36 |
+
# ---------------------------------------------------------------------------
|
37 |
+
SYSTEM_PROMPT_PATH = os.path.join(os.path.dirname(__file__), "system_prompt.txt")
|
38 |
+
with open(SYSTEM_PROMPT_PATH, "r", encoding="utf-8") as f:
|
39 |
+
SYSTEM_PROMPT = f.read().strip()
|
40 |
+
|
41 |
+
|
42 |
# ---------------------------------------------------------------------------
|
43 |
# Model selection helper
|
44 |
# ---------------------------------------------------------------------------
|
|
|
54 |
from smolagents import InferenceClientModel
|
55 |
hf_model_id = os.getenv("HF_MODEL", "HuggingFaceH4/zephyr-7b-beta")
|
56 |
hf_token = os.getenv("HF_API_KEY")
|
57 |
+
return InferenceClientModel(
|
58 |
+
model_id=hf_model_id,
|
59 |
+
token=hf_token,
|
60 |
+
system_prompt=SYSTEM_PROMPT
|
61 |
+
)
|
62 |
|
63 |
if provider == "openai":
|
64 |
from smolagents import OpenAIServerModel
|
65 |
openai_model_id = os.getenv("OPENAI_MODEL", "gpt-3.5-turbo")
|
66 |
openai_token = os.getenv("OPENAI_API_KEY")
|
67 |
+
return OpenAIServerModel(
|
68 |
+
model_id=openai_model_id,
|
69 |
+
api_key=openai_token,
|
70 |
+
system_prompt=SYSTEM_PROMPT
|
71 |
+
)
|
72 |
|
73 |
raise ValueError(
|
74 |
f"Unsupported MODEL_PROVIDER: {provider!r}. "
|
|
|
95 |
# Convenience so the object itself can be *called* directly
|
96 |
def __call__(self, question: str, **kwargs: Any) -> str:
|
97 |
steps = self.run(question, **kwargs)
|
98 |
+
# If steps is a primitive, just return it
|
99 |
+
if isinstance(steps, (int, float, str)):
|
100 |
+
return str(steps).strip()
|
|
|
101 |
last_step = None
|
102 |
for step in steps:
|
103 |
last_step = step
|
104 |
+
# Defensive: handle int/float/str directly
|
105 |
+
if isinstance(last_step, (int, float, str)):
|
106 |
+
return str(last_step).strip()
|
107 |
answer = getattr(last_step, "answer", None)
|
|
|
108 |
if answer is not None:
|
109 |
return str(answer).strip()
|
110 |
return str(last_step).strip()
|
app.py
CHANGED
@@ -6,6 +6,9 @@ import pandas as pd
|
|
6 |
# --- Our Agent ---
|
7 |
from agent import gaia_agent
|
8 |
|
|
|
|
|
|
|
9 |
# (Keep Constants as is)
|
10 |
# --- Constants ---
|
11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
@@ -76,7 +79,11 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
76 |
try:
|
77 |
submitted_answer = agent(question_text)
|
78 |
# --- DEBUG LOGGING ---
|
79 |
-
|
|
|
|
|
|
|
|
|
80 |
# Force string type here just in case (defensive)
|
81 |
submitted_answer = str(submitted_answer).strip()
|
82 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
|
|
6 |
# --- Our Agent ---
|
7 |
from agent import gaia_agent
|
8 |
|
9 |
+
# Debugging level. If DEBUG=0 then DEBUG will be False. If DEBUG=1 then DEBUG will be True.
|
10 |
+
DEBUG = os.getenv("DEBUG", "0") == "1"
|
11 |
+
|
12 |
# (Keep Constants as is)
|
13 |
# --- Constants ---
|
14 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
79 |
try:
|
80 |
submitted_answer = agent(question_text)
|
81 |
# --- DEBUG LOGGING ---
|
82 |
+
if DEBUG:
|
83 |
+
print(f"[DEBUG] Task {task_id}: Answer type: {type(submitted_answer)}, Value: {repr(submitted_answer)}")
|
84 |
+
else:
|
85 |
+
print(f"[{task_id}] {question_text[:50]}... → {submitted_answer[:40]}")
|
86 |
+
|
87 |
# Force string type here just in case (defensive)
|
88 |
submitted_answer = str(submitted_answer).strip()
|
89 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
system_prompt.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
You are a helpful assistant tasked with answering questions using a set of tools.
|
2 |
+
Now, I will ask you a question. Report your thoughts, and finish your answer with the following template:
|
3 |
+
FINAL ANSWER: [YOUR FINAL ANSWER].
|
4 |
+
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
5 |
+
Your answer should only start with "FINAL ANSWER: ", then follows with the answer.
|