Update app.py
Browse files
app.py
CHANGED
|
@@ -12,49 +12,63 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
# --- YENİ ALFRED AJANI ---
|
| 14 |
token = os.getenv("HF_TOKEN")
|
| 15 |
-
model = HfApiModel(model_id="
|
| 16 |
|
| 17 |
class AlfredAgent:
|
| 18 |
def __init__(self):
|
| 19 |
-
CUSTOM_SYSTEM_PROMPT = """You are a
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
{{managed_agents_descriptions}}
|
| 27 |
-
|
| 28 |
-
You can use Python for calculations:
|
| 29 |
{{authorized_imports}}"""
|
| 30 |
|
| 31 |
self.agent = CodeAgent(
|
| 32 |
-
|
| 33 |
-
tools=[VisitWebpageTool()],
|
| 34 |
model=model,
|
| 35 |
-
max_steps=
|
| 36 |
-
add_base_tools=
|
| 37 |
planning_interval=2,
|
| 38 |
system_prompt=CUSTOM_SYSTEM_PROMPT
|
| 39 |
)
|
| 40 |
-
print("AlfredAgent (Llama-Powered) kuruldu.")
|
| 41 |
|
| 42 |
def __call__(self, question: str) -> str:
|
| 43 |
-
prompt = f"""Task: {question}
|
| 44 |
-
|
| 45 |
-
RULES:
|
| 46 |
-
- Provide ONLY the final value.
|
| 47 |
-
- Date: YYYY-MM-DD
|
| 48 |
-
- No units, no extra words."""
|
| 49 |
-
|
| 50 |
try:
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
return "Unknown"
|
| 59 |
|
| 60 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
@@ -107,37 +121,32 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 107 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 108 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 109 |
|
| 110 |
-
# 3. Run your Agent
|
| 111 |
results_log = []
|
| 112 |
answers_payload = []
|
| 113 |
-
print(f"Running agent on {len(questions_data)} questions...")
|
| 114 |
|
| 115 |
for item in questions_data:
|
| 116 |
task_id = item.get("task_id")
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
if not task_id or question_text is None: continue
|
| 120 |
|
| 121 |
-
print(f"\n
|
| 122 |
-
|
| 123 |
try:
|
| 124 |
-
|
| 125 |
-
raw_answer = agent(question_text)
|
| 126 |
|
| 127 |
-
# Sınav
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
# Eğer cevap hala çok uzunsa sadece ilk kelimeyi al (genelde isim veya sayıdır)
|
| 131 |
-
if len(final_fix.split()) > 5:
|
| 132 |
-
final_fix = final_fix.split()[-1]
|
| 133 |
-
|
| 134 |
-
answers_payload.append({"task_id": task_id, "submitted_answer": final_fix})
|
| 135 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": final_fix})
|
| 136 |
-
print(f"BULDUM: {final_fix}")
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
# 4. Prepare Submission
|
| 143 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
|
|
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
# --- YENİ ALFRED AJANI ---
|
| 14 |
token = os.getenv("HF_TOKEN")
|
| 15 |
+
model = HfApiModel(model_id="Qwen/Qwen2.5-72B-Instruct", token=token)
|
| 16 |
|
| 17 |
class AlfredAgent:
|
| 18 |
def __init__(self):
|
| 19 |
+
CUSTOM_SYSTEM_PROMPT = """You are a helpful and agile assistant that has access to a set of tools.
|
| 20 |
+
For every step, you must provide your reasoning in a 'Thoughts:' section and then the tool call in a 'Code:' section.
|
| 21 |
+
|
| 22 |
+
Format:
|
| 23 |
+
Thoughts: I need to search for...
|
| 24 |
+
Code:
|
| 25 |
+
```py
|
| 26 |
+
print(web_search("question"))
|
| 27 |
+
```<end_action>
|
| 28 |
+
|
| 29 |
+
Rules:
|
| 30 |
+
1. Provide ONLY the final answer value. No sentences.
|
| 31 |
+
2. For dates, use YYYY-MM-DD.
|
| 32 |
+
3. If you find the info, stop and give the answer immediately.
|
| 33 |
+
|
| 34 |
+
Tools:
|
| 35 |
{{managed_agents_descriptions}}
|
|
|
|
|
|
|
| 36 |
{{authorized_imports}}"""
|
| 37 |
|
| 38 |
self.agent = CodeAgent(
|
| 39 |
+
tools=[VisitWebpageTool(), DuckDuckGoSearchTool()],
|
|
|
|
| 40 |
model=model,
|
| 41 |
+
max_steps=10,
|
| 42 |
+
add_base_tools=False,
|
| 43 |
planning_interval=2,
|
| 44 |
system_prompt=CUSTOM_SYSTEM_PROMPT
|
| 45 |
)
|
|
|
|
| 46 |
|
| 47 |
def __call__(self, question: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
try:
|
| 49 |
+
# Soruya net kural ekliyoruz
|
| 50 |
+
full_prompt = f"Solve this GAIA task precisely: {question}. Return ONLY the value."
|
| 51 |
+
result = self.agent.run(full_prompt)
|
| 52 |
+
|
| 53 |
+
# İlk temizlik
|
| 54 |
+
ans = str(result).strip()
|
| 55 |
+
|
| 56 |
+
# Eğer model "Final Answer: 1928" dediyse sadece 1928'i al
|
| 57 |
+
if "Final Answer:" in ans:
|
| 58 |
+
ans = ans.split("Final Answer:")[-1].strip()
|
| 59 |
+
|
| 60 |
+
# Kelime bazlı temizlik (gereksiz ekleri atar)
|
| 61 |
+
for word in ["is:", "answer:", "result:", "the answer is"]:
|
| 62 |
+
if word in ans.lower():
|
| 63 |
+
ans = ans.lower().split(word)[-1].strip()
|
| 64 |
+
|
| 65 |
+
# Hala çok uzunsa (muhtemelen paragraf yazdı), sadece en son satırı/kelimeyi al
|
| 66 |
+
if len(ans) > 60:
|
| 67 |
+
ans = ans.split('\n')[-1].strip(" .\"'")
|
| 68 |
+
|
| 69 |
+
return ans
|
| 70 |
+
except Exception:
|
| 71 |
+
# Hata anında bile "None" yerine "Unknown" dönerek formatı koruyoruz
|
| 72 |
return "Unknown"
|
| 73 |
|
| 74 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
|
|
| 121 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 122 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 123 |
|
| 124 |
+
# 3. Run your Agent (Bu kısmı bul ve değiştir)
|
| 125 |
results_log = []
|
| 126 |
answers_payload = []
|
|
|
|
| 127 |
|
| 128 |
for item in questions_data:
|
| 129 |
task_id = item.get("task_id")
|
| 130 |
+
q_text = item.get("question")
|
| 131 |
+
if not task_id: continue
|
|
|
|
| 132 |
|
| 133 |
+
print(f"\n--- Görev: {task_id} ---")
|
|
|
|
| 134 |
try:
|
| 135 |
+
answer = agent(q_text)
|
|
|
|
| 136 |
|
| 137 |
+
# Sınav formatına zorla (lowercase ve temizlik)
|
| 138 |
+
final_ans = str(answer).replace('"', '').replace("'", "").strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
+
# Eğer model çok uzun bir şey döndürdüyse, GAIA bunu kabul etmez.
|
| 141 |
+
# İlk 2-3 kelimeyi veya sayıyı almaya çalışalım.
|
| 142 |
+
if len(final_ans) > 50:
|
| 143 |
+
final_ans = final_ans[:47] + "..."
|
| 144 |
+
|
| 145 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": final_ans})
|
| 146 |
+
results_log.append({"Task ID": task_id, "Question": q_text, "Submitted Answer": final_ans})
|
| 147 |
+
print(f"Cevap Kaydedildi: {final_ans}")
|
| 148 |
+
except:
|
| 149 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": "Unknown"})
|
| 150 |
|
| 151 |
# 4. Prepare Submission
|
| 152 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|