Update app.py
Browse files8 unmodified lines9101112131415166 unmodified lines23242526272829303132333435363738394041424344454647484950515253542 unmodified lines575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931947 unmodified lines2022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712723 unmodified lines2762772782792802812822832842852862872882892902912928 unmodified lines# --- Constants ---DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"TIMEOUT_PER_QUESTION = 30DELAY_BETWEEN_QUESTIONS = 6 # Longer delay to avoid rate limits# ============================================# GROQ CLIENT6 unmodified linesreturn Groq(api_key=api_key)# ============================================# TOOL FUNCTIONS# ============================================def web_search(query: str, num_results: int = 3) -> str:"""Search the web"""try:with DDGS() as ddgs:results = list(ddgs.text(query, max_results=num_results))if not results:return "No results found"output = []for r in results:output.append(f"- {r.get('title', '')}: {r.get('body', '')}")return "\n".join(output)except Exception as e:return f"Search error: {e}"def get_task_file(task_id: str) -> dict:"""Get GAIA task file"""try:url = f"https://agents-course-unit4-scoring.hf.space/files/{task_id}"response = requests.get(url, timeout=15)if response.status_code == 404:return {"has_file": False, "content": ""}content_type = response.headers.get('content-type', '').lower()disposition = response.headers.get('content-disposition', '')2 unmodified linesif 'filename=' in disposition:filename = disposition.split('filename=')[-1].strip('"\'')result = {"has_file": True, "filename": filename, "type": content_type}# Text/code filesif 'text' in content_type or filename.endswith(('.txt', '.py', '.md', '.csv', '.json')):result["content"] = response.text[:6000]return result# Excel filesif 'spreadsheet' in content_type or 'excel' in content_type or filename.endswith(('.xlsx', '.xls')):try:from io import BytesIOdf = pd.read_excel(BytesIO(response.content))result["content"] = f"Excel data:\n{df.to_string()}"return resultexcept:result["content"] = "Excel file (cannot parse)"return result# Images - can't processif 'image' in content_type:result["content"] = "[IMAGE FILE - Cannot analyze]"result["is_image"] = Truereturn resultresult["content"] = f"[Binary file: {content_type}]"return resultexcept Exception as e:return {"has_file": False, "content": ""}def reverse_string(text: str) -> str:return text[::-1]def is_reversed_text(text: str) -> bool:indicators = ['.rewsna', 'eht sa', 'tfel', 'drow eht']return any(ind in text.lower() for ind in indicators)# ============================================# AGENT CLASS# ============================================class BasicAgent:def __init__(self):print("Initializing Groq agent...")self.client = get_groq_client()print("✅ Agent ready!")def ask_llm(self, prompt: str) -> str:"""Ask Groq - using faster model with better rate limits"""max_retries = 2for attempt in range(max_retries):try:# Use mixtral - good balance of speed and qualityresponse = self.client.chat.completions.create(model="mixtral-8x7b-32768", # Better rate limits than llama-70bmessages=[{"role": "user", "content": prompt}],temperature=0,max_tokens=150,timeout=TIMEOUT_PER_QUESTION,)return response.choices[0].message.content.strip()except Exception as e:if "rate" in str(e).lower() or "429" in str(e):wait = (attempt + 1) * 10print(f" ⏳ Rate limited, waiting {wait}s...")time.sleep(wait)else:return f"Error: {e}"return "unknown"def clean_answer(self, answer: str) -> str:# Remove prefixesfor prefix in ["Answer:", "The answer is:", "Final answer:", "A:", "The answer is", "**"]:if answer.lower().startswith(prefix.lower()):answer = answer[len(prefix):].strip()# Remove quotes and trailing punctuationanswer = answer.strip('"\'')if answer.endswith('.') and len(answer.split()) <= 3:answer = answer[:-1]# Remove markdownanswer = answer.replace("**", "").strip()return answerdef __call__(self, question: str, task_id: str = None) -> str:try:context = ""# Check for reversed textif is_reversed_text(question):question = reverse_string(question)context += f"[Decoded reversed text]\n"# Check for fileif task_id:file_info = get_task_file(task_id)if file_info.get("has_file") and file_info.get("content"):context += f"FILE:\n{file_info['content']}\n\n"# Web search for questions that need itneeds_search = any(kw in question.lower() for kw in ["who ", "what ", "when ", "where ", "how many", "how much","album", "actor", "movie", "wikipedia", "surname", "athlete","pitcher", "country", "competition", "nominated"])# Don't search if we have file contentif context and "FILE:" in context:needs_search = Falseif needs_search:search_results = web_search(question[:100], 3)if "No results" not in search_results:context += f"SEARCH RESULTS:\n{search_results}\n\n"prompt = f"""{context}Question: {question}Give ONLY the final answer. No explanation. Be precise."""answer = self.ask_llm(prompt)return self.clean_answer(answer)except Exception as e:print(f" Error: {e}")return "unknown"# ============================================7 unmodified linesusername = profile.usernamespace_id = os.getenv("SPACE_ID")print(f"\n{'='*50}")print(f"User: {username}")if not os.environ.get("GROQ_API_KEY"):return "❌ Add GROQ_API_KEY to Space secrets!", Noneprint("✅ GROQ_API_KEY found")print(f"{'='*50}\n")try:agent = BasicAgent()except Exception as e:return f"❌ Agent init failed: {e}", Nonetry:questions = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15).json()print(f"���� {len(questions)} questions\n")except Exception as e:return f"❌ Failed to fetch questions: {e}", Noneresults = []answers = []start_time = time.time()for i, q in enumerate(questions):task_id = q.get("task_id")question = q.get("question", "")print(f"[{i+1}/{len(questions)}] {question[:60]}...")try:answer = agent(question, task_id)print(f" → {answer[:50]}")except Exception as e:answer = "unknown"print(f" ✗ {e}")answers.append({"task_id": task_id, "submitted_answer": answer})results.append({"#": i+1, "Question": question[:50]+"...", "Answer": answer[:60]})# Delay between questionsif i < len(questions) - 1:time.sleep(DELAY_BETWEEN_QUESTIONS)total_time = time.time() - start_timeprint(f"\n⏱️ {total_time:.0f}s total")# Submittry:submission = {"username": username,"agent_code": f"https://huggingface.co/spaces/{space_id}/tree/main","answers": answers}result = requests.post(f"{DEFAULT_API_URL}/submit", json=submission, timeout=60).json()score = result.get('score', 0)correct = result.get('correct_count', 0)total = result.get('total_attempted', 0)status = f"✅ Done in {total_time:.0f}s\n\n🎯 Score: {score}% ({correct}/{total})\n\n"status += "🎉 PASSED!" if score >= 30 else f"Need {30-score}% more"return status, pd.DataFrame(results)except Exception as e:return f"❌ Submit failed: {e}", pd.DataFrame(results)3 unmodified lines# ============================================with gr.Blocks() as demo:gr.Markdown("# 🎯 GAIA Agent - Unit 4")gr.Markdown("**Groq + Mixtral 8x7B** (better rate limits)")gr.LoginButton()run_btn = gr.Button("🚀 Run", variant="primary", size="lg")status = gr.Textbox(label="Status", lines=5)table = gr.DataFrame(label="Results")run_btn.click(run_and_submit_all, outputs=[status, table])if __name__ == "__main__":print("🎯 GAIA Agent Starting...")print(f"GROQ_API_KEY: {'✅' if os.environ.get('GROQ_API_KEY') else '❌'}")demo.launch()8 unmodified lines91011121314156 unmodified lines2223242526272829303132333435363738394041424344454647482 unmodified lines515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461477 unmodified lines1551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162173 unmodified lines2212222232242252262272282292302312322332342352368 unmodified lines# --- Constants ---DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"DELAY_BETWEEN_QUESTIONS = 8 # 8 seconds between questions to avoid rate limits# ============================================# GROQ CLIENT6 unmodified linesreturn Groq(api_key=api_key)# ============================================# TOOLS# ============================================def web_search(query: str) -> str:try:with DDGS() as ddgs:results = list(ddgs.text(query, max_results=3))if not results:return "No results"return "\n".join([f"- {r.get('title','')}: {r.get('body','')}" for r in results])except:return "Search failed"def get_task_file(task_id: str) -> dict:try:url = f"https://agents-course-unit4-scoring.hf.space/files/{task_id}"response = requests.get(url, timeout=15)if response.status_code == 404:return {"has_file": False}content_type = response.headers.get('content-type', '').lower()disposition = response.headers.get('content-disposition', '')2 unmodified linesif 'filename=' in disposition:filename = disposition.split('filename=')[-1].strip('"\'')# Text filesif 'text' in content_type or filename.endswith(('.txt', '.py', '.md', '.csv', '.json')):return {"has_file": True, "content": response.text[:5000]}# Excelif 'excel' in content_type or 'spreadsheet' in content_type or filename.endswith(('.xlsx', '.xls')):try:from io import BytesIOdf = pd.read_excel(BytesIO(response.content))return {"has_file": True, "content": df.to_string()}except:return {"has_file": True, "content": "[Excel file - parse failed]"}# Imageif 'image' in content_type:return {"has_file": True, "content": "[IMAGE - cannot process]", "is_image": True}return {"has_file": True, "content": f"[Binary: {content_type}]"}except:return {"has_file": False}def reverse_string(text: str) -> str:return text[::-1]def is_reversed(text: str) -> bool:return any(x in text.lower() for x in ['.rewsna', 'eht sa', 'tfel', 'drow eht'])# ============================================# AGENT# ============================================class BasicAgent:def __init__(self):print("Initializing Groq agent...")self.client = get_groq_client()print("✅ Ready!")def ask(self, prompt: str) -> str:for attempt in range(3):try:response = self.client.chat.completions.create(model="llama-3.1-8b-instant", # Faster model with better rate limitsmessages=[{"role": "user", "content": prompt}],temperature=0,max_tokens=100,)return response.choices[0].message.content.strip()except Exception as e:if "rate" in str(e).lower() or "429" in str(e):wait = (attempt + 1) * 15print(f" ⏳ Rate limit, waiting {wait}s...")time.sleep(wait)else:return f"Error: {e}"return "unknown"def __call__(self, question: str, task_id: str = None) -> str:context = ""# Handle reversed textif is_reversed(question):question = reverse_string(question)# Get file if existsif task_id:file = get_task_file(task_id)if file.get("has_file") and file.get("content"):context = f"FILE: {file['content']}\n\n"# Search if needed and no fileif not context:keywords = ["who", "what", "when", "where", "how many", "album", "actor", "surname", "wikipedia"]if any(k in question.lower() for k in keywords):results = web_search(question[:80])if results != "Search failed":context = f"INFO: {results}\n\n"prompt = f"{context}Q: {question}\n\nAnswer with ONLY the final answer, nothing else:"answer = self.ask(prompt)# Cleanfor p in ["Answer:", "The answer is:", "A:", "Final answer:"]:if answer.lower().startswith(p.lower()):answer = answer[len(p):].strip()answer = answer.strip('"\'.')return answer# ============================================7 unmodified linesusername = profile.usernamespace_id = os.getenv("SPACE_ID")if not os.environ.get("GROQ_API_KEY"):return "❌ Add GROQ_API_KEY to secrets!", Noneprint(f"\n{'='*40}\nUser: {username}\n{'='*40}")try:agent = BasicAgent()except Exception as e:return f"❌ Init failed: {e}", Nonetry:questions = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15).json()print(f"���� {len(questions)} questions\n")except Exception as e:return f"❌ Fetch failed: {e}", Noneresults = []answers = []start = time.time()for i, q in enumerate(questions):task_id = q.get("task_id")question = q.get("question", "")print(f"[{i+1}/{len(questions)}] {question[:50]}...")answer = agent(question, task_id)print(f" → {answer[:40]}")answers.append({"task_id": task_id, "submitted_answer": answer})results.append({"#": i+1, "Q": question[:40]+"...", "A": answer[:50]})if i < len(questions) - 1:time.sleep(DELAY_BETWEEN_QUESTIONS)total = time.time() - startprint(f"\n⏱️ {total:.0f}s")try:result = requests.post(f"{DEFAULT_API_URL}/submit",json={"username": username,"agent_code": f"https://huggingface.co/spaces/{space_id}/tree/main","answers": answers},timeout=60).json()score = result.get('score', 0)correct = result.get('correct_count', 0)total_q = result.get('total_attempted', 0)status = f"✅ Done in {total:.0f}s\n\n🎯 {score}% ({correct}/{total_q})\n\n"status += "🎉 PASSED!" if score >= 30 else f"Need {30-score}% more"return status, pd.DataFrame(results)except Exception as e:return f"❌ Submit failed: {e}", pd.DataFrame(results)3 unmodified lines# ============================================with gr.Blocks() as demo:gr.Markdown("# 🎯 GAIA Agent")gr.Markdown("Groq + Llama 3.1 8B (fast, good rate limits)")gr.LoginButton()run_btn = gr.Button("🚀 Run", variant="primary", size="lg")status = gr.Textbox(label="Status", lines=5)table = gr.DataFrame(label="Results")run_btn.click(run_and_submit_all, outputs=[status, table])if __name__ == "__main__":print(f"GROQ_API_KEY: {'✅' if os.environ.get('GROQ_API_KEY') else '❌'}")demo.launch()
app.py+85-141