jbaselga commited on
Commit
401aee4
verified
1 Parent(s): 958a29d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents import CodeAgent, WebSearchTool, PythonInterpreterTool, InferenceClientModel
2
+ import requests
3
+ import os
4
+
5
+ # --- 1. API GAIA ---
6
+ HF_TOKEN = os.getenv("token_curso")
7
+
8
+
9
+ BASE = "https://jbaselga-agentes-unit4.hf.space/api"
10
+ QUESTIONS_URL = f"{BASE}/questions"
11
+ SUBMIT_URL = f"{BASE}/submit"
12
+
13
+ HF_USERNAME = "jbaselga"
14
+ AGENT_CODE_URL = "https://huggingface.co/spaces/jbaselga/agentes-unit4/tree/main"
15
+
16
+ def fetch_gaia_questions():
17
+ resp = requests.get(QUESTIONS_URL, headers={"Authorization": f"Bearer {HF_TOKEN}"})
18
+ return resp.json()
19
+
20
+ def submit_answers(answers: dict):
21
+ payload = {
22
+ "username": HF_USERNAME,
23
+ "agent_code": AGENT_CODE_URL,
24
+ "answers": [
25
+ {"task_id": tid, "submitted_answer": ans}
26
+ for tid, ans in answers.items()
27
+ ]
28
+ }
29
+ resp = requests.post(SUBMIT_URL, json=payload, headers={"Authorization": f"Bearer {HF_TOKEN}"})
30
+ return resp.json()
31
+
32
+ # --- 2. Configuraci贸n del agente ---
33
+ model = InferenceClientModel()
34
+ tools = [
35
+ WebSearchTool(), # B煤squeda web (usa DuckDuckGo)
36
+ PythonInterpreterTool() # Ejecuta c贸digo Python (incluye c谩lculos)
37
+ ]
38
+ agent = CodeAgent(tools=tools, model=model, add_base_tools=True, max_steps=10)
39
+
40
+ # --- 3. Funci贸n para responder preguntas ---
41
+ def answer_question(qid: str, question: str) -> str:
42
+ prompt = f"Pregunta ID:{qid}. Responde SOLO la respuesta, sin explicaciones:\n{question}"
43
+ out = agent.run(prompt)
44
+ return out.strip()
45
+
46
+ # --- 4. Flujo principal ---
47
+ def main():
48
+ qs = fetch_gaia_questions()
49
+ print("DEBUG qs:", qs) # <-- A帽ade esto para ver la estructura
50
+ answers = {}
51
+ for item in qs:
52
+ print("DEBUG item:", item)
53
+ if isinstance(item, dict) and "task_id" in item and "question" in item:
54
+ answers[item["task_id"]] = answer_question(item["task_id"], item["question"])
55
+ else:
56
+ print("Formato inesperado:", item)
57
+ result = submit_answers(answers)
58
+ print("馃И Resultados GAIA:", result)
59
+
60
+ if __name__ == "__main__":
61
+ main()