Spaces:
Sleeping
Sleeping
Update app.py
Browse files30 unmodified lines3132333435363710 unmodified lines484950515253547 unmodified lines62636465666761 unmodified lines1291301311321331341351361371381391401411421431441451461472 unmodified lines1501511521531541551561571581591601611621631641651663 unmodified lines1701711721731741751769 unmodified lines18618718818919019119219319419519619719812 unmodified lines21121221321421521621721821930 unmodified linesdef normalize_answer(ans) -> str:# Если это объект с content (AIMessage и т.п.), извлекаем строкуif hasattr(ans, "content"):ans = ans.contentans = str(ans).strip().strip('"').strip("'")10 unmodified linesif m:return m.group(1)# 3) Последнее числоnums = re.findall(r'\b(\d+)\b', ans)if nums:return nums[-1]7 unmodified linesdef run_and_submit_all(profile: gr.OAuthProfile | None):if not profile:return "Please login to Hugging Face via the button above.", Noneusername = profile.username61 unmodified linesif not tid or not q:continue# 3.1 GAIA override для "how many" вопросовif q.lower().startswith("how many"):raw = GaiaQATool()._run(q)ans = normalize_answer(raw)else:# 3.2 Проверка и загрузка вложенного файла через APIfile_entry = item.get("file") or item.get("file_url") or item.get("attachment")file_path = Noneif file_entry:try:resp = requests.get(f"{api_url}/files/{tid}", timeout=15)resp.raise_for_status()# определяем расширениеext = os.path.splitext(str(file_entry))[-1] or ""tmp_path = f"/tmp/{tid}{ext}"with open(tmp_path, "wb") as f:2 unmodified linesexcept Exception:file_path = None# 3.3 Прямое исполнение .py, если это скриптif file_path and file_path.lower().endswith(".py"):raw = PythonExecTool()._run(file_path)ans = normalize_answer(raw)else:# 3.4 Общий проход через агента# 3.4 Общий проход через агентаprompt = qif file_path:prompt += f"FilePath: {file_path}"raw = agent.invoke(prompt)if hasattr(raw, "content"):raw = raw.content3 unmodified linesexcept Exception:passans = normalize_answer(raw)(raw)results.append({"Task ID": tid, "Question": q, "Answer": ans})payload.append({"task_id": tid, "submitted_answer": ans})9 unmodified linesresp2.raise_for_status()data = resp2.json()status = (f"Submission Successful!"f"User: {data.get('username')}"f"Score: {data.get('score','N/A')} ({data.get('correct_count','?')}/"f"{data.get('total_attempted','?')})"f"{data.get('message','')}")except Exception as e:12 unmodified linesif __name__ == "__main__":demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT",7860)))30 unmodified lines3132333435363710 unmodified lines484950515253547 unmodified lines6263646566676861 unmodified lines1301311321331341351361371381391401411421431441451461472 unmodified lines1501511521531541551561571581591601611621631643 unmodified lines1681691701711721739 unmodified lines18318418518618718818919019119212 unmodified lines20520620730 unmodified linesdef normalize_answer(ans) -> str:# Если это объект с content (AIMessage и т.п.), извлекаем текстif hasattr(ans, "content"):ans = ans.contentans = str(ans).strip().strip('"').strip("'")10 unmodified linesif m:return m.group(1)# 3) Последнее найденное числоnums = re.findall(r'\b(\d+)\b', ans)if nums:return nums[-1]7 unmodified linesdef run_and_submit_all(profile: gr.OAuthProfile | None):# Проверка логинаif not profile:return "Please login to Hugging Face via the button above.", Noneusername = profile.username61 unmodified linesif not tid or not q:continue# 3.1 GAIA override для вопросов "how many"if q.lower().startswith("how many"):raw = GaiaQATool()._run(q)ans = normalize_answer(raw)else:# 3.2 Загрузка вложенного файла через APIfile_entry = item.get("file") or item.get("file_url") or item.get("attachment")file_path = Noneif file_entry:try:resp = requests.get(f"{api_url}/files/{tid}", timeout=15)resp.raise_for_status()ext = os.path.splitext(str(file_entry))[-1] or ""tmp_path = f"/tmp/{tid}{ext}"with open(tmp_path, "wb") as f:2 unmodified linesexcept Exception:file_path = None# 3.3 Прямое исполнение .pyif file_path and file_path.lower().endswith(".py"):raw = PythonExecTool()._run(file_path)ans = normalize_answer(raw)else:# 3.4 Общий случай: invoke agentprompt = qif file_path:prompt += f"\nFilePath: {file_path}"raw = agent.invoke(prompt)if hasattr(raw, "content"):raw = raw.content3 unmodified linesexcept Exception:passans = normalize_answer(raw)results.append({"Task ID": tid, "Question": q, "Answer": ans})payload.append({"task_id": tid, "submitted_answer": ans})9 unmodified linesresp2.raise_for_status()data = resp2.json()status = (f"Submission Successful!\n"f"User: {data.get('username')}\n"f"Score: {data.get('score','N/A')} ({data.get('correct_count','?')}/"f"{data.get('total_attempted','?')})\n"f"{data.get('message','')}")except Exception as e:12 unmodified linesif __name__ == "__main__":demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT",7860)))
app.py+11-23