Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,9 @@ import re
|
|
| 3 |
import json
|
| 4 |
import traceback
|
| 5 |
import tempfile
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
import spaces
|
| 8 |
import wikipedia
|
| 9 |
import gradio as gr
|
|
@@ -65,7 +67,31 @@ def get_openrouter_headers() -> dict[str, str]:
|
|
| 65 |
"HTTP-Referer": "https://huggingface.co/spaces/Haider67/Final_Assignment_Agent",
|
| 66 |
"X-OpenRouter-Title": "GAIA Benchmark Agent",
|
| 67 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
def generate_openrouter_response(prompt: str) -> str:
|
| 71 |
payload = {
|
|
@@ -82,7 +108,7 @@ def generate_openrouter_response(prompt: str) -> str:
|
|
| 82 |
},
|
| 83 |
}
|
| 84 |
|
| 85 |
-
response =
|
| 86 |
OPENROUTER_API_URL,
|
| 87 |
headers=get_openrouter_headers(),
|
| 88 |
json=payload,
|
|
@@ -148,6 +174,18 @@ def generate_openrouter_response(prompt: str) -> str:
|
|
| 148 |
)
|
| 149 |
|
| 150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
def generate_answers(prompts: list[str]) -> list[str]:
|
| 152 |
answers = []
|
| 153 |
|
|
@@ -157,9 +195,14 @@ def generate_answers(prompts: list[str]) -> list[str]:
|
|
| 157 |
f"with {OPENROUTER_MODEL}"
|
| 158 |
)
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
answers.append(answer)
|
|
|
|
| 163 |
|
| 164 |
return answers
|
| 165 |
|
|
@@ -223,15 +266,22 @@ def read_attached_file(file_path: str) -> str:
|
|
| 223 |
with open(file_path, "r", encoding="utf-8") as file:
|
| 224 |
code = file.read()
|
| 225 |
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
return (
|
| 233 |
-
"
|
| 234 |
-
|
|
|
|
| 235 |
)
|
| 236 |
|
| 237 |
# Audio files: transcribe through OpenRouter Whisper.
|
|
@@ -302,13 +352,19 @@ def read_attached_file(file_path: str) -> str:
|
|
| 302 |
def build_clean_query(question: str) -> str:
|
| 303 |
query = re.sub(r"https?://\S+", "", question)
|
| 304 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
words = [
|
| 306 |
word
|
| 307 |
for word in re.findall(r"[A-Za-z0-9'-]+", query)
|
| 308 |
-
if len(word) > 2
|
| 309 |
]
|
| 310 |
|
| 311 |
-
return " ".join(words[:
|
| 312 |
|
| 313 |
|
| 314 |
def search_web_and_wiki(question: str) -> str:
|
|
@@ -360,7 +416,14 @@ def search_web_and_wiki(question: str) -> str:
|
|
| 360 |
# =========================================================
|
| 361 |
# MAIN GAIA EVALUATION
|
| 362 |
# =========================================================
|
| 363 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 364 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 365 |
try:
|
| 366 |
# Check Hugging Face login
|
|
@@ -438,8 +501,12 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 438 |
file_name,
|
| 439 |
)
|
| 440 |
|
| 441 |
-
#
|
| 442 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 443 |
context = read_attached_file(file_path)
|
| 444 |
else:
|
| 445 |
context = search_web_and_wiki(question_text)
|
|
|
|
| 3 |
import json
|
| 4 |
import traceback
|
| 5 |
import tempfile
|
| 6 |
+
import subprocess
|
| 7 |
+
import sys
|
| 8 |
+
import time
|
| 9 |
import spaces
|
| 10 |
import wikipedia
|
| 11 |
import gradio as gr
|
|
|
|
| 67 |
"HTTP-Referer": "https://huggingface.co/spaces/Haider67/Final_Assignment_Agent",
|
| 68 |
"X-OpenRouter-Title": "GAIA Benchmark Agent",
|
| 69 |
}
|
| 70 |
+
|
| 71 |
+
RETRY_STATUS = {429, 500, 502, 503, 504}
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def post_with_retry(url: str, max_attempts: int = 4, **kwargs):
|
| 75 |
+
last_error = None
|
| 76 |
+
|
| 77 |
+
for attempt in range(1, max_attempts + 1):
|
| 78 |
+
try:
|
| 79 |
+
response = requests.post(url, **kwargs)
|
| 80 |
+
except requests.RequestException as error:
|
| 81 |
+
last_error = error
|
| 82 |
+
print(f"Request error (attempt {attempt}): {error}")
|
| 83 |
+
time.sleep(5 * attempt)
|
| 84 |
+
continue
|
| 85 |
|
| 86 |
+
if response.status_code in RETRY_STATUS and attempt < max_attempts:
|
| 87 |
+
wait = 10 * attempt
|
| 88 |
+
print(f"HTTP {response.status_code}, retrying in {wait}s...")
|
| 89 |
+
time.sleep(wait)
|
| 90 |
+
continue
|
| 91 |
+
|
| 92 |
+
return response
|
| 93 |
+
|
| 94 |
+
raise RuntimeError(f"Request failed after {max_attempts} attempts: {last_error}")
|
| 95 |
|
| 96 |
def generate_openrouter_response(prompt: str) -> str:
|
| 97 |
payload = {
|
|
|
|
| 108 |
},
|
| 109 |
}
|
| 110 |
|
| 111 |
+
response = post_with_retry(
|
| 112 |
OPENROUTER_API_URL,
|
| 113 |
headers=get_openrouter_headers(),
|
| 114 |
json=payload,
|
|
|
|
| 174 |
)
|
| 175 |
|
| 176 |
|
| 177 |
+
def clean_answer(answer: str) -> str:
|
| 178 |
+
answer = answer.strip()
|
| 179 |
+
answer = re.sub(
|
| 180 |
+
r"^(final answer|answer)\s*:\s*",
|
| 181 |
+
"",
|
| 182 |
+
answer,
|
| 183 |
+
flags=re.IGNORECASE,
|
| 184 |
+
)
|
| 185 |
+
answer = answer.strip().strip('"').strip("'").strip()
|
| 186 |
+
return answer.rstrip(".").strip()
|
| 187 |
+
|
| 188 |
+
|
| 189 |
def generate_answers(prompts: list[str]) -> list[str]:
|
| 190 |
answers = []
|
| 191 |
|
|
|
|
| 195 |
f"with {OPENROUTER_MODEL}"
|
| 196 |
)
|
| 197 |
|
| 198 |
+
try:
|
| 199 |
+
answer = clean_answer(generate_openrouter_response(user_prompt))
|
| 200 |
+
except Exception as error:
|
| 201 |
+
print(f"Question {index} failed: {error}")
|
| 202 |
+
answer = "unknown"
|
| 203 |
+
|
| 204 |
answers.append(answer)
|
| 205 |
+
time.sleep(3) # stay under the free-tier per-minute limit
|
| 206 |
|
| 207 |
return answers
|
| 208 |
|
|
|
|
| 266 |
with open(file_path, "r", encoding="utf-8") as file:
|
| 267 |
code = file.read()
|
| 268 |
|
| 269 |
+
try:
|
| 270 |
+
run = subprocess.run(
|
| 271 |
+
[sys.executable, file_path],
|
| 272 |
+
capture_output=True,
|
| 273 |
+
text=True,
|
| 274 |
+
timeout=30,
|
| 275 |
+
)
|
| 276 |
+
output = (run.stdout or "").strip()
|
| 277 |
+
errors = (run.stderr or "").strip()
|
| 278 |
+
except subprocess.TimeoutExpired:
|
| 279 |
+
output, errors = "", "Execution timed out after 30 seconds."
|
| 280 |
|
| 281 |
return (
|
| 282 |
+
f"Python Source Code:\n{code[:10000]}\n\n"
|
| 283 |
+
f"Actual Program Output (stdout):\n{output[:3000]}\n\n"
|
| 284 |
+
f"Errors (stderr):\n{errors[:1000]}"
|
| 285 |
)
|
| 286 |
|
| 287 |
# Audio files: transcribe through OpenRouter Whisper.
|
|
|
|
| 352 |
def build_clean_query(question: str) -> str:
|
| 353 |
query = re.sub(r"https?://\S+", "", question)
|
| 354 |
|
| 355 |
+
stop_words = {
|
| 356 |
+
"the", "and", "for", "that", "this", "with", "what", "who",
|
| 357 |
+
"how", "many", "was", "were", "are", "you", "can", "use",
|
| 358 |
+
"give", "only", "please", "your", "from", "have", "has",
|
| 359 |
+
}
|
| 360 |
+
|
| 361 |
words = [
|
| 362 |
word
|
| 363 |
for word in re.findall(r"[A-Za-z0-9'-]+", query)
|
| 364 |
+
if len(word) > 2 and word.lower() not in stop_words
|
| 365 |
]
|
| 366 |
|
| 367 |
+
return " ".join(words[:16])
|
| 368 |
|
| 369 |
|
| 370 |
def search_web_and_wiki(question: str) -> str:
|
|
|
|
| 416 |
# =========================================================
|
| 417 |
# MAIN GAIA EVALUATION
|
| 418 |
# =========================================================
|
| 419 |
+
def reversed_text_hint(question: str) -> str:
|
| 420 |
+
if question.strip().startswith(".") or "rewsna" in question.lower():
|
| 421 |
+
return (
|
| 422 |
+
"Note: the question above is written backwards. "
|
| 423 |
+
"Reversed, it reads:\n" + question[::-1]
|
| 424 |
+
)
|
| 425 |
+
return ""
|
| 426 |
+
|
| 427 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 428 |
try:
|
| 429 |
# Check Hugging Face login
|
|
|
|
| 501 |
file_name,
|
| 502 |
)
|
| 503 |
|
| 504 |
+
# Reversed-text hint, attachment, or web search context
|
| 505 |
+
hint = reversed_text_hint(question_text)
|
| 506 |
+
|
| 507 |
+
if hint:
|
| 508 |
+
context = hint
|
| 509 |
+
elif file_path:
|
| 510 |
context = read_attached_file(file_path)
|
| 511 |
else:
|
| 512 |
context = search_web_and_wiki(question_text)
|