Spaces:
No application file
No application file
Update app.py
Browse files
app.py
CHANGED
|
@@ -272,83 +272,105 @@ class AIAgent:
|
|
| 272 |
}
|
| 273 |
|
| 274 |
# --- Main Execution Example ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
if __name__ == "__main__":
|
| 276 |
-
|
| 277 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
else:
|
| 279 |
agent = AIAgent(openai_client=client)
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 338 |
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 348 |
else:
|
| 349 |
-
print(
|
|
|
|
|
|
|
|
|
|
| 350 |
|
| 351 |
-
|
| 352 |
-
print(f"Error: Input file '{input_gaia_file}' not found.")
|
| 353 |
-
except Exception as e:
|
| 354 |
-
print(f"An unexpected error occurred: {e}")
|
|
|
|
| 272 |
}
|
| 273 |
|
| 274 |
# --- Main Execution Example ---
|
| 275 |
+
# ... (all your previous code: GAIA_FINAL_ANSWER_SYSTEM_PROMPT, Pydantic models, Tool functions, AIAgent class,
|
| 276 |
+
# and the API interaction functions: api_get_questions, api_download_file, api_submit_answers
|
| 277 |
+
# should be defined above this block) ...
|
| 278 |
+
|
| 279 |
+
# --- Main Execution Block (Using API for Questions) ---
|
| 280 |
if __name__ == "__main__":
|
| 281 |
+
print("===== Application Startup =====")
|
| 282 |
+
|
| 283 |
+
# !!! ACTION REQUIRED: Set your username and agent_code identifier here !!!
|
| 284 |
+
YOUR_USERNAME = "nik" # Replace with your actual username for the course platform
|
| 285 |
+
YOUR_AGENT_CODE_IDENTIFIER = "NikAgent_HF_App_v1" # A name for your agent submission
|
| 286 |
+
|
| 287 |
+
if not OPENAI_API_KEY_VALUE:
|
| 288 |
+
print("CRITICAL: OpenAI API Key is not available. Main execution cannot proceed.")
|
| 289 |
+
elif not client:
|
| 290 |
+
print("CRITICAL: OpenAI client failed to initialize. Main execution cannot proceed.")
|
| 291 |
else:
|
| 292 |
agent = AIAgent(openai_client=client)
|
| 293 |
+
all_submission_answers = [] # To collect {"task_id": ..., "answer": ...} dicts
|
| 294 |
+
|
| 295 |
+
# 1. Fetch questions from the API
|
| 296 |
+
print("\nSTEP 1: Fetching questions from API...")
|
| 297 |
+
api_questions = api_get_questions() # This function calls the course API
|
| 298 |
+
|
| 299 |
+
if api_questions:
|
| 300 |
+
print(f"Fetched {len(api_questions)} questions from the API.")
|
| 301 |
+
# You might want to process a subset for testing during development:
|
| 302 |
+
# questions_to_process = api_questions[:1] # Example: process only the first question
|
| 303 |
+
questions_to_process = api_questions
|
| 304 |
+
|
| 305 |
+
|
| 306 |
+
for question_data_from_api in questions_to_process:
|
| 307 |
+
task_id = question_data_from_api.get("task_id")
|
| 308 |
+
# Ensure you are using the correct key for question text from the API response.
|
| 309 |
+
# Common keys: "question", "text", "query". Check API docs or a sample response.
|
| 310 |
+
question_text = question_data_from_api.get("question")
|
| 311 |
+
|
| 312 |
+
# Check the structure of question_data_from_api to see how file info is provided.
|
| 313 |
+
# It might be 'file_name', 'filename', 'associated_file', 'has_file', etc.
|
| 314 |
+
original_file_name_from_api = question_data_from_api.get("file_name") # Assuming 'file_name'
|
| 315 |
+
local_downloaded_file_path = None
|
| 316 |
+
|
| 317 |
+
if not task_id or not question_text:
|
| 318 |
+
print(f"Skipping question due to missing task_id or question text in API response: {question_data_from_api}")
|
| 319 |
+
continue
|
| 320 |
+
|
| 321 |
+
print(f"\n-----------------------------------\nProcessing Task ID: {task_id}")
|
| 322 |
+
|
| 323 |
+
# 2. Download file if it's indicated by the API response for the current question
|
| 324 |
+
if original_file_name_from_api:
|
| 325 |
+
print(f"STEP 2: File indicated by API ({original_file_name_from_api}). Attempting download...")
|
| 326 |
+
# Files will be downloaded into 'downloaded_gaia_files' relative to where app.py runs in the Space
|
| 327 |
+
local_downloaded_file_path = api_download_file(
|
| 328 |
+
task_id,
|
| 329 |
+
original_file_name_from_api,
|
| 330 |
+
download_dir="downloaded_gaia_files" # This will be created inside your HF Space app directory
|
| 331 |
+
)
|
| 332 |
+
else:
|
| 333 |
+
print(f"STEP 2: No file indicated by API for task {task_id}.")
|
| 334 |
+
|
| 335 |
+
# 3. Process the question with your AI Agent
|
| 336 |
+
print(f"\nSTEP 3: Processing task {task_id} with AI Agent...")
|
| 337 |
+
agent_result = agent.process_question(
|
| 338 |
+
task_id=task_id,
|
| 339 |
+
question_text=question_text,
|
| 340 |
+
file_path_for_agent=local_downloaded_file_path # Pass the path of the downloaded file
|
| 341 |
+
)
|
| 342 |
+
|
| 343 |
+
# agent_result["model_answer"] should be the clean answer, stripped of "FINAL ANSWER:"
|
| 344 |
+
clean_answer = agent_result.get("model_answer", "ERROR: No answer from agent")
|
| 345 |
+
|
| 346 |
+
print(f"Agent's Clean Answer for {task_id}: {clean_answer}")
|
| 347 |
+
|
| 348 |
+
all_submission_answers.append({
|
| 349 |
+
"task_id": task_id,
|
| 350 |
+
"answer": clean_answer
|
| 351 |
+
})
|
| 352 |
+
|
| 353 |
+
# Optional: Add a small delay if processing many questions to be kind to APIs
|
| 354 |
+
# import time
|
| 355 |
+
# time.sleep(0.5)
|
| 356 |
|
| 357 |
+
# 4. Submit all collected answers
|
| 358 |
+
if all_submission_answers:
|
| 359 |
+
print("\nSTEP 4: Submitting all processed answers...")
|
| 360 |
+
submission_result = api_submit_answers(
|
| 361 |
+
username=YOUR_USERNAME,
|
| 362 |
+
agent_code_identifier=YOUR_AGENT_CODE_IDENTIFIER,
|
| 363 |
+
processed_answers=all_submission_answers
|
| 364 |
+
)
|
| 365 |
+
if submission_result:
|
| 366 |
+
print("\n--- Submission Summary Received from API ---")
|
| 367 |
+
# Process/display submission_result as needed (e.g., score)
|
| 368 |
+
else:
|
| 369 |
+
print("Submission attempt failed or no result returned from submission API.")
|
| 370 |
else:
|
| 371 |
+
print("No answers were generated to submit.")
|
| 372 |
+
|
| 373 |
+
else:
|
| 374 |
+
print("Could not fetch questions from API. Exiting.")
|
| 375 |
|
| 376 |
+
print("\n===== Application Finished =====")
|
|
|
|
|
|
|
|
|