Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,33 @@
|
|
| 1 |
# ===========================
|
| 2 |
# app.py
|
| 3 |
# ===========================
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
import requests
|
| 7 |
import pandas as pd
|
| 8 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# ===========================
|
| 11 |
-
#
|
| 12 |
# ===========================
|
| 13 |
-
WIKI_API = "https://en.wikipedia.org/w/api.php"
|
| 14 |
-
UA = {"User-Agent": "GAIA-Agent/1.0"}
|
| 15 |
-
|
| 16 |
-
def fetch_wiki(title: str) -> str | None:
|
| 17 |
-
for attempt in range(3):
|
| 18 |
-
try:
|
| 19 |
-
params = {
|
| 20 |
-
"action": "parse",
|
| 21 |
-
"page": title,
|
| 22 |
-
"prop": "wikitext",
|
| 23 |
-
"format": "json",
|
| 24 |
-
"formatversion": 2,
|
| 25 |
-
"redirects": "1",
|
| 26 |
-
}
|
| 27 |
-
r = requests.get(WIKI_API, params=params, headers=UA, timeout=15)
|
| 28 |
-
r.raise_for_status()
|
| 29 |
-
return r.json()["parse"]["wikitext"]
|
| 30 |
-
except Exception:
|
| 31 |
-
time.sleep(0.5)
|
| 32 |
-
return None
|
| 33 |
-
|
| 34 |
def strip_refs(text: str) -> str:
|
| 35 |
text = re.sub(r"<ref[^>]*>.*?</ref>", "", text, flags=re.DOTALL)
|
| 36 |
text = re.sub(r"<ref[^/>]*/>", "", text)
|
| 37 |
return text
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
def solve_reverse_left(q: str) -> str | None:
|
| 43 |
if "tfel" in q:
|
| 44 |
return "right"
|
|
@@ -79,9 +68,6 @@ def solve_actor_ray_polish_to_magda_m(q: str) -> str | None:
|
|
| 79 |
return m.group(1).split()[0]
|
| 80 |
return None
|
| 81 |
|
| 82 |
-
# ===========================
|
| 83 |
-
# Hybrid Agent
|
| 84 |
-
# ===========================
|
| 85 |
class HybridAgent:
|
| 86 |
def __init__(self):
|
| 87 |
self.guaranteed_solvers = [
|
|
@@ -107,81 +93,72 @@ class HybridAgent:
|
|
| 107 |
|
| 108 |
if 'how many' in q_lower and numbers:
|
| 109 |
return numbers[-1]
|
| 110 |
-
|
| 111 |
if q.strip().endswith('?'):
|
| 112 |
starters = ['is', 'are', 'was', 'were', 'does', 'do', 'did']
|
| 113 |
if any(q_lower.startswith(w) for w in starters):
|
| 114 |
return "No" if any(neg in q_lower for neg in ["not","never","n't"]) else "Yes"
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
if years:
|
| 118 |
-
return years[-1]
|
| 119 |
-
|
| 120 |
-
if any(op in q for op in ['+', '-', '*', '/']):
|
| 121 |
-
try:
|
| 122 |
-
nums = [float(n) for n in numbers[:2]]
|
| 123 |
-
if '+' in q: return str(int(nums[0]+nums[1]))
|
| 124 |
-
if '-' in q: return str(int(nums[0]-nums[1]))
|
| 125 |
-
if '*' in q: return str(int(nums[0]*nums[1]))
|
| 126 |
-
if '/' in q: return str(nums[0]/nums[1])
|
| 127 |
-
except:
|
| 128 |
-
pass
|
| 129 |
-
|
| 130 |
return "Unknown"
|
| 131 |
|
| 132 |
# ===========================
|
| 133 |
-
#
|
| 134 |
# ===========================
|
| 135 |
-
def
|
| 136 |
-
if not
|
| 137 |
-
return "❌ Please login with your Hugging Face account.",
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
-
agent = HybridAgent()
|
| 140 |
try:
|
| 141 |
-
questions = requests.get("
|
| 142 |
except Exception as e:
|
| 143 |
-
return f"❌ Failed to fetch questions: {e}",
|
| 144 |
|
| 145 |
-
|
|
|
|
| 146 |
results_log = []
|
| 147 |
|
| 148 |
-
for
|
| 149 |
task_id = task.get("task_id")
|
| 150 |
-
q_text = task.get("question"
|
| 151 |
if not task_id or not q_text:
|
| 152 |
continue
|
| 153 |
answer = agent(q_text)
|
| 154 |
-
|
| 155 |
-
results_log.append({"ID": task_id, "Question": q_text[:100]+"...", "Answer": answer})
|
| 156 |
-
time.sleep(0.
|
| 157 |
-
|
| 158 |
-
# 模擬計算分數
|
| 159 |
-
correct = sum(1 for ans in submission_answers if ans['submitted_answer'] != "Unknown")
|
| 160 |
-
total = len(submission_answers)
|
| 161 |
-
score = int(correct/total*100) if total>0 else 0
|
| 162 |
-
|
| 163 |
-
code_link = "https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE/tree/main"
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
|
|
|
|
|
|
|
|
|
| 175 |
with gr.Blocks() as demo:
|
| 176 |
-
gr.Markdown("
|
| 177 |
-
|
|
|
|
| 178 |
login_btn = gr.LoginButton()
|
| 179 |
run_btn = gr.Button("🚀 Run Evaluation & Submit All Answers")
|
| 180 |
-
status_box = gr.Textbox(label="
|
| 181 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 182 |
|
| 183 |
-
|
| 184 |
-
run_btn.click(fn=run_and_submit, inputs=username_state, outputs=[status_box, results_table])
|
| 185 |
|
| 186 |
-
if __name__=="__main__":
|
| 187 |
demo.launch()
|
|
|
|
| 1 |
# ===========================
|
| 2 |
# app.py
|
| 3 |
# ===========================
|
| 4 |
+
import os
|
| 5 |
+
import gradio as gr
|
| 6 |
import requests
|
| 7 |
import pandas as pd
|
| 8 |
+
import re
|
| 9 |
+
import time
|
| 10 |
+
|
| 11 |
+
# --- Constants ---
|
| 12 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 13 |
|
| 14 |
# ===========================
|
| 15 |
+
# GAIA Hybrid Agent
|
| 16 |
# ===========================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def strip_refs(text: str) -> str:
|
| 18 |
text = re.sub(r"<ref[^>]*>.*?</ref>", "", text, flags=re.DOTALL)
|
| 19 |
text = re.sub(r"<ref[^/>]*/>", "", text)
|
| 20 |
return text
|
| 21 |
|
| 22 |
+
def fetch_wiki(title: str) -> str | None:
|
| 23 |
+
try:
|
| 24 |
+
params = {"action": "parse", "page": title, "prop": "wikitext", "format": "json", "formatversion": 2}
|
| 25 |
+
r = requests.get("https://en.wikipedia.org/w/api.php", params=params, timeout=10)
|
| 26 |
+
r.raise_for_status()
|
| 27 |
+
return r.json()["parse"]["wikitext"]
|
| 28 |
+
except:
|
| 29 |
+
return None
|
| 30 |
+
|
| 31 |
def solve_reverse_left(q: str) -> str | None:
|
| 32 |
if "tfel" in q:
|
| 33 |
return "right"
|
|
|
|
| 68 |
return m.group(1).split()[0]
|
| 69 |
return None
|
| 70 |
|
|
|
|
|
|
|
|
|
|
| 71 |
class HybridAgent:
|
| 72 |
def __init__(self):
|
| 73 |
self.guaranteed_solvers = [
|
|
|
|
| 93 |
|
| 94 |
if 'how many' in q_lower and numbers:
|
| 95 |
return numbers[-1]
|
|
|
|
| 96 |
if q.strip().endswith('?'):
|
| 97 |
starters = ['is', 'are', 'was', 'were', 'does', 'do', 'did']
|
| 98 |
if any(q_lower.startswith(w) for w in starters):
|
| 99 |
return "No" if any(neg in q_lower for neg in ["not","never","n't"]) else "Yes"
|
| 100 |
+
if numbers:
|
| 101 |
+
return numbers[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
return "Unknown"
|
| 103 |
|
| 104 |
# ===========================
|
| 105 |
+
# Main Submission Function
|
| 106 |
# ===========================
|
| 107 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 108 |
+
if not profile:
|
| 109 |
+
return "❌ Please login with your Hugging Face account.", None
|
| 110 |
+
|
| 111 |
+
username = profile.username
|
| 112 |
+
agent_code = f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main"
|
| 113 |
|
|
|
|
| 114 |
try:
|
| 115 |
+
questions = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15).json()
|
| 116 |
except Exception as e:
|
| 117 |
+
return f"❌ Failed to fetch questions: {e}", None
|
| 118 |
|
| 119 |
+
agent = HybridAgent()
|
| 120 |
+
answers_payload = []
|
| 121 |
results_log = []
|
| 122 |
|
| 123 |
+
for task in questions:
|
| 124 |
task_id = task.get("task_id")
|
| 125 |
+
q_text = task.get("question")
|
| 126 |
if not task_id or not q_text:
|
| 127 |
continue
|
| 128 |
answer = agent(q_text)
|
| 129 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": answer})
|
| 130 |
+
results_log.append({"Task ID": task_id, "Question": q_text[:100]+"...", "Answer": answer})
|
| 131 |
+
time.sleep(0.05)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
+
submission_data = {"username": username, "agent_code": agent_code, "answers": answers_payload}
|
| 134 |
+
try:
|
| 135 |
+
response = requests.post(f"{DEFAULT_API_URL}/submit", json=submission_data, timeout=60)
|
| 136 |
+
response.raise_for_status()
|
| 137 |
+
result_data = response.json()
|
| 138 |
+
final_status = (
|
| 139 |
+
f"Submission Successful!\n"
|
| 140 |
+
f"User: {result_data.get('username')}\n"
|
| 141 |
+
f"Score: {result_data.get('score', 'N/A')}% "
|
| 142 |
+
f"({result_data.get('correct_count','?')}/{result_data.get('total_attempted','?')} correct)\n"
|
| 143 |
+
f"Message: {result_data.get('message','No message')}"
|
| 144 |
+
)
|
| 145 |
+
return final_status, pd.DataFrame(results_log)
|
| 146 |
+
except Exception as e:
|
| 147 |
+
return f"❌ Submission Failed: {e}", pd.DataFrame(results_log)
|
| 148 |
|
| 149 |
+
# ===========================
|
| 150 |
+
# Gradio Interface
|
| 151 |
+
# ===========================
|
| 152 |
with gr.Blocks() as demo:
|
| 153 |
+
gr.Markdown("# 🎯 GAIA Hybrid Agent")
|
| 154 |
+
gr.Markdown("4 Guaranteed Solvers + Fallback\n\nPlease login and submit your answers.")
|
| 155 |
+
|
| 156 |
login_btn = gr.LoginButton()
|
| 157 |
run_btn = gr.Button("🚀 Run Evaluation & Submit All Answers")
|
| 158 |
+
status_box = gr.Textbox(label="Run Status / Submission Result", lines=10)
|
| 159 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 160 |
|
| 161 |
+
run_btn.click(run_and_submit_all, inputs=login_btn, outputs=[status_box, results_table])
|
|
|
|
| 162 |
|
| 163 |
+
if __name__ == "__main__":
|
| 164 |
demo.launch()
|