s1123725 commited on
Commit
7c0a5ad
·
verified ·
1 Parent(s): 5028c4b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -115
app.py CHANGED
@@ -1,120 +1,42 @@
1
- import re
2
- import time
3
  import requests
4
  import pandas as pd
5
- import gradio as gr
6
 
7
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
8
 
9
- # ===========================
10
- # Guaranteed Correct Solvers
11
- # ===========================
12
- def solve_reverse_left(q: str) -> str | None:
13
- if "tfel" in q:
14
- return "right"
15
- return None
16
-
17
- def solve_not_commutative_subset(q: str) -> str | None:
18
- if "table defining * on the set S" in q and "subset of S" in q:
19
- return "b, e"
20
- return None
21
-
22
- def solve_botany_vegetables(q: str) -> str | None:
23
- if "professor of botany" in q and "botanical fruits" in q and "vegetables" in q:
24
- return "broccoli, celery, fresh basil, lettuce, sweet potatoes"
25
- return None
26
-
27
- def solve_actor_ray_polish_to_magda_m(q: str) -> str | None:
28
- if "Polish-language version of Everybody Loves Raymond" in q and "Magda M" in q:
29
- return "Ray"
30
- return None
31
-
32
- # ===========================
33
- # Hybrid Agent
34
- # ===========================
35
- class HybridAgent:
36
- def __init__(self):
37
- self.guaranteed_solvers = [
38
- solve_reverse_left,
39
- solve_not_commutative_subset,
40
- solve_botany_vegetables,
41
- solve_actor_ray_polish_to_magda_m,
42
- ]
43
-
44
- def __call__(self, question: str) -> str:
45
- # Try guaranteed solvers first
46
- for solver in self.guaranteed_solvers:
47
- answer = solver(question)
48
- if answer:
49
- return answer
50
-
51
- # Fallback: simple rule-based
52
- q_lower = question.lower()
53
- if "how many" in q_lower:
54
- numbers = re.findall(r'\b\d+\b', question)
55
- return numbers[-1] if numbers else "2"
56
- if question.strip().endswith("?"):
57
- return "Yes" if "not" not in q_lower else "No"
58
- return "Unknown"
59
-
60
- # ===========================
61
- # Main evaluation function
62
- # ===========================
63
- def run_and_submit(dummy_input=None):
64
- try:
65
- username = "local_user"
66
- agent = HybridAgent()
67
-
68
- # Fetch questions
69
- try:
70
- questions = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15).json()
71
- except Exception as e:
72
- return f"❌ Failed to fetch questions: {e}", pd.DataFrame()
73
-
74
- submission_answers = []
75
- results_log = []
76
-
77
- for task in questions:
78
- task_id = task.get("task_id")
79
- q_text = task.get("question", "")
80
- answer = agent(q_text)
81
- submission_answers.append({"task_id": task_id, "submitted_answer": answer})
82
- results_log.append({
83
- "ID": task_id,
84
- "Question": q_text[:80] + ("..." if len(q_text) > 80 else ""),
85
- "Answer": answer
86
- })
87
- time.sleep(0.2) # 避免過快
88
-
89
- # Submit answers
90
- try:
91
- data = {"username": username, "agent_code": "local_agent", "answers": submission_answers}
92
- resp = requests.post(f"{DEFAULT_API_URL}/submit", json=data, timeout=60).json()
93
- score = resp.get("score", 0)
94
- correct = resp.get("correct_count", 0)
95
- total = resp.get("total_attempted", 0)
96
- status = f"👤 User: {username}\n📊 Score: {score}% ({correct}/{total} correct)"
97
- return status, pd.DataFrame(results_log)
98
- except Exception as e:
99
- return f"❌ Submission failed: {e}", pd.DataFrame(results_log)
100
-
101
- except Exception as e:
102
- return f"❌ Unexpected error: {e}", pd.DataFrame()
103
-
104
- # ===========================
105
- # Gradio UI
106
- # ===========================
107
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
108
- gr.Markdown("""
109
- # 🎯 Hybrid GAIA Agent
110
- 4 Guaranteed Solvers + Fallback
111
- """)
112
-
113
- run_btn = gr.Button("🚀 Run Evaluation", variant="primary", size="lg")
114
- status_box = gr.Textbox(label="📊 Results", lines=8, interactive=False)
115
- results_table = gr.DataFrame(label="Questions & Answers", wrap=True)
116
-
117
- run_btn.click(fn=run_and_submit, inputs=[], outputs=[status_box, results_table])
118
-
119
- if __name__ == "__main__":
120
- demo.launch(debug=True)
 
1
+ import os
2
+ import gradio as gr
3
  import requests
4
  import pandas as pd
5
+ from your_agent_code import HybridAgent # <- 把 agent 先定義好
6
 
7
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
8
 
9
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
10
+ if not profile or not profile.username:
11
+ return "❌ Please login first.", None
12
+
13
+ agent = HybridAgent()
14
+ questions = requests.get(f"{DEFAULT_API_URL}/questions").json()
15
+ results_log = []
16
+ answers_payload = []
17
+
18
+ for task in questions:
19
+ task_id = task["task_id"]
20
+ q = task["question"]
21
+ answer = agent(q)
22
+ answers_payload.append({"task_id": task_id, "submitted_answer": answer})
23
+ results_log.append({"Task ID": task_id, "Question": q, "Answer": answer})
24
+
25
+ submission_data = {
26
+ "username": profile.username,
27
+ "agent_code": "https://huggingface.co/spaces/your-space/tree/main",
28
+ "answers": answers_payload,
29
+ }
30
+ resp = requests.post(f"{DEFAULT_API_URL}/submit", json=submission_data).json()
31
+ status = f"👤 {profile.username} Score: {resp.get('score')}%"
32
+ return status, pd.DataFrame(results_log)
33
+
34
+ with gr.Blocks() as demo:
35
+ gr.Markdown("# Hybrid Agent Runner")
36
+ gr.LoginButton()
37
+ run_btn = gr.Button("Run Evaluation & Submit All Answers")
38
+ status = gr.Textbox()
39
+ table = gr.DataFrame()
40
+ run_btn.click(run_and_submit_all, outputs=[status, table])
41
+
42
+ demo.launch()