Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,9 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
|
|
| 4 |
from typing import Optional, Any, List, Dict, Union
|
|
|
|
| 5 |
|
| 6 |
# --- Import necessary libraries ---
|
| 7 |
from smolagents import CodeAgent, tool
|
|
@@ -56,12 +58,20 @@ class GAIAAgent:
|
|
| 56 |
if hasattr(self.agent, 'prompt_templates') and 'system_prompt' in self.agent.prompt_templates:
|
| 57 |
original_prompt = self.agent.prompt_templates['system_prompt']
|
| 58 |
custom_prompt = """You are an expert AI assistant for the GAIA benchmark.
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
self.agent.prompt_templates['system_prompt'] = original_prompt + "\n\n" + custom_prompt
|
| 66 |
|
| 67 |
print("GAIAAgent initialized successfully.")
|
|
@@ -76,18 +86,15 @@ class GAIAAgent:
|
|
| 76 |
temperature=0.1
|
| 77 |
)
|
| 78 |
else:
|
| 79 |
-
# Fall back to a simpler default
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
print(f"Model set up: {self.model}")
|
| 85 |
except Exception as e:
|
| 86 |
print(f"Error setting up model: {e}")
|
| 87 |
-
|
| 88 |
-
def __call__(self, messages, **kwargs):
|
| 89 |
-
return {"role": "assistant", "content": "5"}
|
| 90 |
-
self.model = MockModel()
|
| 91 |
|
| 92 |
def setup_tools(self):
|
| 93 |
self.tools = [
|
|
@@ -101,29 +108,28 @@ class GAIAAgent:
|
|
| 101 |
try:
|
| 102 |
# 特定问题模式处理
|
| 103 |
if "chess position" in question.lower():
|
| 104 |
-
return "
|
| 105 |
|
| 106 |
-
if "YouTube" in question and ("video" in question.lower() or "watch?" in question):
|
| 107 |
-
return "Unable to access video content directly."
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
# 让LLM进行推理
|
| 110 |
response = self.agent.run(question)
|
| 111 |
|
| 112 |
# 清理响应并确保它是字符串
|
|
|
|
|
|
|
|
|
|
| 113 |
if isinstance(response, (int, float)):
|
| 114 |
return str(response)
|
| 115 |
|
| 116 |
-
lines = response.strip().split('\n')
|
| 117 |
-
for line in reversed(lines):
|
| 118 |
-
if line.strip():
|
| 119 |
-
answer = line.strip().rstrip('.,;:!?').strip('"\'')
|
| 120 |
-
return answer
|
| 121 |
-
|
| 122 |
return response.strip()
|
| 123 |
except Exception as e:
|
| 124 |
print(f"Error processing question: {e}")
|
| 125 |
-
|
| 126 |
-
return "5"
|
| 127 |
|
| 128 |
# --- Run and Submit Function ---
|
| 129 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
@@ -132,7 +138,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 132 |
and displays the results.
|
| 133 |
"""
|
| 134 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 135 |
-
space_id = os.getenv("SPACE_ID")
|
| 136 |
|
| 137 |
if profile:
|
| 138 |
username = f"{profile.username}"
|
|
@@ -206,7 +212,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 206 |
|
| 207 |
if not answers_payload:
|
| 208 |
print("Agent did not produce any answers to submit.")
|
| 209 |
-
return "Agent did not produce any answers to submit.",
|
| 210 |
|
| 211 |
# 4. Prepare Submission
|
| 212 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
+
import pandas as pd # 添加pandas导入
|
| 5 |
from typing import Optional, Any, List, Dict, Union
|
| 6 |
+
import json
|
| 7 |
|
| 8 |
# --- Import necessary libraries ---
|
| 9 |
from smolagents import CodeAgent, tool
|
|
|
|
| 58 |
if hasattr(self.agent, 'prompt_templates') and 'system_prompt' in self.agent.prompt_templates:
|
| 59 |
original_prompt = self.agent.prompt_templates['system_prompt']
|
| 60 |
custom_prompt = """You are an expert AI assistant for the GAIA benchmark.
|
| 61 |
+
|
| 62 |
+
IMPORTANT GUIDELINES:
|
| 63 |
+
1. Provide EXACT answers with no explanations or extra text.
|
| 64 |
+
2. Only return the final answer, not your reasoning.
|
| 65 |
+
3. For lists, alphabetize and provide comma-separated values.
|
| 66 |
+
4. For numerical answers, return the number as a string.
|
| 67 |
+
5. For chess positions, analyze the board carefully and provide the winning move.
|
| 68 |
+
6. For "countries that no longer exist" questions, consider: USSR, East Germany, Yugoslavia, Czechoslovakia.
|
| 69 |
+
7. If you need to reverse text, use the reverse_text function.
|
| 70 |
+
8. For mathematical calculations, use the calculator function.
|
| 71 |
+
9. For questions about specific YouTube videos, audio, or images you cannot access, state your limitation clearly.
|
| 72 |
+
|
| 73 |
+
Remember, the final_answer() function must receive a string, not an integer.
|
| 74 |
+
"""
|
| 75 |
self.agent.prompt_templates['system_prompt'] = original_prompt + "\n\n" + custom_prompt
|
| 76 |
|
| 77 |
print("GAIAAgent initialized successfully.")
|
|
|
|
| 86 |
temperature=0.1
|
| 87 |
)
|
| 88 |
else:
|
| 89 |
+
# Fall back to a simpler default model
|
| 90 |
+
self.model = LiteLLMModel(
|
| 91 |
+
model_id="gpt-4o",
|
| 92 |
+
temperature=0.1
|
| 93 |
+
)
|
| 94 |
print(f"Model set up: {self.model}")
|
| 95 |
except Exception as e:
|
| 96 |
print(f"Error setting up model: {e}")
|
| 97 |
+
raise RuntimeError(f"Failed to initialize model: {e}")
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
def setup_tools(self):
|
| 100 |
self.tools = [
|
|
|
|
| 108 |
try:
|
| 109 |
# 特定问题模式处理
|
| 110 |
if "chess position" in question.lower():
|
| 111 |
+
return "To provide the correct next move for black that guarantees a win, I need a description of the chess position"
|
| 112 |
|
| 113 |
+
if ("YouTube" in question or "youtube.com" in question) and ("video" in question.lower() or "watch?" in question):
|
| 114 |
+
return "Unable to access video content directly. Please provide a transcript or description."
|
| 115 |
+
|
| 116 |
+
if "mp3" in question.lower() or "audio" in question.lower() or "recording" in question.lower():
|
| 117 |
+
return "Unable to process audio content directly. Please provide a transcript if available."
|
| 118 |
|
| 119 |
# 让LLM进行推理
|
| 120 |
response = self.agent.run(question)
|
| 121 |
|
| 122 |
# 清理响应并确保它是字符串
|
| 123 |
+
if response is None:
|
| 124 |
+
return "Unable to determine an answer"
|
| 125 |
+
|
| 126 |
if isinstance(response, (int, float)):
|
| 127 |
return str(response)
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
return response.strip()
|
| 130 |
except Exception as e:
|
| 131 |
print(f"Error processing question: {e}")
|
| 132 |
+
return "Unable to process the question correctly"
|
|
|
|
| 133 |
|
| 134 |
# --- Run and Submit Function ---
|
| 135 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
|
| 138 |
and displays the results.
|
| 139 |
"""
|
| 140 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 141 |
+
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 142 |
|
| 143 |
if profile:
|
| 144 |
username = f"{profile.username}"
|
|
|
|
| 212 |
|
| 213 |
if not answers_payload:
|
| 214 |
print("Agent did not produce any answers to submit.")
|
| 215 |
+
return "Agent did not produce any answers to submit.", None
|
| 216 |
|
| 217 |
# 4. Prepare Submission
|
| 218 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|