Update app.py
Browse files181 unmodified lines18218318418518618718818919019119219319419519619719819920020120220341 unmodified lines24524624724824925025125225325425525625725825926026126226326426516 unmodified lines2822832842852862872882892902912922932942952962972982993003013023033043053063073081 unmodified line3103113123133143153163173183193203213223233243253263273283293303313323333343353362 unmodified lines3393403413423433443453463473483493503513523535 unmodified lines35936036136236336436536623 unmodified lines3903913923933943953963973982 unmodified lines40140240340440540640740840941014 unmodified lines4254264274284294304314324334348 unmodified lines4434444454464474484494504514524534544554564574584595 unmodified lines4654664674684694704714724734744754764774 unmodified lines48248348448548648748848949049149249349449549611 unmodified lines50850951051151251351451551611 unmodified lines5285295305315325335345355365372 unmodified lines5405415425435445455465475485 unmodified lines5545555565575585595605615625635645655665675685695705715725735745755 unmodified lines58158258358458558658758858959059159259359459559659759859960060160260360460560660760860933 unmodified lines64364464564664764864933 unmodified lines6836846856866876886896901 unmodified line69269369469569669769869970070170270370470570670770870971071171271371471571671771871919 unmodified lines73974074174274374474574674774874975075175275375475575675775875976076176276376476576661 unmodified lines82882983083183283383483583683716 unmodified lines85485585685785885986086186286311 unmodified lines875876877878879880881882883884885886887888183 unmodified lines1072107310741075107610771078107910801081104 unmodified lines118611871188118911901191119211931194181 unmodified lines.lower())if ("text/html" in content_typeand safe_name.lower().endswith((".png",".jpg",".jpeg",".webp",".mp3",".wav",".m4a",".flac",".ogg",".xlsx",".xlsm",))):raise RuntimeError(41 unmodified linestry:for sheet_name in workbook_formulas.sheetnames:ws_formula = (workbook_formulas[sheet_name])ws_value = (workbook_values[sheet_name])output.append(f"\n=== SHEET: "f"{sheet_name} ===")max_rows = min(16 unmodified lines1,max_columns + 1,):formula_cell = (ws_formula.cell(row=row_number,column=column_number,))value_cell = (ws_value.cell(row=row_number,column=column_number,))formula_value = (formula_cell.value)cached_value = (value_cell.value)if (formula_value is None1 unmodified line):continuecoordinate = (formula_cell.coordinate)if (formula_cell.data_type== "f"):rendered = (f"{coordinate}="f"FORMULA("f"{formula_value!r}),"f"CACHED="f"{cached_value!r}")else:final_value = (cached_valueif cached_valueis not Noneelse formula_value)2 unmodified linesf"{final_value!r}")row_items.append(rendered)if row_items:output.append(" | ".join(row_items))finally:5 unmodified linesif len(text) > 80000:text = (text[:80000]+ "\n"+ "[WORKBOOK CONTENT TRUNCATED]")return text23 unmodified lines)if not results:return ("NO WEB SEARCH RESULTS FOUND.")output = []urls = []2 unmodified linesresults,start=1,):title = (result.get("title")or "")url = (result.get("href")14 unmodified linesf"Snippet: {snippet}\n")if (urland len(urls) < 3):urls.append(url)for index, url in enumerate(8 unmodified linesfmt="text_plain",)if isinstance(page,dict,):page_text = str(page.get("content","",))else:page_text = str(page or ""5 unmodified linesif len(page_text) > 8000:page_text = (page_text[:8000]+ "\n"+ "[PAGE TRUNCATED]")output.append(f"\nWEB PAGE "f"{index}\n"f"URL: {url}\n"f"{page_text}\n")4 unmodified linesf"{url}: {error}")research = "\n".join(output)if len(research) > 35000:research = (research[:35000]+ "\n"+ "[WEB RESEARCH TRUNCATED]")return research11 unmodified linesfile_name,)suffix = (path.suffix.lower())if suffix in {".png",11 unmodified linesf"{file_name}")media = (self.client.files.upload(file=str(path)))print(2 unmodified linesreturn (("\nThe task includes the ""attached media file named "f"{file_name}. ""Inspect the actual attachment ""directly and use it when ""answering the question.\n"5 unmodified lines".xlsx",".xlsm",}:content = (self.extract_excel(path))return (("\n\n""=== ATTACHED EXCEL ""WORKBOOK ===\n"f"Filename: "f"{file_name}\n"f"{content}\n""=== END EXCEL ""WORKBOOK ===\n"),None,)5 unmodified lines".json",".md",}:content = (path.read_text(encoding="utf-8",errors="replace",))if len(content) > 80000:content = (content[:80000]+ "\n"+ "[FILE TRUNCATED]")return (("\n\n""=== ATTACHED FILE ===\n"f"Filename: "f"{file_name}\n"f"{content}\n""=== END ATTACHED ""FILE ===\n"),None,)33 unmodified linesidentify whose turn it is, calculate forcing moves andreturn the correct move in the notation requested.For audio, listen to the actual attached audio carefully.For Python files, determine the real program output.33 unmodified linescontents = [prompt]if media is not None:contents.append(media)print(1 unmodified line)response = (self.client.models.generate_content(model=GEMINI_MODEL,contents=contents,config=types.GenerateContentConfig(system_instruction=system_instruction,max_output_tokens=4096,thinking_config=types.ThinkingConfig(thinking_level="medium"),tools=[types.Tool(code_execution=types.ToolCodeExecution)],),))19 unmodified linesattachment_context = ""media = Noneif (task_idand file_name):(attachment_context,media,) = (self.prepare_attachment(task_id,file_name,))print("Running web research...")research = (self.web_research(question))print(61 unmodified linesitem = response.json()if isinstance(item,list,):if not item:return ("Random-question API "16 unmodified linesor "")if (not task_idor question is None):return (f"Invalid API response: "f"{item}",11 unmodified linesresults = pd.DataFrame([{"Task ID":task_id,"Question":question,"File":file_name,"Agent Answer":answer,}])183 unmodified linesf"Task ID: {task_id}")if (not task_idor question_text is None):error_message = ("Invalid question payload.")104 unmodified linesbreakif failed_tasks:failure = (failed_tasks[0])return ((181 unmodified lines18218318418518618718818919019119219319419519619719819920020120220320420541 unmodified lines24724824925025125225325425525625716 unmodified lines2742752762772782792802812822832842852862872882892902911 unmodified line2932942952962972982993003013023033043053063073083093102 unmodified lines3133143153163173183193203213223235 unmodified lines32933033133233333433523 unmodified lines3593603613623633643652 unmodified lines36836937037137237337414 unmodified lines3893903913923933943958 unmodified lines4044054064074084094104114124134144154165 unmodified lines4224234244254264274284294304314324 unmodified lines43743843944044144244344444544644744811 unmodified lines46046146246346446546611 unmodified lines4784794804814824834844852 unmodified lines4884894904914924934944955 unmodified lines5015025035045055065075085095105115125135145155165175 unmodified lines52352452552652752852953053153253353453553653753853954054154254354454554633 unmodified lines58058158258358458558658733 unmodified lines6216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506511 unmodified line65365465565665765865966066166266366419 unmodified lines68468568668768868969069169269369469569669769869970070170270370461 unmodified lines76676776876977077177216 unmodified lines78979079179279379479511 unmodified lines807808809810811812813814815816183 unmodified lines1000100110021003100410051006104 unmodified lines1111111211131114111511161117181 unmodified lines.lower())binary_extensions = (".png",".jpg",".jpeg",".webp",".mp3",".wav",".m4a",".flac",".ogg",".xlsx",".xlsm",)if ("text/html" in content_typeand safe_name.lower().endswith(binary_extensions)):raise RuntimeError(41 unmodified linestry:for sheet_name in workbook_formulas.sheetnames:ws_formula = workbook_formulas[sheet_name]ws_value = workbook_values[sheet_name]output.append(f"\n=== SHEET: {sheet_name} ===")max_rows = min(16 unmodified lines1,max_columns + 1,):formula_cell = ws_formula.cell(row=row_number,column=column_number,)value_cell = ws_value.cell(row=row_number,column=column_number,)formula_value = formula_cell.valuecached_value = value_cell.valueif (formula_value is None1 unmodified line):continuecoordinate = formula_cell.coordinateif formula_cell.data_type == "f":rendered = (f"{coordinate}="f"FORMULA({formula_value!r}),"f"CACHED={cached_value!r}")else:final_value = (cached_valueif cached_value is not Noneelse formula_value)2 unmodified linesf"{final_value!r}")row_items.append(rendered)if row_items:output.append(" | ".join(row_items))finally:5 unmodified linesif len(text) > 80000:text = (text[:80000]+ "\n[WORKBOOK CONTENT TRUNCATED]")return text23 unmodified lines)if not results:return "NO WEB SEARCH RESULTS FOUND."output = []urls = []2 unmodified linesresults,start=1,):title = result.get("title") or ""url = (result.get("href")14 unmodified linesf"Snippet: {snippet}\n")if url and len(urls) < 3:urls.append(url)for index, url in enumerate(8 unmodified linesfmt="text_plain",)if isinstance(page, dict):page_text = str(page.get("content","",))else:page_text = str(page or ""5 unmodified linesif len(page_text) > 8000:page_text = (page_text[:8000]+ "\n[PAGE TRUNCATED]")output.append(f"\nWEB PAGE {index}\n"f"URL: {url}\n"f"{page_text}\n")4 unmodified linesf"{url}: {error}")research = "\n".join(output)if len(research) > 35000:research = (research[:35000]+ "\n[WEB RESEARCH TRUNCATED]")return research11 unmodified linesfile_name,)suffix = path.suffix.lower()if suffix in {".png",11 unmodified linesf"{file_name}")media = self.client.files.upload(file=str(path))print(2 unmodified linesreturn (("\nThe task includes the attached "f"media file named {file_name}. ""Inspect the actual attachment ""directly and use it when ""answering the question.\n"5 unmodified lines".xlsx",".xlsm",}:content = self.extract_excel(path)return (("\n\n""=== ATTACHED EXCEL WORKBOOK ===\n"f"Filename: {file_name}\n"f"{content}\n""=== END EXCEL WORKBOOK ===\n"),None,)5 unmodified lines".json",".md",}:content = path.read_text(encoding="utf-8",errors="replace",)if len(content) > 80000:content = (content[:80000]+ "\n[FILE TRUNCATED]")return (("\n\n""=== ATTACHED FILE ===\n"f"Filename: {file_name}\n"f"{content}\n""=== END ATTACHED FILE ===\n"),None,)33 unmodified linesidentify whose turn it is, calculate forcing moves andreturn the correct move in the notation requested.For audio, listen to the actual attached audio carefullyand answer using what is actually spoken in the recording.For Python files, determine the real program output.33 unmodified linescontents = [prompt]if media is not None:contents.append(media)config_kwargs = {"system_instruction": system_instruction,"max_output_tokens": 4096,"thinking_config": types.ThinkingConfig(thinking_level="medium"),}if media is None:config_kwargs["tools"] = [types.Tool(code_execution=types.ToolCodeExecution)]print("Code execution enabled.")else:print("Media input detected. ""Code execution disabled.")print(1 unmodified line)response = (self.client.models.generate_content(model=GEMINI_MODEL,contents=contents,config=types.GenerateContentConfig(**config_kwargs),))19 unmodified linesattachment_context = ""media = Noneif task_id and file_name:(attachment_context,media,) = self.prepare_attachment(task_id,file_name,)print("Running web research...")research = self.web_research(question)print(61 unmodified linesitem = response.json()if isinstance(item, list):if not item:return ("Random-question API "16 unmodified linesor "")if not task_id or question is None:return (f"Invalid API response: "f"{item}",11 unmodified linesresults = pd.DataFrame([{"Task ID": task_id,"Question": question,"File": file_name,"Agent Answer": answer,}])183 unmodified linesf"Task ID: {task_id}")if not task_id or question_text is None:error_message = ("Invalid question payload.")104 unmodified linesbreakif failed_tasks:failure = failed_tasks[0]return ((
app.py+103-180