Spaces:
Sleeping
Sleeping
Update app.py
Browse files23 unmodified lines242526272829303132333435363738394041416 unmodified lines45845946046146246346446546646746846947047147247347447547647747847948048148248348448548648782 unmodified lines57057157257357457523 unmodified lines59960060160260360460552 unmodified lines65865966066166266366466566666766866956 unmodified lines7267277287297307317323 unmodified lines7367377387397407417424 unmodified lines74774874975075175275375475575675775829 unmodified lines78878979079179279379415 unmodified lines8108118128138148158166 unmodified lines8238248258268278288298 unmodified lines83883984084184284384420 unmodified lines86586686786886987087187223 unmodified linesm.strip()for m in os.getenv("GROQ_MODELS","llama-3.1-8b-instant,llama-3.3-70b-versatile",).split(",")if m.strip()]GROQ_VISION_MODEL = os.getenv("GROQ_VISION_MODEL", "meta-llama/llama-4-scout-17b-16e-instruct")GROQ_WHISPER_MODEL = os.getenv("GROQ_WHISPER_MODEL", "whisper-large-v3-turbo")MAX_TOOL_ITERATIONS = 5TOOL_RESULT_MAX_CHARS = 1800 # smaller -> stays under 6K TPM for 70b fallbackHISTORY_TRIM_AFTER = 6 # trim old tool turns once we have this many messagesANSWER_CACHE_PATH = os.getenv("ANSWER_CACHE_PATH", "answers_cache.json")INTER_QUESTION_SLEEP = float(os.getenv("INTER_QUESTION_SLEEP", "2"))# Track downloaded task files so vision/audio tools can re-use them by task_id._TASK_FILE_CACHE: dict[str, dict] = {}416 unmodified linesTools: web_search, fetch_url, wikipedia, python, get_task_file, transcribe_audio, view_image, youtube_transcript.Decide tools by reading the question carefully:- ONLY call get_task_file if the question literally says "attached file", "attached image", ".mp3", ".xlsx", ".pdf", ".py", "the file", "the image", "the audio", "the recording", or similar. If get_task_file returns NO_FILE, do NOT call it again — answer from web research.- If the question references an audio file/recording, call get_task_file then transcribe_audio.- If it references an image, call get_task_file then view_image with a focused question.- If it contains a YouTube URL, call youtube_transcript(url) directly. (No need for get_task_file.)- If the question text looks reversed (e.g. starts with strange punctuation like ".rewsna" or seems like backwards English), use python to reverse it: `result = "<text>"[::-1]` then answer that reversed question.- For factual lookups, prefer wikipedia first for entities/people/places, web_search + fetch_url for everything else.- Use python for ALL arithmetic, sums, date math, sorting, alphabetizing. Never compute by hand.Be concise with tool calls — you have at most 5 tool turns, so plan well.Answer formatting (CRITICAL — grader does an exact-match comparison):- Reply with ONLY the answer. No preamble, no explanation, no quotes, no trailing period, no markdown.- Do NOT include the words "FINAL ANSWER", "Answer:", or any label.- Numbers: digits only, no commas, no units, no $ sign — UNLESS the question asks for the unit.- Currency with "two decimal places": e.g. "89706.00" not "$89,706" not "89706".- Strings: no leading articles ("the", "a") unless required; spell out, no abbreviations; write digits as digits.- For names: just the requested form (first name only / last name only / full name) — read the question carefully.- Lists: comma-separated, single space after each comma, applying the rules above to each element.- For "alphabetical order" lists, sort them.- For "ascending order" numeric lists, sort numerically."""# ---------------------------------------------------------------------------# Agent# ---------------------------------------------------------------------------82 unmodified linesreturn head + [summary] + taildef __call__(self, question: str, task_id: str | None = None) -> str:user_content = questionif task_id:user_content = f"task_id: {task_id}\n\nQuestion: {question}"23 unmodified linesif not tool_calls:answer = (msg.content or "").strip()return self._postprocess_answer(answer, question)messages.append({52 unmodified lines)try:resp = self._chat(self._trim_messages(messages), use_tools=False, max_tokens=200)return self._postprocess_answer((resp.choices[0].message.content or "").strip(), question)except Exception as e:return f"AGENT ERROR: {e}"@staticmethoddef _postprocess_answer(text: str, question: str = "") -> str:if not text:56 unmodified linesusername = f"{profile.username}"print(f"User logged in: {username}")else:return "Please Login to Hugging Face with the button.", Noneapi_url = DEFAULT_API_URLquestions_url = f"{api_url}/questions"3 unmodified linesagent = GroqAgent()except Exception as e:print(f"Error instantiating agent: {e}")return f"Error initializing agent: {e}", Noneagent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"print(agent_code)4 unmodified linesresponse.raise_for_status()questions_data = response.json()if not questions_data:return "Fetched questions list is empty or invalid format.", Noneprint(f"Fetched {len(questions_data)} questions.")except requests.exceptions.RequestException as e:return f"Error fetching questions: {e}", Noneexcept Exception as e:return f"An unexpected error occurred fetching questions: {e}", Noneresults_log = []answers_payload = []29 unmodified linestime.sleep(INTER_QUESTION_SLEEP)if not answers_payload:return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)submission_data = {"username": username.strip(),15 unmodified linesf"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"f"Message: {result_data.get('message', 'No message received.')}")return final_status, pd.DataFrame(results_log)except requests.exceptions.HTTPError as e:status = e.response.status_code if e.response is not None else "?"last_error = e6 unmodified lineserror_detail += f" Detail: {e.response.json().get('detail', e.response.text)}"except Exception:error_detail += f" Response: {e.response.text[:500] if e.response is not None else ''}"return f"Submission Failed: {error_detail}", pd.DataFrame(results_log)except requests.exceptions.Timeout as e:last_error = eprint(f"Submission attempt {attempt + 1} timed out.")8 unmodified linesreturn (f"Submission Failed after retries: {last_error}. Answers are cached at "f"{ANSWER_CACHE_PATH} — re-run to retry without re-querying the model.",pd.DataFrame(results_log),)20 unmodified linesrun_button = gr.Button("Run Evaluation & Submit All Answers")status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])if __name__ == "__main__":23 unmodified lines2425262728293031323334353637383940414243416 unmodified lines46046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751882 unmodified lines60160260360460560660760860961061161261323 unmodified lines63763863964064164264352 unmodified lines69669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574656 unmodified lines8038048058068078088093 unmodified lines8138148158168178188194 unmodified lines82482582682782882983083183283383483529 unmodified lines86586686786886987087187287387487587687787815 unmodified lines8948958968978988999006 unmodified lines9079089099109119129138 unmodified lines92292392492592692792892920 unmodified lines95095195295395495595695795823 unmodified linesm.strip()for m in os.getenv("GROQ_MODELS",# 70b first — much better reasoning. Trim history aggressively to fit 6K TPM."llama-3.3-70b-versatile,llama-3.1-8b-instant",).split(",")if m.strip()]GROQ_VISION_MODEL = os.getenv("GROQ_VISION_MODEL", "meta-llama/llama-4-scout-17b-16e-instruct")GROQ_WHISPER_MODEL = os.getenv("GROQ_WHISPER_MODEL", "whisper-large-v3-turbo")MAX_TOOL_ITERATIONS = 6TOOL_RESULT_MAX_CHARS = 1400 # tighter -> stays under 6K TPM for 70bHISTORY_TRIM_AFTER = 5 # trim aggressivelyANSWER_CACHE_PATH = os.getenv("ANSWER_CACHE_PATH", "/tmp/answers_cache.json")RESULTS_CSV_PATH = "/tmp/gaia_results.csv"INTER_QUESTION_SLEEP = float(os.getenv("INTER_QUESTION_SLEEP", "3"))# Track downloaded task files so vision/audio tools can re-use them by task_id._TASK_FILE_CACHE: dict[str, dict] = {}416 unmodified linesTools: web_search, fetch_url, wikipedia, python, get_task_file, transcribe_audio, view_image, youtube_transcript.Decision rules:- If the question literally references "attached file/image/audio/Excel/PDF/.mp3/.xlsx/.py/code/recording", call get_task_file FIRST. If it returns NO_FILE, do NOT call it again.- Audio file -> transcribe_audio(task_id).- Image file -> view_image(task_id, question="<focused question>").- YouTube URL -> youtube_transcript(url) directly (no get_task_file needed).- For factual lookups about people / places / artists / albums / animals / Wikipedia featured articles, START with wikipedia. Then fetch_url the relevant page if more detail needed.- For everything else research-y, web_search then fetch_url.- Use python for ALL arithmetic, sums (e.g. summing Excel rows), date math, sorting, alphabetizing, set/group operations, string reversal. Never compute by hand.- For Excel/CSV totals, after get_task_file shows you the data, ALWAYS use python to compute the sum precisely.You have 6 tool turns. Be decisive. Do not loop on the same query.ANSWER FORMATTING (the grader does an exact-match comparison; sentence answers ALWAYS lose):Worked examples of correct GAIA format:- Q: "How many albums..." -> "3" (NOT "3 albums" or "There were 3 albums")- Q: "Express your answer in USD with two decimal places" -> "89706.00" (NOT "$89,706" or "89706")- Q: "Give the IOC country code" -> "MLT" (NOT "Malta" or "Malta (MLT)")- Q: "Just the city name without abbreviations" -> "Saint Petersburg"- Q: "Give only the first name" -> "Bartek" (NOT "Bartlomiej" or "Bartek Kasprzykowski")- Q: "Comma separated list ... in alphabetical order" -> "broccoli, celery, fresh basil, lettuce, sweet potatoes, zucchini"- Q: "Under what NASA award number..." -> "80NSSC21K1130" (just the code, NO surrounding sentence)- Q: "Final numeric output from the attached Python code" -> "0" (just the number)- Q: opposite of "left" -> "right" (one word)Strict rules:- Reply with ONLY the answer. No preamble. No explanation. No quotes. No trailing period.- Do NOT include "FINAL ANSWER", "Answer:", or any label.- Numbers: digits only, no commas, no units, no $ — UNLESS the question asks for the unit.- Currency "two decimal places": e.g. "89706.00".- Strings: no leading articles ("the", "a") unless required; no abbreviations (write "Saint" not "St."); digits as digits.- Names: read the question carefully. "First name only" / "last name only" / "surname" / "full name". Match exactly.- Lists: comma-separated, ONE space after each comma. Apply formatting rules to each element. Sort if asked."""def _maybe_reverse_text(question: str) -> str:"""If the question text looks reversed, flip it. Returns possibly-modified question."""# Heuristic: a normal English sentence has many word-frequencies like 'the', 'a', 'of'.# A reversed one has 'eht', 'fo', 'sa', etc., and often starts with punctuation like '.'.q = question.strip()if not q:return questionstarts_with_punct = q[0] in ".,;:!?"reversed_text = q[::-1]# Look for common English words in the reversed version.common = (" the ", " of ", " and ", " to ", " is ", " a ", " in ", " for ")hits = sum(1 for w in common if w in (" " + reversed_text.lower() + " "))if starts_with_punct and hits >= 2:return reversed_textreturn question# ---------------------------------------------------------------------------# Agent# ---------------------------------------------------------------------------82 unmodified linesreturn head + [summary] + taildef __call__(self, question: str, task_id: str | None = None) -> str:# Deterministic preprocess: detect & flip reversed-text trick questions.original_q = questionflipped = _maybe_reverse_text(question)if flipped != question:print("[reversed-text detected, flipping question]")question = flippeduser_content = questionif task_id:user_content = f"task_id: {task_id}\n\nQuestion: {question}"23 unmodified linesif not tool_calls:answer = (msg.content or "").strip()return self._finalize(answer, question)messages.append({52 unmodified lines)try:resp = self._chat(self._trim_messages(messages), use_tools=False, max_tokens=200)return self._finalize((resp.choices[0].message.content or "").strip(), question)except Exception as e:return f"AGENT ERROR: {e}"def _finalize(self, raw: str, question: str) -> str:"""Post-process and, if the answer still looks like a sentence, ask the model to reformat."""cleaned = self._postprocess_answer(raw, question)if not cleaned:return cleaned# If the cleaned answer is suspiciously long or contains explanation-y patterns,# do a single tiny reformat pass.looks_sentence = (len(cleaned.split()) > 12or re.search(r"\b(because|received|grant|seems|unable|sorry|cannot|provides|indicating)\b", cleaned, re.IGNORECASE))if looks_sentence:try:resp = self._chat([{"role": "system","content": ("Extract ONLY the final answer from the assistant text below, ""matching the question's required format exactly. No preamble, ""no explanation, no quotes, no trailing period, no labels."),},{"role": "user","content": f"Question: {question}\n\nAssistant text: {cleaned}\n\nFinal answer:",},],use_tools=False,max_tokens=80,)reformat = (resp.choices[0].message.content or "").strip()reformat = self._postprocess_answer(reformat, question)if reformat:return reformatexcept Exception as e:print(f"reformat pass failed: {e}")return cleaned@staticmethoddef _postprocess_answer(text: str, question: str = "") -> str:if not text:56 unmodified linesusername = f"{profile.username}"print(f"User logged in: {username}")else:return "Please Login to Hugging Face with the button.", None, Noneapi_url = DEFAULT_API_URLquestions_url = f"{api_url}/questions"3 unmodified linesagent = GroqAgent()except Exception as e:print(f"Error instantiating agent: {e}")return f"Error initializing agent: {e}", None, Noneagent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"print(agent_code)4 unmodified linesresponse.raise_for_status()questions_data = response.json()if not questions_data:return "Fetched questions list is empty or invalid format.", None, Noneprint(f"Fetched {len(questions_data)} questions.")except requests.exceptions.RequestException as e:return f"Error fetching questions: {e}", None, Noneexcept Exception as e:return f"An unexpected error occurred fetching questions: {e}", None, Noneresults_log = []answers_payload = []29 unmodified linestime.sleep(INTER_QUESTION_SLEEP)if not answers_payload:df = pd.DataFrame(results_log)df.to_csv(RESULTS_CSV_PATH, index=False)return "Agent did not produce any answers to submit.", df, RESULTS_CSV_PATH# Save results CSV before submission so the user can download even if submit fails.df = pd.DataFrame(results_log)df.to_csv(RESULTS_CSV_PATH, index=False)print(f"Results CSV written to {RESULTS_CSV_PATH}")submission_data = {"username": username.strip(),15 unmodified linesf"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"f"Message: {result_data.get('message', 'No message received.')}")return final_status, df, RESULTS_CSV_PATHexcept requests.exceptions.HTTPError as e:status = e.response.status_code if e.response is not None else "?"last_error = e6 unmodified lineserror_detail += f" Detail: {e.response.json().get('detail', e.response.text)}"except Exception:error_detail += f" Response: {e.response.text[:500] if e.response is not None else ''}"return f"Submission Failed: {error_detail}", df, RESULTS_CSV_PATHexcept requests.exceptions.Timeout as e:last_error = eprint(f"Submission attempt {attempt + 1} timed out.")8 unmodified linesreturn (f"Submission Failed after retries: {last_error}. Answers are cached at "f"{ANSWER_CACHE_PATH} — re-run to retry without re-querying the model.",df,RESULTS_CSV_PATH,)20 unmodified linesrun_button = gr.Button("Run Evaluation & Submit All Answers")status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)results_csv = gr.File(label="Download Results CSV (paste this back to me for tuning)")run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table, results_csv])if __name__ == "__main__":
app.py+125-39