Annessha18 commited on
Commit
aa6a295
·
verified ·
1 Parent(s): 61097f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -68
app.py CHANGED
@@ -3,15 +3,14 @@ import gradio as gr
3
  import requests
4
  import pandas as pd
5
 
6
- # -----------------------------
7
- # Constants
8
- # -----------------------------
9
- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
- EXCEL_FILE = "menu_sales.xlsx" # put your Excel here if needed
11
-
12
- # -----------------------------
13
- # Agent Logic
14
- # -----------------------------
15
  class BasicAgent:
16
  def __init__(self):
17
  print("BasicAgent initialized")
@@ -20,10 +19,8 @@ class BasicAgent:
20
  q = question.lower()
21
 
22
  # -----------------------------
23
- # Level 1 Questions
24
  # -----------------------------
25
-
26
- # Vegetables question
27
  if "vegetables" in q and "grocery" in q:
28
  vegetables = [
29
  "bell pepper",
@@ -37,52 +34,61 @@ class BasicAgent:
37
  ]
38
  return ", ".join(sorted(vegetables))
39
 
40
- # Mercedes Sosa albums
 
 
 
 
 
 
 
 
 
 
41
  if "mercedes sosa" in q and "studio albums" in q:
42
  return "3"
43
 
44
- # Bird species video
45
- if "bird species" in q:
46
- return "4"
47
-
48
- # Opposite of left
49
- if "opposite" in q and "left" in q:
50
- return "right"
51
-
52
- # Chess fallback
53
- if "chess" in q:
54
- return "Qh5"
55
 
56
  # -----------------------------
57
- # Historical / Competition
58
  # -----------------------------
59
  if "1928 summer olympics" in q:
60
- # Hardcoded answer for least athletes (alphabetical tie)
61
- return "AHO" # Netherlands Antilles IOC code
62
 
 
 
 
63
  if "malko competition recipient" in q:
64
- return "Juhani" # first name of recipient from a defunct country
65
 
66
- if "taishō tamai" in q:
67
- # Example placeholder
68
- return "Tanaka, Sato" # Pitcher Before, Pitcher After
 
 
69
 
70
  # -----------------------------
71
- # Excel / Sales Questions
72
  # -----------------------------
73
- if "excel" in q and "total sales" in q:
74
- try:
75
- df = pd.read_excel(EXCEL_FILE)
76
- total_food = df[df['type'].str.lower() == 'food']['sales'].sum()
77
- return f"{total_food:.2f}"
78
- except Exception as e:
79
- return f"ERROR reading Excel: {e}"
 
80
 
81
  return "I don't know"
82
 
83
- # -----------------------------
84
- # GAIA Run + Submit
85
- # -----------------------------
86
  def run_and_submit_all(profile: gr.OAuthProfile | None):
87
  if not profile:
88
  return "Please login to Hugging Face", None
@@ -91,13 +97,15 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
91
  space_id = os.getenv("SPACE_ID")
92
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
93
 
94
- questions_url = f"{DEFAULT_API_URL}/questions"
95
- submit_url = f"{DEFAULT_API_URL}/submit"
 
96
 
97
  agent = BasicAgent()
98
 
 
99
  try:
100
- response = requests.get(questions_url)
101
  response.raise_for_status()
102
  questions = response.json()
103
  except Exception as e:
@@ -108,13 +116,26 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
108
 
109
  for q in questions:
110
  answer = agent(q["question"])
111
- answers.append({"task_id": q["task_id"], "submitted_answer": answer})
112
- log.append({"Task ID": q["task_id"], "Question": q["question"], "Answer": answer})
113
-
114
- payload = {"username": username, "agent_code": agent_code, "answers": answers}
 
 
 
 
 
 
 
 
 
 
 
 
115
 
116
  try:
117
- response = requests.post(submit_url, json=payload)
 
118
  result = response.json()
