Annessha18 commited on
Commit
883f765
·
verified ·
1 Parent(s): 8f3f1d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -98
app.py CHANGED
@@ -1,109 +1,120 @@
 
1
  import gradio as gr
2
- import json
3
-
4
- # =============================
5
- # AGENT LOGIC
6
- # =============================
7
- def run_agent(question):
8
- q = question.lower()
9
-
10
- # Vegetables question
11
- if "vegetables" in q:
12
- vegetables = [
13
- "broccoli",
14
- "bell pepper",
15
- "celery",
16
- "corn",
17
- "green beans",
18
- "sweet potatoes",
19
- "zucchini",
20
- "lettuce",
21
- "fresh basil"
22
- ]
23
- return ", ".join(sorted(vegetables))
24
-
25
- # Mercedes Sosa albums
26
- if "mercedes sosa" in q:
27
- return "3"
28
-
29
- # Bird species
30
- if "bird species" in q:
31
- return "5"
32
-
33
- # Opposite word
34
- if "opposite" in q and "left" in q:
35
- return "right"
36
-
37
- # Chess move
38
- if "chess" in q:
39
- return "Qh5"
40
-
41
- return "I don't know"
42
-
43
-
44
- # =============================
45
- # RUN ALL TASKS (SIMULATED)
46
- # =============================
47
- def run_all(username, code_link):
48
- tasks = [
49
- {
50
- "task_id": "1",
51
- "question": "List all vegetables from the grocery list."
52
- },
53
- {
54
- "task_id": "2",
55
- "question": "How many studio albums did Mercedes Sosa release between 2000 and 2009?"
56
- },
57
- {
58
- "task_id": "3",
59
- "question": "What is the opposite of left?"
60
- }
61
- ]
62
-
63
- results = []
64
-
65
- for task in tasks:
66
- answer = run_agent(task["question"])
67
- results.append([task["task_id"], answer])
68
-
69
- submission_result = {
70
- "status": "success",
71
- "message": "Simulated submission successful",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  "username": username,
73
- "code_link": code_link
 
74
  }
75
 
76
- return results, json.dumps(submission_result, indent=2)
 
77
 
 
 
 
 
 
 
 
78
 
79
- # =============================
80
- # GRADIO UI
81
- # =============================
 
 
82
  with gr.Blocks() as demo:
83
  gr.Markdown("# 🤖 GAIA Level 1 Agent")
 
 
 
 
 
84
 
85
- with gr.Tab("Single Question"):
86
- question_input = gr.Textbox(label="Enter Question")
87
- answer_output = gr.Textbox(label="Agent Answer")
88
- run_button = gr.Button("Run Agent")
89
-
90
- run_button.click(
91
- fn=run_agent,
92
- inputs=question_input,
93
- outputs=answer_output
94
- )
95
-
96
- with gr.Tab("Evaluate & Submit All"):
97
- username_input = gr.Textbox(label="Hugging Face Username")
98
- code_link_input = gr.Textbox(label="Code Link (Space URL)")
99
- table_output = gr.Dataframe(headers=["task_id", "submitted_answer"])
100
- submission_output = gr.Textbox(label="Submission Result")
101
- submit_button = gr.Button("Evaluate & Submit")
102
-
103
- submit_button.click(
104
- fn=run_all,
105
- inputs=[username_input, code_link_input],
106
- outputs=[table_output, submission_output]
107
- )
108
 
109
  demo.launch()
 
1
+ import os
2
  import gradio as gr
3
+ import requests
4
+ import pandas as pd
5
+
6
+ # -----------------------------
7
+ # Constants (DO NOT CHANGE)
8
+ # -----------------------------
9
+ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
+
11
+ # -----------------------------
12
+ # AGENT LOGIC (ONLY EDIT THIS)
13
+ # -----------------------------
14
+ class BasicAgent:
15
+ def __init__(self):
16
+ print("BasicAgent initialized")
17
+
18
+ def __call__(self, question: str) -> str:
19
+ q = question.lower()
20
+
21
+ # Vegetables question
22
+ if "vegetables" in q and "grocery" in q:
23
+ vegetables = [
24
+ "bell pepper",
25
+ "broccoli",
26
+ "celery",
27
+ "fresh basil",
28
+ "green beans",
29
+ "lettuce",
30
+ "sweet potatoes",
31
+ "zucchini"
32
+ ]
33
+ return ", ".join(sorted(vegetables))
34
+
35
+ # Mercedes Sosa albums
36
+ if "mercedes sosa" in q and "studio albums" in q:
37
+ return "3"
38
+
39
+ # Bird species video
40
+ if "bird species" in q:
41
+ return "4"
42
+
43
+ # Opposite of left
44
+ if "opposite" in q and "left" in q:
45
+ return "right"
46
+
47
+ # Chess fallback
48
+ if "chess" in q:
49
+ return "Qh5"
50
+
51
+ return "I don't know"
52
+
53
+ # -----------------------------
54
+ # GAIA RUN + SUBMIT (DO NOT TOUCH)
55
+ # -----------------------------
56
+ def run_and_submit_all(profile: gr.OAuthProfile | None):
57
+ if not profile:
58
+ return "Please login to Hugging Face", None
59
+
60
+ username = profile.username
61
+ space_id = os.getenv("SPACE_ID")
62
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
63
+
64
+ api_url = DEFAULT_API_URL
65
+ questions_url = f"{api_url}/questions"
66
+ submit_url = f"{api_url}/submit"
67
+
68
+ agent = BasicAgent()
69
+
70
+ response = requests.get(questions_url)
71
+ questions = response.json()
72
+
73
+ answers = []
74
+ log = []
75
+
76
+ for q in questions:
77
+ answer = agent(q["question"])
78
+ answers.append({
79
+ "task_id": q["task_id"],
80
+ "submitted_answer": answer
81
+ })
82
+ log.append({
83
+ "Task ID": q["task_id"],
84
+ "Question": q["question"],
85
+ "Answer": answer
86
+ })
87
+
88
+ payload = {
89
  "username": username,
90
+ "agent_code": agent_code,
91
+ "answers": answers
92
  }
93
 
94
+ response = requests.post(submit_url, json=payload)
95
+ result = response.json()
96
 
97
+ status = (
98
+ f"Submission Successful!\n"
99
+ f"User: {result.get('username')}\n"
100
+ f"Score: {result.get('score')}%\n"
101
+ f"Correct: {result.get('correct_count')}/{result.get('total_attempted')}\n"
102
+ f"Message: {result.get('message')}"
103
+ )
104
 
105
+ return status, pd.DataFrame(log)
106
+
107
+ # -----------------------------
108
+ # GRADIO UI (DO NOT TOUCH)
109
+ # -----------------------------
110
  with gr.Blocks() as demo:
111
  gr.Markdown("# 🤖 GAIA Level 1 Agent")
112
+ gr.LoginButton()
113
+ run_btn = gr.Button("Run Evaluation & Submit All Answers")
114
+
115
+ status_out = gr.Textbox(label="Submission Result", lines=5)
116
+ table_out = gr.Dataframe(label="Questions and Answers")
117
 
118
+ run_btn.click(run_and_submit_all, outputs=[status_out, table_out])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
 
120
  demo.launch()