Update app.py
Browse files
app.py
CHANGED
|
@@ -4,21 +4,16 @@ import requests
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
-
# --- Basic Agent Definition ---
|
| 12 |
-
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
-
class BasicAgent:
|
| 14 |
-
def __init__(self):
|
| 15 |
-
print("BasicAgent initialized.")
|
| 16 |
-
def __call__(self, question: str) -> str:
|
| 17 |
-
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 18 |
-
fixed_answer = "This is a default answer."
|
| 19 |
-
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 20 |
-
return fixed_answer
|
| 21 |
-
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
| 24 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
@@ -40,7 +35,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 40 |
|
| 41 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 42 |
try:
|
| 43 |
-
agent =
|
| 44 |
except Exception as e:
|
| 45 |
print(f"Error instantiating agent: {e}")
|
| 46 |
return f"Error initializing agent: {e}", None
|
|
@@ -76,11 +71,40 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 76 |
for item in questions_data:
|
| 77 |
task_id = item.get("task_id")
|
| 78 |
question_text = item.get("question")
|
|
|
|
| 79 |
if not task_id or question_text is None:
|
| 80 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 81 |
continue
|
| 82 |
try:
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 85 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 86 |
except Exception as e:
|
|
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
|
| 7 |
+
from config import set_environment
|
| 8 |
+
set_environment()
|
| 9 |
+
|
| 10 |
+
from tools import upload_file_to_bucket
|
| 11 |
+
from graph import generate_graph
|
| 12 |
+
|
| 13 |
# (Keep Constants as is)
|
| 14 |
# --- Constants ---
|
| 15 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 18 |
"""
|
| 19 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
|
|
| 35 |
|
| 36 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 37 |
try:
|
| 38 |
+
agent = generate_graph()
|
| 39 |
except Exception as e:
|
| 40 |
print(f"Error instantiating agent: {e}")
|
| 41 |
return f"Error initializing agent: {e}", None
|
|
|
|
| 71 |
for item in questions_data:
|
| 72 |
task_id = item.get("task_id")
|
| 73 |
question_text = item.get("question")
|
| 74 |
+
filename = item.get('file_name')
|
| 75 |
if not task_id or question_text is None:
|
| 76 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 77 |
continue
|
| 78 |
try:
|
| 79 |
+
path_filename = None
|
| 80 |
+
gcp_uri_file = None
|
| 81 |
+
if filename != '':
|
| 82 |
+
url = f"{api_url}/files/{task_id}"
|
| 83 |
+
|
| 84 |
+
with requests.get(url, stream=True, timeout=15) as r:
|
| 85 |
+
r.raise_for_status()
|
| 86 |
+
# ── Obtener nombre de archivo ──────────────────────────────
|
| 87 |
+
cd = r.headers.get("content-disposition", "")
|
| 88 |
+
m = re.search(r'filename="?([^";]+)', cd) # RFC 6266
|
| 89 |
+
filename = m.group(1) if m else f"{task_id}.bin"
|
| 90 |
+
# ── Guardar en disco en chunks ─────────────────────────────
|
| 91 |
+
path_filename = "data/" + filename
|
| 92 |
+
with open(path_filename, "wb") as f:
|
| 93 |
+
for chunk in r.iter_content(chunk_size=8192):
|
| 94 |
+
if chunk: # evita keep-alives vacíos
|
| 95 |
+
f.write(chunk)
|
| 96 |
+
|
| 97 |
+
gcp_uri_file = upload_file_to_bucket(path_filename)
|
| 98 |
+
|
| 99 |
+
#submitted_answer = agent(question_text)
|
| 100 |
+
result = agent.invoke({"messages": [("user",
|
| 101 |
+
task)],
|
| 102 |
+
"check_final_answer": False,
|
| 103 |
+
"path_filename": path_filename,
|
| 104 |
+
"gcp_path": gcp_uri_file},
|
| 105 |
+
{"recursion_limit": 20})
|
| 106 |
+
|
| 107 |
+
submitted_answer = result['final_answer']
|
| 108 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 109 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 110 |
except Exception as e:
|