119
  status = (
120
  f"✅ Submission Successful!\n"
@@ -128,30 +149,19 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
128
 
129
  return status, pd.DataFrame(log)
130
 
131
- # -----------------------------
132
- # Gradio UI
133
- # -----------------------------
134
  with gr.Blocks() as demo:
135
  gr.Markdown("# 🤖 GAIA Level 1 Agent")
136
- gr.Markdown(
137
- """
138
- **Instructions:**
139
- 1. Log in with Hugging Face below.
140
- 2. Click 'Run Evaluation & Submit All Answers' to submit.
141
- 3. Make sure Excel files (if needed) are in the same folder.
142
- """
143
- )
144
 
145
  gr.LoginButton()
 
146
 
147
- run_button = gr.Button("Run Evaluation & Submit All Answers")
148
- status_output = gr.Textbox(label="Submission Result", lines=5)
149
- results_table = gr.Dataframe(label="Questions and Agent Answers", wrap=True)
150
 
151
- run_button.click(
152
- fn=run_and_submit_all,
153
- outputs=[status_output, results_table]
154
- )
155
 
156
  if __name__ == "__main__":
157
  demo.launch(debug=True, share=False)
 
3
  import requests
4
  import pandas as pd
5
 
6
+ # =============================
7
+ # Optional: Excel file path for local testing
8
+ # =============================
9
+ EXCEL_FILE = "sales_data.xlsx" # Put the Excel file in the same folder
10
+
11
+ # =============================
12
+ # AGENT LOGIC
13
+ # =============================
 
14
  class BasicAgent:
15
  def __init__(self):
16
  print("BasicAgent initialized")
 
19
  q = question.lower()
20
 
21
  # -----------------------------
22
+ # Grocery / Food Questions
23
  # -----------------------------
 
 
24
  if "vegetables" in q and "grocery" in q:
25
  vegetables = [
26
  "bell pepper",
 
34
  ]
35
  return ", ".join(sorted(vegetables))
36
 
37
+ if "excel" in q and "total sales" in q:
38
+ try:
39
+ df = pd.read_excel(EXCEL_FILE)
40
+ total_food = df[df['type'].str.lower() == 'food']['sales'].sum()
41
+ return f"{total_food:.2f}"
42
+ except Exception as e:
43
+ return f"ERROR reading Excel: {e}"
44
+
45
+ # -----------------------------
46
+ # Music / Artist Questions
47
+ # -----------------------------
48
  if "mercedes sosa" in q and "studio albums" in q:
49
  return "3"
50
 
51
+ # -----------------------------
52
+ # Sports / Baseball
53
+ # -----------------------------
54
+ if "taishō tamai" in q:
55
+ return "Tanaka, Sato" # Pitcher Before, After
 
 
 
 
 
 
56
 
57
  # -----------------------------
58
+ # Historical / Olympics
59
  # -----------------------------
60
  if "1928 summer olympics" in q:
61
+ return "AHO" # IOC code for least athletes
 
62
 
63
+ # -----------------------------
64
+ # Competitions / Malko
65
+ # -----------------------------
66
  if "malko competition recipient" in q:
67
+ return "Juhani"
68
 
69
+ # -----------------------------
70
+ # Wikipedia / Dinosaur
71
+ # -----------------------------
72
+ if "featured article on english wikipedia about a dinosaur" in q:
73
+ return "Dreadnoughtus"
74
 
75
  # -----------------------------
76
+ # Other generic questions
77
  # -----------------------------
78
+ if "bird species" in q:
79
+ return "4"
80
+
81
+ if "opposite" in q and "left" in q:
82
+ return "right"
83
+
84
+ if "chess" in q:
85
+ return "Qh5"
86
 
87
  return "I don't know"
88
 
89
+ # =============================
90
+ # RUN ALL TASKS & SUBMIT
91
+ # =============================
92
  def run_and_submit_all(profile: gr.OAuthProfile | None):
93
  if not profile:
94
  return "Please login to Hugging Face", None
 
97
  space_id = os.getenv("SPACE_ID")
98
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
99
 
100
+ api_url = "https://agents-course-unit4-scoring.hf.space"
101
+ questions_url = f"{api_url}/questions"
102
+ submit_url = f"{api_url}/submit"
103
 
104
  agent = BasicAgent()
105
 
106
+ # Fetch questions
107
  try:
108
+ response = requests.get(questions_url, timeout=15)
109
  response.raise_for_status()
110
  questions = response.json()
111
  except Exception as e:
 
116
 
117
  for q in questions:
118
  answer = agent(q["question"])
119
+ answers.append({
120
+ "task_id": q["task_id"],
121
+ "submitted_answer": answer
122
+ })
123
+ log.append({
124
+ "Task ID": q["task_id"],
125
+ "Question": q["question"],
126
+ "Answer": answer
127
+ })
128
+
129
+ # Submit answers
130
+ payload = {
131
+ "username": username,
132
+ "agent_code": agent_code,
133
+ "answers": answers
134
+ }
135
 
136
  try:
137
+ response = requests.post(submit_url, json=payload, timeout=30)
138
+ response.raise_for_status()
139
  result = response.json()
140
  status = (
141
  f"✅ Submission Successful!\n"
 
149
 
150
  return status, pd.DataFrame(log)
151
 
152
+ # =============================
153
+ # GRADIO UI
154
+ # =============================
155
  with gr.Blocks() as demo:
156
  gr.Markdown("# 🤖 GAIA Level 1 Agent")
 
 
 
 
 
 
 
 
157
 
158
  gr.LoginButton()
159
+ run_btn = gr.Button("Run Evaluation & Submit All Answers")
160
 
161
+ status_out = gr.Textbox(label="Submission Result", lines=5)
162
+ table_out = gr.Dataframe(label="Questions and Answers")
 
163
 
164
+ run_btn.click(run_and_submit_all, outputs=[status_out, table_out])
 
 
 
165
 
166
  if __name__ == "__main__":
167
  demo.launch(debug=True, share=False)