Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files2135 unmodified lines213621372138213921402141214221432144214521462143214721482149215045 unmodified lines2196219721982195219922002201220221982199220022032204220522062207220822092210221122122213221423 unmodified lines22382239224022302241224222432244224522462236224722482249225045 unmodified lines22962297229822992300230123022303230423052306230723084 unmodified lines231323142315231623172318231923202321232223232324232523262327232823292330233123328 unmodified lines234123422343234423452346234719 unmodified lines2367236823692341237023712372237349 unmodified lines242324242425242624272428242923982399240024012402240324042405240624072408240924102411241224132414241524302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247481 unmodified lines25562557255825592560250425052561256225632564256525662567256825692570257125722573257425752576257725783 unmodified lines25822583258425152585258625874 unmodified lines259225932594252625952596259725982531259926002601260225362603260426052606254126072608260958 unmodified lines266826692670267126722135 unmodified lines
if not hasattr(self, "_local_state"): self._local_state = threading.local() if not hasattr(self, "_dead_models"): self._dead_models = set() if not hasattr(self, "_model_cooldowns"): self._model_cooldowns = {}
if hasattr(self, "_reset_key_indices"): self._reset_key_indices()
deadline = time.monotonic() + 140.0 deadline = time.monotonic() + 75.0 self._local_state.deadline = deadline self._task_deadline = deadline
45 unmodified lines else: prompt += f"\n\nTask ID: {task_id}."
blocked_models = set() blocked_models = set(self._dead_models) blocked_providers = set()
now = time.monotonic() models_to_try = list(getattr(self, "working_models", []) or [self.model_id]) if self.model_id and self.model_id not in models_to_try: models_to_try.append(self.model_id) all_candidates = list(getattr(self, "working_models", []) or [self.model_id]) if self.model_id and self.model_id not in all_candidates: all_candidates.append(self.model_id) models_to_try = [ m for m in all_candidates if m not in self._dead_models and self._model_cooldowns.get(m, 0.0) <= now ] if not models_to_try: models_to_try = [m for m in all_candidates if m not in self._dead_models] or all_candidates
backoff_rounds = 0 while time.monotonic() < deadline:23 unmodified lines rem_now = deadline - time.monotonic() if rem_now <= 6.0: break call_timeout = max(12.0, min(35.0, rem_now - 3.0)) call_timeout = max(10.0, min(25.0, rem_now - 3.0)) built_model = self._build_model(model_name) built_model.model_id = getattr(built_model, "model_id", model_name) built_model.timeout = call_timeout built_model.deadline = deadline run_agent = self._create_code_agent(built_model) run_agent.max_steps = 8 if rem_now > 35.0 else 5 run_agent.max_steps = 7 if rem_now > 30.0 else 4 result = run_agent.run(prompt) raw_result = "" if result is None else str(result).strip() if raw_result.lower().startswith("error in generating"):45 unmodified lines ): if hasattr(self, "_rotate_api_key") and self._rotate_api_key(provider): continue cooldown_until = time.monotonic() + 30.0 for m in models_to_try: if m.startswith(f"{provider}/"): rate_limited_models.add(m) self._model_cooldowns[m] = cooldown_until rate_limited_models.add(model_name) self._model_cooldowns[model_name] = cooldown_until rate_limited_providers.add(provider) break if any(4 unmodified lines ) ): blocked_models.add(model_name) self._model_cooldowns[model_name] = time.monotonic() + 180.0 break if any( marker in err_lower for marker in ( "404", "not found", "model_not_found", "decommissioned", "unsupported", ) ): blocked_models.add(model_name) self._dead_models.add(model_name) break if any( marker in err_lower8 unmodified lines time.sleep(0.5) continue blocked_models.add(model_name) self._model_cooldowns[model_name] = time.monotonic() + 60.0 break blocked_models.add(model_name) break19 unmodified lines self._provider_key_idx[prov] = 0 if hasattr(self, "_sync_provider_env"): self._sync_provider_env(prov, pool[0] if pool else "") time.sleep(min(10.0, max(2.0, rem_after_pass - 12.0))) time.sleep(min(4.0, max(1.5, rem_after_pass - 12.0))) continue break finally:49 unmodified lines print(agent_code)
# 2. Fetch Questions import json import time
print(f"Fetching questions from: {questions_url}") try: response = requests.get(questions_url, timeout=45) response.raise_for_status() questions_data = response.json() if not questions_data: print("Fetched questions list is empty.") return "Fetched questions list is empty or invalid format.", None print(f"Fetched {len(questions_data)} questions.") except requests.exceptions.RequestException as e: print(f"Error fetching questions: {e}") return f"Error fetching questions: {e}", None except requests.exceptions.JSONDecodeError as e: print(f"Error decoding JSON response from questions endpoint: {e}") print(f"Response text: {response.text[:500]}") return f"Error decoding server response for questions: {e}", None except Exception as e: print(f"An unexpected error occurred fetching questions: {e}") return f"An unexpected error occurred fetching questions: {e}", None questions_cache_path = "/tmp/gaia_questions.json" questions_data = None last_fetch_exc = None for attempt in range(3): try: response = requests.get(questions_url, timeout=(15, 90)) if response.status_code in {429, 502, 503, 504} and attempt < 2: time.sleep(3 * (attempt + 1)) continue response.raise_for_status() questions_data = response.json() if questions_data: try: with open(questions_cache_path, "w", encoding="utf-8") as f_q: json.dump(questions_data, f_q, ensure_ascii=False) except Exception: pass break except requests.exceptions.RequestException as e: last_fetch_exc = e if attempt < 2: time.sleep(3 * (attempt + 1)) continue except Exception as e: last_fetch_exc = e break
if questions_data is None and os.path.isfile(questions_cache_path): try: with open(questions_cache_path, "r", encoding="utf-8") as f_q: questions_data = json.load(f_q) print("Loaded questions from local cache after network error.") except Exception: questions_data = None
if questions_data is None: print(f"Error fetching questions: {last_fetch_exc}") return f"Error fetching questions: {last_fetch_exc}", None if not questions_data: print("Fetched questions list is empty.") return "Fetched questions list is empty or invalid format.", None print(f"Fetched {len(questions_data)} questions.")
# 3. Run your Agent from concurrent.futures import ThreadPoolExecutor, TimeoutError as FutureTimeoutError81 unmodified lines
# 5. Submit print(f"Submitting {len(answers_payload)} answers to: {submit_url}") results_df = pd.DataFrame(results_log) try: response = requests.post(submit_url, json=submission_data, timeout=45) response.raise_for_status() response = None for attempt in range(3): try: response = requests.post(submit_url, json=submission_data, timeout=(15, 90)) if response.status_code in {429, 502, 503, 504} and attempt < 2: time.sleep(3 * (attempt + 1)) continue response.raise_for_status() break except (requests.exceptions.Timeout, requests.exceptions.ConnectionError): if attempt < 2: time.sleep(3 * (attempt + 1)) continue raise
result_data = response.json() final_status = ( f"Submission Successful!\n"3 unmodified lines f"Message: {result_data.get('message', 'No message received.')}" ) print("Submission successful.") results_df = pd.DataFrame(results_log) return final_status, results_df except requests.exceptions.HTTPError as e: error_detail = f"Server responded with status {e.response.status_code}."4 unmodified lines error_detail += f" Response: {e.response.text[:500]}" status_message = f"Submission Failed: {error_detail}" print(status_message) results_df = pd.DataFrame(results_log) return status_message, results_df except requests.exceptions.Timeout: status_message = "Submission Failed: The request timed out." print(status_message) results_df = pd.DataFrame(results_log) return status_message, results_df except requests.exceptions.RequestException as e: status_message = f"Submission Failed: Network error - {e}" print(status_message) results_df = pd.DataFrame(results_log) return status_message, results_df except Exception as e: status_message = f"An unexpected error occurred during submission: {e}" print(status_message) results_df = pd.DataFrame(results_log) return status_message, results_df
58 unmodified lines print("-" * (60 + len(" App Starting ")) + "\n")
print("Launching Gradio Interface for Basic Agent Evaluation...") demo.queue() demo.launch(debug=True, share=False)app.py+99-33