Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,9 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
import os
|
|
|
|
| 4 |
import requests
|
|
|
|
| 5 |
import traceback
|
| 6 |
from agent import solve_task, load_tasks
|
| 7 |
from mistral_hf_wrapper import MistralInference
|
|
@@ -11,34 +13,55 @@ API_TOKEN = os.getenv("HF_TOKEN")
|
|
| 11 |
USERNAME = os.getenv("HF_USERNAME")
|
| 12 |
CODE_LINK = os.getenv("HF_CODE_LINK")
|
| 13 |
|
|
|
|
|
|
|
|
|
|
| 14 |
def run_and_submit_all():
|
| 15 |
model = MistralInference(api_url=API_URL, api_token=API_TOKEN)
|
| 16 |
tasks = load_tasks()
|
| 17 |
|
|
|
|
|
|
|
| 18 |
if not tasks:
|
| 19 |
return "No tasks loaded from metadata.jsonl. Make sure the file exists and is valid."
|
| 20 |
|
| 21 |
-
print(f"[INFO] Loaded {len(tasks)} tasks from metadata.jsonl")
|
| 22 |
-
|
| 23 |
answers = []
|
| 24 |
for i, task in enumerate(tasks):
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
try:
|
| 44 |
res = requests.post(
|
|
@@ -49,16 +72,19 @@ def run_and_submit_all():
|
|
| 49 |
"agent_code": CODE_LINK,
|
| 50 |
"answers": answers
|
| 51 |
},
|
| 52 |
-
timeout=60 #
|
| 53 |
)
|
| 54 |
-
|
| 55 |
if res.ok:
|
|
|
|
| 56 |
return json.dumps(res.json(), indent=2)
|
| 57 |
else:
|
| 58 |
return f"Error submitting: {res.status_code} - {res.text}"
|
| 59 |
|
|
|
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
-
return f"
|
|
|
|
| 62 |
# Gradio interface
|
| 63 |
gr.Interface(
|
| 64 |
fn=run_and_submit_all,
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
+
import time
|
| 5 |
import requests
|
| 6 |
+
from tools import TOOLS
|
| 7 |
import traceback
|
| 8 |
from agent import solve_task, load_tasks
|
| 9 |
from mistral_hf_wrapper import MistralInference
|
|
|
|
| 13 |
USERNAME = os.getenv("HF_USERNAME")
|
| 14 |
CODE_LINK = os.getenv("HF_CODE_LINK")
|
| 15 |
|
| 16 |
+
MAX_RETRIES = 3
|
| 17 |
+
INFERENCE_TIMEOUT = 45 # seconds
|
| 18 |
+
|
| 19 |
def run_and_submit_all():
|
| 20 |
model = MistralInference(api_url=API_URL, api_token=API_TOKEN)
|
| 21 |
tasks = load_tasks()
|
| 22 |
|
| 23 |
+
print(f"[INFO] Loaded {len(tasks)} tasks from metadata.jsonl")
|
| 24 |
+
|
| 25 |
if not tasks:
|
| 26 |
return "No tasks loaded from metadata.jsonl. Make sure the file exists and is valid."
|
| 27 |
|
|
|
|
|
|
|
| 28 |
answers = []
|
| 29 |
for i, task in enumerate(tasks):
|
| 30 |
+
task_id = task.get("task_id", f"UNKNOWN-{i}")
|
| 31 |
+
print(f"[INFO] Solving task {i+1}/{len(tasks)}: {task_id}")
|
| 32 |
+
|
| 33 |
+
attempt = 0
|
| 34 |
+
success = False
|
| 35 |
+
while attempt < MAX_RETRIES and not success:
|
| 36 |
+
attempt += 1
|
| 37 |
+
try:
|
| 38 |
+
start = time.time()
|
| 39 |
+
result = solve_task(task, tools=TOOLS)
|
| 40 |
+
duration = time.time() - start
|
| 41 |
+
|
| 42 |
+
# Handle empty responses
|
| 43 |
+
if not result.get("submitted_answer"):
|
| 44 |
+
raise ValueError("Empty model response")
|
| 45 |
|
| 46 |
+
print(f"[INFO] Answer in {duration:.1f}s: {result['submitted_answer'][:100]}...")
|
| 47 |
+
answers.append({
|
| 48 |
+
"task_id": task_id,
|
| 49 |
+
"submitted_answer": result["submitted_answer"].strip()
|
| 50 |
+
})
|
| 51 |
+
success = True
|
| 52 |
+
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print(f"[WARN] Attempt {attempt} failed: {e}")
|
| 55 |
+
if attempt == MAX_RETRIES:
|
| 56 |
+
answers.append({
|
| 57 |
+
"task_id": task_id,
|
| 58 |
+
"submitted_answer": f"ERROR: {str(e)}"
|
| 59 |
+
})
|
| 60 |
+
|
| 61 |
+
with open("partial_answers.json", "w", encoding="utf-8") as f:
|
| 62 |
+
json.dump(answers, f, indent=2)
|
| 63 |
+
|
| 64 |
+
print("[INFO] Submitting answers to leaderboard...")
|
| 65 |
|
| 66 |
try:
|
| 67 |
res = requests.post(
|
|
|
|
| 72 |
"agent_code": CODE_LINK,
|
| 73 |
"answers": answers
|
| 74 |
},
|
| 75 |
+
timeout=60 # Set submission timeout
|
| 76 |
)
|
|
|
|
| 77 |
if res.ok:
|
| 78 |
+
print("[INFO] Submission successful")
|
| 79 |
return json.dumps(res.json(), indent=2)
|
| 80 |
else:
|
| 81 |
return f"Error submitting: {res.status_code} - {res.text}"
|
| 82 |
|
| 83 |
+
except requests.exceptions.Timeout:
|
| 84 |
+
return " Submission timed out. Try again later."
|
| 85 |
except Exception as e:
|
| 86 |
+
return f" Submission failed with exception: {e}"
|
| 87 |
+
|
| 88 |
# Gradio interface
|
| 89 |
gr.Interface(
|
| 90 |
fn=run_and_submit_all,
|