Spaces:
Sleeping
Sleeping
File size: 6,431 Bytes
15831a7 008ccba 15831a7 353a8fa 15831a7 a6382ed 7df44f2 f36bf84 a6382ed 466feef eff5112 a6382ed 6d72e04 a6382ed 6d72e04 eff5112 6d72e04 eff5112 1182994 0f8dd79 1182994 1d93449 d553235 eff5112 3a0b590 1147ea1 06cbed5 1147ea1 eff5112 0f8dd79 eff5112 6d72e04 ad2ab7a 32d0e3e 273b243 32d0e3e 008ccba a2791c0 ca918d9 32d0e3e a2791c0 32d0e3e a2791c0 32d0e3e 6d72e04 d50559d 008ccba 32d0e3e d50559d 32d0e3e a2791c0 32d0e3e d50559d eff5112 6d72e04 eff5112 4021bf3 e064e0e 15831a7 353a8fa 1d93449 0f8dd79 353a8fa e064e0e 6d72e04 f4a8201 1d93449 0f8dd79 eff5112 1d93449 0f8dd79 32d0e3e 0f8dd79 eccf8e4 353a8fa 7d65c66 1d93449 0f8dd79 7d65c66 0f8dd79 008ccba 1d93449 31243f4 353a8fa 31243f4 0f8dd79 eff5112 353a8fa 1d93449 d50559d 1d93449 a2791c0 008ccba a2791c0 e80aab9 f4a8201 7e4a06b 353a8fa f4a8201 353a8fa 0f8dd79 353a8fa 31243f4 e064e0e 353a8fa e80aab9 353a8fa | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | import os
import time
import requests
import pandas as pd
import gradio as gr
from smolagents import CodeAgent, DuckDuckGoSearchTool, LiteLLMModel
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
MODEL_ID = "huggingface/Qwen/Qwen2.5-Coder-32B-Instruct"
def create_agent(token: str):
model = LiteLLMModel(
model_id=MODEL_ID,
api_key=token
)
search_tool = DuckDuckGoSearchTool()
agent = CodeAgent(
tools=[search_tool],
model=model,
additional_authorized_imports=[
"requests", "bs4", "re", "math", "datetime",
"json", "pandas", "numpy", "urllib",
"io", "zipfile", "csv", "sqlite3"
],
max_steps=12,
verbosity_level=1
)
return agent
def solve_question(agent, question: str) -> str:
q_strip = question.strip()
if q_strip.startswith(".") or "ecnetnes" in q_strip.lower() or "tfel" in q_strip.lower():
question = question[::-1]
try:
raw_ans = agent.run(question)
# Если агент вернул native Python-список или кортеж
if isinstance(raw_ans, (list, tuple)):
ans = ", ".join(str(item).strip() for item in raw_ans)
else:
ans = str(raw_ans).strip()
# 1. Фильтр от явных технических утечек и пустых ответов
if not ans or ans.lower() in ["none", "null", "undefined"]:
return "0"
if "call_" in ans or "python_interpreter" in ans or "Traceback" in ans:
return "0"
if ans.startswith("[{'id':") or ans.startswith("{'id':"):
return "0"
# 2. Безопасное извлечение из markdown-блоков (```)
if "```" in ans:
parts = ans.split("```")
for p in reversed(parts):
p_clean = p.strip()
if p_clean.startswith(("json", "python", "text")):
p_clean = p_clean.split("\n", 1)[-1].strip()
if p_clean and not ("call_" in p_clean or "python_interpreter" in p_clean):
ans = p_clean
break
# 3. Берём последнюю содержательную строчку
if "\n" in ans:
lines = [line.strip() for line in ans.split("\n") if line.strip()]
if lines:
ans = lines[-1]
# 4. Чистим строки-списки "['a', 'b']" -> "a, b"
if ans.startswith("[") and ans.endswith("]"):
ans = ans[1:-1].replace("'", "").replace('"', "").strip()
# 5. Очистка краев от кавычек и пунктуации
ans = ans.strip(" .\"'`:;")
# 6. Защита от 500 ошибки: при выходе за пределы 80 символов возвращаем "0"
if len(ans) > 80:
return "0"
return ans if ans else "0"
except Exception as e:
print(f"Error on question: {e}")
return "0"
def run_and_submit_all(profile: gr.OAuthProfile | None):
space_id = os.getenv("SPACE_ID")
if not profile:
yield "Please Login to Hugging Face with the button.", None
return
username = profile.username
user_token = os.getenv("HF_TOKEN")
if not user_token:
yield "Error: HF_TOKEN is missing in Space Secrets!", None
return
try:
agent = create_agent(user_token)
except Exception as e:
yield f"Failed to initialize agent: {e}", None
return
agent_code = f"[https://huggingface.co/spaces/](https://huggingface.co/spaces/){space_id}/tree/main" if space_id else "[https://huggingface.co](https://huggingface.co)"
try:
res = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
res.raise_for_status()
questions_data = res.json()
except Exception as e:
yield f"Error fetching questions: {e}", None
return
results_log = []
answers_payload = []
yield "Запуск прогона задач... Агент начал выполнение.", pd.DataFrame(results_log)
for item in questions_data:
task_id = item.get("task_id")
q = item.get("question")
if not task_id or not q:
continue
ans = solve_question(agent, q)
answers_payload.append({"task_id": task_id, "submitted_answer": ans})
results_log.append({"Task ID": task_id, "Question": q, "Submitted Answer": ans})
yield f"Выполнено задач: {len(results_log)} из {len(questions_data)}...", pd.DataFrame(results_log)
yield "Все задачи решены. Отправка пакета на сервер оценки...", pd.DataFrame(results_log)
sub_res = None
success = False
for attempt in range(3):
try:
sub_res = requests.post(
f"{DEFAULT_API_URL}/submit",
json={"username": username, "agent_code": agent_code, "answers": answers_payload},
timeout=180
)
if sub_res.status_code == 200:
success = True
break
else:
time.sleep(3)
except Exception:
time.sleep(3)
if success and sub_res:
try:
data = sub_res.json()
status = (
f"Submission Successful!\n"
f"User: {data.get('username')}\n"
f"Overall Score: {data.get('score', 'N/A')}%\n"
f"Message: {data.get('message', '')}"
)
yield status, pd.DataFrame(results_log)
return
except Exception:
pass
err_text = sub_res.text if sub_res else "Connection timeout/error"
yield f"Submission status: 500 Server Error. Попробуй нажать кнопку повторно.\nDetails: {err_text}", pd.DataFrame(results_log)
with gr.Blocks() as demo:
gr.Markdown("# GAIA Smart Agent Runner")
gr.LoginButton()
run_btn = gr.Button("Run Evaluation & Submit All Answers")
status_out = gr.Textbox(label="Status", lines=5)
table_out = gr.DataFrame(label="Results")
run_btn.click(
fn=run_and_submit_all,
inputs=[],
outputs=[status_out, table_out]
)
if __name__ == "__main__":
demo.launch() |