Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,6 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
from smolagents import CodeAgent, InferenceClientModel, DuckDuckGoSearchTool
|
| 14 |
import os
|
| 15 |
-
import re
|
| 16 |
|
| 17 |
class BasicAgent:
|
| 18 |
def __init__(self):
|
|
@@ -22,7 +21,7 @@ class BasicAgent:
|
|
| 22 |
print("TOKEN EXISTS:", hf_token is not None)
|
| 23 |
|
| 24 |
self.model = InferenceClientModel(
|
| 25 |
-
model_id="Qwen/Qwen2.5-
|
| 26 |
provider="hf-inference",
|
| 27 |
token=hf_token
|
| 28 |
)
|
|
@@ -31,39 +30,21 @@ class BasicAgent:
|
|
| 31 |
tools=[DuckDuckGoSearchTool()],
|
| 32 |
model=self.model,
|
| 33 |
add_base_tools=True,
|
| 34 |
-
max_steps=
|
| 35 |
)
|
| 36 |
|
| 37 |
-
def
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
answer = answer.replace("final answer:", "")
|
| 43 |
-
answer = answer.strip()
|
| 44 |
-
|
| 45 |
-
# remove quotes if answer is wrapped in quotes
|
| 46 |
-
if len(answer) >= 2 and answer[0] in ['"', "'"] and answer[-1] in ['"', "'"]:
|
| 47 |
-
answer = answer[1:-1].strip()
|
| 48 |
-
|
| 49 |
-
# keep only first line if model gives extra text
|
| 50 |
-
answer = answer.split("\n")[0].strip()
|
| 51 |
-
|
| 52 |
-
return answer
|
| 53 |
|
| 54 |
def __call__(self, question: str) -> str:
|
| 55 |
-
print(
|
| 56 |
|
| 57 |
prompt = f"""
|
| 58 |
-
You are solving an exact-match benchmark question.
|
| 59 |
-
|
| 60 |
Return ONLY the final answer.
|
| 61 |
-
|
| 62 |
-
Do not include "FINAL ANSWER".
|
| 63 |
-
Do not include full sentences unless the answer itself is a sentence.
|
| 64 |
-
For numbers, return only the number.
|
| 65 |
-
For names, return only the name.
|
| 66 |
-
For comma-separated lists, use the exact requested order.
|
| 67 |
|
| 68 |
Question:
|
| 69 |
{question}
|
|
@@ -71,14 +52,26 @@ Question:
|
|
| 71 |
|
| 72 |
try:
|
| 73 |
answer = self.agent.run(prompt)
|
| 74 |
-
|
| 75 |
-
print("FINAL SUBMITTED:", final_answer)
|
| 76 |
-
return final_answer
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
return "error"
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 84 |
"""
|
|
|
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
from smolagents import CodeAgent, InferenceClientModel, DuckDuckGoSearchTool
|
| 14 |
import os
|
|
|
|
| 15 |
|
| 16 |
class BasicAgent:
|
| 17 |
def __init__(self):
|
|
|
|
| 21 |
print("TOKEN EXISTS:", hf_token is not None)
|
| 22 |
|
| 23 |
self.model = InferenceClientModel(
|
| 24 |
+
model_id="Qwen/Qwen2.5-7B-Instruct", # lighter + stable
|
| 25 |
provider="hf-inference",
|
| 26 |
token=hf_token
|
| 27 |
)
|
|
|
|
| 30 |
tools=[DuckDuckGoSearchTool()],
|
| 31 |
model=self.model,
|
| 32 |
add_base_tools=True,
|
| 33 |
+
max_steps=6
|
| 34 |
)
|
| 35 |
|
| 36 |
+
def clean(self, ans):
|
| 37 |
+
ans = str(ans).strip()
|
| 38 |
+
ans = ans.replace("FINAL ANSWER:", "").strip()
|
| 39 |
+
ans = ans.split("\n")[0].strip()
|
| 40 |
+
return ans
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
def __call__(self, question: str) -> str:
|
| 43 |
+
print("Q:", question[:80])
|
| 44 |
|
| 45 |
prompt = f"""
|
|
|
|
|
|
|
| 46 |
Return ONLY the final answer.
|
| 47 |
+
No explanation. No extra words.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
Question:
|
| 50 |
{question}
|
|
|
|
| 52 |
|
| 53 |
try:
|
| 54 |
answer = self.agent.run(prompt)
|
| 55 |
+
final = self.clean(answer)
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
if final == "" or final.lower() == "none":
|
| 58 |
+
return "unknown"
|
|
|
|
| 59 |
|
| 60 |
+
print("FINAL:", final)
|
| 61 |
+
return final
|
| 62 |
+
|
| 63 |
+
except Exception as e:
|
| 64 |
+
print("ERROR:", e)
|
| 65 |
+
|
| 66 |
+
# 🔥 fallback (VERY IMPORTANT)
|
| 67 |
+
try:
|
| 68 |
+
simple = self.model.generate(
|
| 69 |
+
prompt + "\nAnswer:",
|
| 70 |
+
max_new_tokens=50
|
| 71 |
+
)
|
| 72 |
+
return self.clean(simple)
|
| 73 |
+
except:
|
| 74 |
+
return "unknown"
|
| 75 |
|
| 76 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 77 |
"""
|