Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -89,36 +89,17 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 89 |
Fetches all questions from the GAIA API, runs the Groq agent on each,
|
| 90 |
and submits the answers under the authenticated user's profile.
|
| 91 |
"""
|
| 92 |
-
# Authentication Guard Clause
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
yield f"Authenticated as Hugging Face user: {username}"
|
| 98 |
-
|
| 99 |
-
if not OPENAI_API_KEY or not GROQ_API_KEY or not TAVILY_API_KEY:
|
| 100 |
-
yield "Error: API keys (OPENAI_API_KEY, GROQ_API_KEY, TAVILY_API_KEY) are not set. Please configure them as Space secrets."
|
| 101 |
-
return
|
| 102 |
-
|
| 103 |
-
# Fetch questions from the API
|
| 104 |
-
try:
|
| 105 |
-
yield "Fetching questions from the GAIA API..."
|
| 106 |
-
response = requests.get(f"{GAIA_API_URL}/questions", timeout=30)
|
| 107 |
-
response.raise_for_status()
|
| 108 |
-
questions = response.json()
|
| 109 |
-
yield f"Successfully fetched {len(questions)} questions."
|
| 110 |
-
except requests.exceptions.RequestException as e:
|
| 111 |
-
yield f"Error fetching questions from API: {e}"
|
| 112 |
return
|
| 113 |
|
| 114 |
-
|
| 115 |
|
| 116 |
-
#
|
| 117 |
-
|
| 118 |
-
file_tools = [read_file, write_file, list_files]
|
| 119 |
-
all_tools = base_tools + file_tools
|
| 120 |
-
|
| 121 |
-
agent_executor = initialize_agent(llm, all_tools)
|
| 122 |
|
| 123 |
# Process each task using the imported agent
|
| 124 |
for i, task in enumerate(questions):
|
|
@@ -129,15 +110,18 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 129 |
yield f"--- Starting Task {i+1}/{len(questions)} (ID: {task_id}) ---"
|
| 130 |
yield f"Question: {question}"
|
| 131 |
|
| 132 |
-
# Download associated file if it exists
|
| 133 |
if file_name:
|
| 134 |
try:
|
| 135 |
yield f"Downloading file: {file_name}..."
|
| 136 |
file_response = requests.get(f"{GAIA_API_URL}/files/{file_name}")
|
| 137 |
file_response.raise_for_status()
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
| 139 |
f.write(file_response.content)
|
| 140 |
-
yield f"File '{file_name}' downloaded
|
| 141 |
except requests.exceptions.RequestException as e:
|
| 142 |
yield f"Error downloading file {file_name}: {e}"
|
| 143 |
continue
|
|
@@ -170,9 +154,11 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 170 |
yield f"Error submitting answer for task {task_id}: {e}"
|
| 171 |
|
| 172 |
# Clean up the downloaded file
|
| 173 |
-
if file_name
|
| 174 |
-
os.
|
| 175 |
-
|
|
|
|
|
|
|
| 176 |
|
| 177 |
yield "\n--- All tasks completed! ---"
|
| 178 |
|
|
|
|
| 89 |
Fetches all questions from the GAIA API, runs the Groq agent on each,
|
| 90 |
and submits the answers under the authenticated user's profile.
|
| 91 |
"""
|
| 92 |
+
# ... (Authentication Guard Clause remains the same) ...
|
| 93 |
+
|
| 94 |
+
# Check for required API keys for the Groq agent
|
| 95 |
+
if not GROQ_API_KEY or not TAVILY_API_KEY:
|
| 96 |
+
yield "Error: Required API keys (GROQ_API_KEY, TAVILY_API_KEY) are not set. Please configure them as Space secrets."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
return
|
| 98 |
|
| 99 |
+
# ... (Fetching questions logic remains the same) ...
|
| 100 |
|
| 101 |
+
# The agent and its tools are now self-contained in agent.py
|
| 102 |
+
# All dead code related to a separate OpenAI agent has been removed.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
# Process each task using the imported agent
|
| 105 |
for i, task in enumerate(questions):
|
|
|
|
| 110 |
yield f"--- Starting Task {i+1}/{len(questions)} (ID: {task_id}) ---"
|
| 111 |
yield f"Question: {question}"
|
| 112 |
|
| 113 |
+
# Download associated file if it exists into the agent's workspace
|
| 114 |
if file_name:
|
| 115 |
try:
|
| 116 |
yield f"Downloading file: {file_name}..."
|
| 117 |
file_response = requests.get(f"{GAIA_API_URL}/files/{file_name}")
|
| 118 |
file_response.raise_for_status()
|
| 119 |
+
# Save the file in the workspace directory defined in agent.py
|
| 120 |
+
file_path = os.path.join("workspace", file_name)
|
| 121 |
+
os.makedirs("workspace", exist_ok=True)
|
| 122 |
+
with open(file_path, "wb") as f:
|
| 123 |
f.write(file_response.content)
|
| 124 |
+
yield f"File '{file_name}' downloaded to workspace. The agent can now access it."
|
| 125 |
except requests.exceptions.RequestException as e:
|
| 126 |
yield f"Error downloading file {file_name}: {e}"
|
| 127 |
continue
|
|
|
|
| 154 |
yield f"Error submitting answer for task {task_id}: {e}"
|
| 155 |
|
| 156 |
# Clean up the downloaded file
|
| 157 |
+
if file_name:
|
| 158 |
+
cleanup_path = os.path.join("workspace", file_name)
|
| 159 |
+
if os.path.exists(cleanup_path):
|
| 160 |
+
os.remove(cleanup_path)
|
| 161 |
+
yield f"Cleaned up file: {file_name}"
|
| 162 |
|
| 163 |
yield "\n--- All tasks completed! ---"
|
| 164 |
|