Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files268 unmodified lines26927027127227327427530 unmodified lines3063073083093103113121205 unmodified lines151815191520152115221523152486 unmodified lines161116121613161416151616161716181619162016211622162349 unmodified lines167316741675167616771678167960 unmodified lines17401741174217431744174526 unmodified lines177217731774177517761777177868 unmodified lines184718481849185018511852268 unmodified linesdef _read_single(path: str, max_chars: int) -> str:f_ext = os.path.splitext(path)[1].lower()if f_ext in {".xlsx", ".xls"}:sheets = pd.read_excel(path, sheet_name=None)if not sheets:return "Empty Excel workbook."30 unmodified linesif not os.path.isfile(file_path):return f"File not found: '{file_path}'"ext = os.path.splitext(file_path)[1].lower()if ext == ".zip" or zipfile.is_zipfile(file_path):extract_dir = os.path.join(os.path.dirname(file_path),f"extracted_{os.path.basename(file_path)}",1205 unmodified linesos.environ["OPENROUTER_API_KEY"] = keyelif model_name.startswith("groq/"):os.environ["GROQ_API_KEY"] = keykwargs.setdefault("timeout", 45)if model_name.startswith("openrouter/"):kwargs["max_tokens"] = 4096return kwargs86 unmodified linesexcept Exception:return (model_name, True)from concurrent.futures import ThreadPoolExecutorprobed_slice = self.working_models[:5]with ThreadPoolExecutor(max_workers=5) as probe_pool:probe_results = dict(probe_pool.map(lambda m: _probe_candidate(m), probed_slice, timeout=6))filtered_models = [m for m in self.working_models if probe_results.get(m, True)]if filtered_models:self.working_models = filtered_models49 unmodified lines"max_steps": 10,"verbosity_level": 0,}filtered_agent_kwargs = {k: v for k, v in agent_kwargs.items() if k in sig.parameters}self.agent = CodeAgent(**filtered_agent_kwargs)if not hasattr(self, "_build_model") or not hasattr(self, "agent"):60 unmodified linesself.agent.max_steps = min(int(prev_max_steps), 4)try:while time.monotonic() < deadline:rate_limited_models = set()rate_limited_providers = set()26 unmodified linesbuilt_model.timeout = call_timeoutself.agent.model = built_modelresult = self.agent.run(prompt)cleaned = self._clean_exact_match_answer("" if result is None else str(result).strip())if cleaned and not cleaned.startswith("AGENT ERROR:"):return cleanedblocked_models.add(model_name)68 unmodified linesif self._rotate_api_key(prov):rotated_any = Trueif not rotated_any:breakfinally:if prev_max_steps is not None:268 unmodified lines26927027127227327427530 unmodified lines3063073083093103113123131205 unmodified lines151915201521152215231524152586 unmodified lines161216131614161516161617161816191620162116221623162416251626162716281629163016311632163349 unmodified lines168316841685168616871688168916901691169260 unmodified lines175317541755175617571758175926 unmodified lines178617871788178917901791179217931794179568 unmodified lines1864186518661867186818691870187118721873268 unmodified linesdef _read_single(path: str, max_chars: int) -> str:f_ext = os.path.splitext(path)[1].lower()if f_ext in {".xlsx", ".xlsm", ".xls"}:sheets = pd.read_excel(path, sheet_name=None)if not sheets:return "Empty Excel workbook."30 unmodified linesif not os.path.isfile(file_path):return f"File not found: '{file_path}'"ext = os.path.splitext(file_path)[1].lower()excel_exts = {".xlsx", ".xlsm", ".xls"}if ext == ".zip" or (ext not in excel_exts and zipfile.is_zipfile(file_path)):extract_dir = os.path.join(os.path.dirname(file_path),f"extracted_{os.path.basename(file_path)}",1205 unmodified linesos.environ["OPENROUTER_API_KEY"] = keyelif model_name.startswith("groq/"):os.environ["GROQ_API_KEY"] = keykwargs.setdefault("timeout", getattr(self, "timeout", None) or 45)if model_name.startswith("openrouter/"):kwargs["max_tokens"] = 4096return kwargs86 unmodified linesexcept Exception:return (model_name, True)from concurrent.futures import ThreadPoolExecutor, wait as futures_waitprobed_slice = self.working_models[:5]probe_results = {}probe_pool = ThreadPoolExecutor(max_workers=5)try:probe_futures = [probe_pool.submit(_probe_candidate, m) for m in probed_slice]done, _ = futures_wait(probe_futures, timeout=12)for fut in done:try:probed_name, probed_ok = fut.result()probe_results[probed_name] = probed_okexcept Exception:passfinally:probe_pool.shutdown(wait=False, cancel_futures=True)filtered_models = [m for m in self.working_models if probe_results.get(m, True)]if filtered_models:self.working_models = filtered_models49 unmodified lines"max_steps": 10,"verbosity_level": 0,}from smolagents import MultiStepAgentaccepted_params = set(sig.parameters) | set(inspect.signature(MultiStepAgent.__init__).parameters)filtered_agent_kwargs = {k: v for k, v in agent_kwargs.items() if k in accepted_params}self.agent = CodeAgent(**filtered_agent_kwargs)if not hasattr(self, "_build_model") or not hasattr(self, "agent"):60 unmodified linesself.agent.max_steps = min(int(prev_max_steps), 4)try:backoff_rounds = 0while time.monotonic() < deadline:rate_limited_models = set()rate_limited_providers = set()26 unmodified linesbuilt_model.timeout = call_timeoutself.agent.model = built_modelresult = self.agent.run(prompt)raw_result = "" if result is None else str(result).strip()if raw_result.lower().startswith("error in generating"):raise RuntimeError(raw_result)cleaned = self._clean_exact_match_answer(raw_result)if cleaned and not cleaned.startswith("AGENT ERROR:"):return cleanedblocked_models.add(model_name)68 unmodified linesif self._rotate_api_key(prov):rotated_any = Trueif not rotated_any:if rate_limited_models and backoff_rounds < 3 and (deadline - time.monotonic()) > 15.0:backoff_rounds += 1time.sleep(4.0 * backoff_rounds)continuebreakfinally:if prev_max_steps is not None:
app.py+32-11