Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,13 +8,8 @@ import gradio as gr
|
|
| 8 |
# ===============================
|
| 9 |
|
| 10 |
BASE_URL = os.environ.get("GAIA_API_URL")
|
| 11 |
-
|
| 12 |
-
# Your username (auto-detected, fallback added)
|
| 13 |
HF_USERNAME = os.environ.get("SPACE_AUTHOR_NAME", "jatinror")
|
| 14 |
-
|
| 15 |
-
# Your Space ID hardcoded as fallback
|
| 16 |
SPACE_ID = os.environ.get("SPACE_ID", "jatinror/Final_Assignment_Template")
|
| 17 |
-
|
| 18 |
AGENT_CODE_URL = f"https://huggingface.co/spaces/{SPACE_ID}/tree/main"
|
| 19 |
|
| 20 |
print("Using SPACE_ID:", SPACE_ID)
|
|
@@ -38,7 +33,6 @@ def web_search(query, max_results=3):
|
|
| 38 |
def download_task_file(task_id):
|
| 39 |
url = f"{BASE_URL}/files/{task_id}"
|
| 40 |
response = requests.get(url)
|
| 41 |
-
|
| 42 |
if response.status_code == 200:
|
| 43 |
file_path = f"/tmp/{task_id}"
|
| 44 |
with open(file_path, "wb") as f:
|
|
@@ -47,14 +41,12 @@ def download_task_file(task_id):
|
|
| 47 |
return None
|
| 48 |
|
| 49 |
# ===============================
|
| 50 |
-
# BASIC REASONING
|
| 51 |
# ===============================
|
| 52 |
|
| 53 |
def solve_question(question, task_id):
|
| 54 |
file_path = download_task_file(task_id)
|
| 55 |
-
|
| 56 |
context = ""
|
| 57 |
-
|
| 58 |
if file_path and os.path.exists(file_path):
|
| 59 |
try:
|
| 60 |
with open(file_path, "r", errors="ignore") as f:
|
|
@@ -63,22 +55,17 @@ def solve_question(question, task_id):
|
|
| 63 |
context = ""
|
| 64 |
else:
|
| 65 |
context = web_search(question)
|
| 66 |
-
|
| 67 |
return extract_answer(context)
|
| 68 |
|
| 69 |
# ===============================
|
| 70 |
-
# ANSWER EXTRACTION
|
| 71 |
# ===============================
|
| 72 |
|
| 73 |
def extract_answer(text):
|
| 74 |
import re
|
| 75 |
-
|
| 76 |
-
# Try returning first number (many GAIA answers are numeric)
|
| 77 |
numbers = re.findall(r"\b\d+(?:\.\d+)?\b", text)
|
| 78 |
if numbers:
|
| 79 |
return numbers[0]
|
| 80 |
-
|
| 81 |
-
# Otherwise return short phrase
|
| 82 |
words = text.split()
|
| 83 |
return " ".join(words[:6]).strip()
|
| 84 |
|
|
@@ -101,7 +88,6 @@ def submit_answers(answers):
|
|
| 101 |
"agent_code": AGENT_CODE_URL,
|
| 102 |
"answers": answers
|
| 103 |
}
|
| 104 |
-
|
| 105 |
print("Submitting payload...")
|
| 106 |
response = requests.post(f"{BASE_URL}/submit", json=payload)
|
| 107 |
print("Server response:", response.text)
|
|
@@ -113,46 +99,41 @@ def submit_answers(answers):
|
|
| 113 |
def run_agent():
|
| 114 |
print("Fetching GAIA questions...")
|
| 115 |
questions = get_questions()
|
| 116 |
-
|
| 117 |
answers = []
|
| 118 |
-
|
| 119 |
for q in questions:
|
| 120 |
task_id = q["task_id"]
|
| 121 |
question = q["question"]
|
| 122 |
-
|
| 123 |
print("Solving:", task_id)
|
| 124 |
-
|
| 125 |
try:
|
| 126 |
result = solve_question(question, task_id)
|
| 127 |
except Exception as e:
|
| 128 |
print("Error:", e)
|
| 129 |
result = ""
|
| 130 |
-
|
| 131 |
answers.append({
|
| 132 |
"task_id": task_id,
|
| 133 |
"submitted_answer": result.strip()
|
| 134 |
})
|
| 135 |
-
|
| 136 |
submit_answers(answers)
|
| 137 |
print("Finished submission.")
|
| 138 |
|
| 139 |
# ===============================
|
| 140 |
-
# GRADIO
|
| 141 |
# ===============================
|
| 142 |
|
| 143 |
-
def run_pipeline():
|
| 144 |
try:
|
| 145 |
run_agent()
|
| 146 |
return "✅ GAIA submission completed. Check leaderboard."
|
| 147 |
except Exception as e:
|
| 148 |
return f"❌ Error occurred: {str(e)}"
|
| 149 |
|
|
|
|
| 150 |
demo = gr.Interface(
|
| 151 |
fn=run_pipeline,
|
| 152 |
-
inputs=
|
| 153 |
outputs="text",
|
| 154 |
title="GAIA Final Assignment Agent",
|
| 155 |
-
description="Click
|
| 156 |
)
|
| 157 |
|
| 158 |
if __name__ == "__main__":
|
|
|
|
| 8 |
# ===============================
|
| 9 |
|
| 10 |
BASE_URL = os.environ.get("GAIA_API_URL")
|
|
|
|
|
|
|
| 11 |
HF_USERNAME = os.environ.get("SPACE_AUTHOR_NAME", "jatinror")
|
|
|
|
|
|
|
| 12 |
SPACE_ID = os.environ.get("SPACE_ID", "jatinror/Final_Assignment_Template")
|
|
|
|
| 13 |
AGENT_CODE_URL = f"https://huggingface.co/spaces/{SPACE_ID}/tree/main"
|
| 14 |
|
| 15 |
print("Using SPACE_ID:", SPACE_ID)
|
|
|
|
| 33 |
def download_task_file(task_id):
|
| 34 |
url = f"{BASE_URL}/files/{task_id}"
|
| 35 |
response = requests.get(url)
|
|
|
|
| 36 |
if response.status_code == 200:
|
| 37 |
file_path = f"/tmp/{task_id}"
|
| 38 |
with open(file_path, "wb") as f:
|
|
|
|
| 41 |
return None
|
| 42 |
|
| 43 |
# ===============================
|
| 44 |
+
# BASIC REASONING
|
| 45 |
# ===============================
|
| 46 |
|
| 47 |
def solve_question(question, task_id):
|
| 48 |
file_path = download_task_file(task_id)
|
|
|
|
| 49 |
context = ""
|
|
|
|
| 50 |
if file_path and os.path.exists(file_path):
|
| 51 |
try:
|
| 52 |
with open(file_path, "r", errors="ignore") as f:
|
|
|
|
| 55 |
context = ""
|
| 56 |
else:
|
| 57 |
context = web_search(question)
|
|
|
|
| 58 |
return extract_answer(context)
|
| 59 |
|
| 60 |
# ===============================
|
| 61 |
+
# ANSWER EXTRACTION
|
| 62 |
# ===============================
|
| 63 |
|
| 64 |
def extract_answer(text):
|
| 65 |
import re
|
|
|
|
|
|
|
| 66 |
numbers = re.findall(r"\b\d+(?:\.\d+)?\b", text)
|
| 67 |
if numbers:
|
| 68 |
return numbers[0]
|
|
|
|
|
|
|
| 69 |
words = text.split()
|
| 70 |
return " ".join(words[:6]).strip()
|
| 71 |
|
|
|
|
| 88 |
"agent_code": AGENT_CODE_URL,
|
| 89 |
"answers": answers
|
| 90 |
}
|
|
|
|
| 91 |
print("Submitting payload...")
|
| 92 |
response = requests.post(f"{BASE_URL}/submit", json=payload)
|
| 93 |
print("Server response:", response.text)
|
|
|
|
| 99 |
def run_agent():
|
| 100 |
print("Fetching GAIA questions...")
|
| 101 |
questions = get_questions()
|
|
|
|
| 102 |
answers = []
|
|
|
|
| 103 |
for q in questions:
|
| 104 |
task_id = q["task_id"]
|
| 105 |
question = q["question"]
|
|
|
|
| 106 |
print("Solving:", task_id)
|
|
|
|
| 107 |
try:
|
| 108 |
result = solve_question(question, task_id)
|
| 109 |
except Exception as e:
|
| 110 |
print("Error:", e)
|
| 111 |
result = ""
|
|
|
|
| 112 |
answers.append({
|
| 113 |
"task_id": task_id,
|
| 114 |
"submitted_answer": result.strip()
|
| 115 |
})
|
|
|
|
| 116 |
submit_answers(answers)
|
| 117 |
print("Finished submission.")
|
| 118 |
|
| 119 |
# ===============================
|
| 120 |
+
# GRADIO INTERFACE (GUARANTEES RUN BUTTON)
|
| 121 |
# ===============================
|
| 122 |
|
| 123 |
+
def run_pipeline(dummy_button):
|
| 124 |
try:
|
| 125 |
run_agent()
|
| 126 |
return "✅ GAIA submission completed. Check leaderboard."
|
| 127 |
except Exception as e:
|
| 128 |
return f"❌ Error occurred: {str(e)}"
|
| 129 |
|
| 130 |
+
# Use a dummy button as input to force Run button to appear
|
| 131 |
demo = gr.Interface(
|
| 132 |
fn=run_pipeline,
|
| 133 |
+
inputs=gr.Button("Run GAIA Agent"), # <- this creates the Run button
|
| 134 |
outputs="text",
|
| 135 |
title="GAIA Final Assignment Agent",
|
| 136 |
+
description="Click the button to execute your GAIA agent and submit answers."
|
| 137 |
)
|
| 138 |
|
| 139 |
if __name__ == "__main__":
|