gsgoncalves commited on
Commit
96d2264
·
verified ·
1 Parent(s): b0c0469

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -31
app.py CHANGED
@@ -3,23 +3,24 @@ import logging
3
  import gradio as gr
4
  import pandas as pd
5
  from api import GAIAHFAPIClient
6
- from agents.agent import BasicAgent, SimpleGeminiAgent
7
  import random
8
 
9
  logging.basicConfig(
10
  level=logging.INFO,
11
  format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
12
- datefmt="%Y-%m-%d %H:%M:%S"
13
  )
14
  logger = logging.getLogger(__name__)
15
 
 
16
  def run_and_submit_all(profile: gr.OAuthProfile | None):
17
  """
18
  Fetches all questions, runs the BasicAgent on them, submits all answers,
19
  and displays the results.
20
  """
21
  api_client = GAIAHFAPIClient(profile=profile) # Initialize the API client
22
- agent = SimpleGeminiAgent()
23
  # 2. Fetch Questions
24
  questions_data, error = api_client.get_questions()
25
  if error is None or questions_data is None:
@@ -37,25 +38,48 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
37
  continue
38
  try:
39
  submitted_answer = agent(question_text)
40
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
41
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
 
 
 
 
 
 
 
 
42
  except Exception as e:
43
  logger.error(f"Error running agent on task {task_id}: {e}")
44
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
 
 
 
 
 
45
 
46
  if not answers_payload:
47
  logger.warning("Agent did not produce any answers to submit.")
48
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
49
 
50
- # 4. Prepare Submission
51
- submission_data = {"username": api_client.username.strip(), "agent_code": api_client.agent_code, "answers": answers_payload}
52
- logger.info(f"Agent finished. Submitting {len(answers_payload)} answers for user '{api_client.username}'...")
 
 
 
 
 
 
53
 
54
  # 5. Submit
55
- logger.info(f"Submitting {len(answers_payload)} answers to: {api_client.submit_url}")
 
 
56
  status_message, results_df = api_client.submit_answers(submission_data, results_log)
57
  return status_message, results_df
58
 
 
59
  def run_and_submit_one(profile: gr.OAuthProfile | None):
60
  """
61
  Fetches all questions, runs the BasicAgent on them, submits all answers,
@@ -72,32 +96,57 @@ def run_and_submit_one(profile: gr.OAuthProfile | None):
72
  # 3. Run your Agent
73
  results_log = []
74
  answers_payload = []
75
-
76
  task_id = question_data.get("task_id")
77
  question_text = question_data.get("question")
78
  if not task_id or question_text is None:
79
- logger.warning(f"Skipping item with missing task_id or question: {question_data}")
 
 
80
  try:
81
  submitted_answer = agent(question_text)
82
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
83
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
 
 
 
 
 
 
 
 
84
  except Exception as e:
85
  logger.error(f"Error running agent on task {task_id}: {e}")
86
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
 
 
 
 
 
87
 
88
  if not answers_payload:
89
  logger.warning("Agent did not produce any answers to submit.")
90
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
91
 
92
- # 4. Prepare Submission
93
- submission_data = {"username": api_client.username.strip(), "agent_code": api_client.agent_code, "answers": answers_payload}
94
- logger.info(f"Agent finished. Submitting {len(answers_payload)} answers for user '{api_client.username}'...")
 
 
 
 
 
 
95
 
96
  # 5. Submit
97
- logger.info(f"Submitting {len(answers_payload)} answers to: {api_client.submit_url}")
 
 
98
  status_message, results_df = api_client.submit_answers(submission_data, results_log)
99
  return status_message, results_df
100
 
 
101
  def build_gradio_interface():
102
  # --- Build Gradio Interface using Blocks ---
103
  with gr.Blocks() as demo:
@@ -122,25 +171,26 @@ def build_gradio_interface():
122
  run_all_button = gr.Button("Run Evaluation & Submit All Answers")
123
  run_one_button = gr.Button("Run a Single Evaluation")
124
 
125
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
 
 
126
  # Removed max_rows=10 from DataFrame constructor
127
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
128
 
129
  run_all_button.click(
130
- fn=run_and_submit_all,
131
- outputs=[status_output, results_table]
132
  )
133
  run_one_button.click(
134
- fn=run_and_submit_one,
135
- outputs=[status_output, results_table]
136
  )
137
  return demo
138
 
 
139
  if __name__ == "__main__":
140
- print("\n" + "-"*30 + " App Starting " + "-"*30)
141
  # Check for SPACE_HOST and SPACE_ID at startup for information
142
  space_host_startup = os.getenv("SPACE_HOST")
143
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
144
 
145
  if space_host_startup:
146
  print(f"✅ SPACE_HOST found: {space_host_startup}")
@@ -148,15 +198,19 @@ if __name__ == "__main__":
148
  else:
149
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
150
 
151
- if space_id_startup: # Print repo URLs if SPACE_ID is found
152
  print(f"✅ SPACE_ID found: {space_id_startup}")
153
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
154
- print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
 
 
155
  else:
156
- print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
 
 
157
 
158
- print("-"*(60 + len(" App Starting ")) + "\n")
159
 
160
  print("Launching Gradio Interface for Basic Agent Evaluation...")
161
  demo = build_gradio_interface()
162
- demo.launch(debug=True, share=False)
 
3
  import gradio as gr
4
  import pandas as pd
5
  from api import GAIAHFAPIClient
6
+ from agents.agent import BasicAgent, DeepResearchGeminiAgent, SimpleGeminiAgent
7
  import random
8
 
9
  logging.basicConfig(
10
  level=logging.INFO,
11
  format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
12
+ datefmt="%Y-%m-%d %H:%M:%S",
13
  )
14
  logger = logging.getLogger(__name__)
15
 
16
+
17
  def run_and_submit_all(profile: gr.OAuthProfile | None):
18
  """
19
  Fetches all questions, runs the BasicAgent on them, submits all answers,
20
  and displays the results.
21
  """
22
  api_client = GAIAHFAPIClient(profile=profile) # Initialize the API client
23
+ agent = DeepResearchGeminiAgent()
24
  # 2. Fetch Questions
25
  questions_data, error = api_client.get_questions()
26
  if error is None or questions_data is None:
 
38
  continue
39
  try:
40
  submitted_answer = agent(question_text)
41
+ answers_payload.append(
42
+ {"task_id": task_id, "submitted_answer": submitted_answer}
43
+ )
44
+ results_log.append(
45
+ {
46
+ "Task ID": task_id,
47
+ "Question": question_text,
48
+ "Submitted Answer": submitted_answer,
49
+ }
50
+ )
51
  except Exception as e:
52
  logger.error(f"Error running agent on task {task_id}: {e}")
53
+ results_log.append(
54
+ {
55
+ "Task ID": task_id,
56
+ "Question": question_text,
57
+ "Submitted Answer": f"AGENT ERROR: {e}",
58
+ }
59
+ )
60
 
61
  if not answers_payload:
62
  logger.warning("Agent did not produce any answers to submit.")
63
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
64
 
65
+ # 4. Prepare Submission
66
+ submission_data = {
67
+ "username": api_client.username.strip(),
68
+ "agent_code": api_client.agent_code,
69
+ "answers": answers_payload,
70
+ }
71
+ logger.info(
72
+ f"Agent finished. Submitting {len(answers_payload)} answers for user '{api_client.username}'..."
73
+ )
74
 
75
  # 5. Submit
76
+ logger.info(
77
+ f"Submitting {len(answers_payload)} answers to: {api_client.submit_url}"
78
+ )
79
  status_message, results_df = api_client.submit_answers(submission_data, results_log)
80
  return status_message, results_df
81
 
82
+
83
  def run_and_submit_one(profile: gr.OAuthProfile | None):
84
  """
85
  Fetches all questions, runs the BasicAgent on them, submits all answers,
 
96
  # 3. Run your Agent
97
  results_log = []
98
  answers_payload = []
99
+
100
  task_id = question_data.get("task_id")
101
  question_text = question_data.get("question")
102
  if not task_id or question_text is None:
103
+ logger.warning(
104
+ f"Skipping item with missing task_id or question: {question_data}"
105
+ )
106
  try:
107
  submitted_answer = agent(question_text)
108
+ answers_payload.append(
109
+ {"task_id": task_id, "submitted_answer": submitted_answer}
110
+ )
111
+ results_log.append(
112
+ {
113
+ "Task ID": task_id,
114
+ "Question": question_text,
115
+ "Submitted Answer": submitted_answer,
116
+ }
117
+ )
118
  except Exception as e:
119
  logger.error(f"Error running agent on task {task_id}: {e}")
120
+ results_log.append(
121
+ {
122
+ "Task ID": task_id,
123
+ "Question": question_text,
124
+ "Submitted Answer": f"AGENT ERROR: {e}",
125
+ }
126
+ )
127
 
128
  if not answers_payload:
129
  logger.warning("Agent did not produce any answers to submit.")
130
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
131
 
132
+ # 4. Prepare Submission
133
+ submission_data = {
134
+ "username": api_client.username.strip(),
135
+ "agent_code": api_client.agent_code,
136
+ "answers": answers_payload,
137
+ }
138
+ logger.info(
139
+ f"Agent finished. Submitting {len(answers_payload)} answers for user '{api_client.username}'..."
140
+ )
141
 
142
  # 5. Submit
143
+ logger.info(
144
+ f"Submitting {len(answers_payload)} answers to: {api_client.submit_url}"
145
+ )
146
  status_message, results_df = api_client.submit_answers(submission_data, results_log)
147
  return status_message, results_df
148
 
149
+
150
  def build_gradio_interface():
151
  # --- Build Gradio Interface using Blocks ---
152
  with gr.Blocks() as demo:
 
171
  run_all_button = gr.Button("Run Evaluation & Submit All Answers")
172
  run_one_button = gr.Button("Run a Single Evaluation")
173
 
174
+ status_output = gr.Textbox(
175
+ label="Run Status / Submission Result", lines=5, interactive=False
176
+ )
177
  # Removed max_rows=10 from DataFrame constructor
178
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
179
 
180
  run_all_button.click(
181
+ fn=run_and_submit_all, outputs=[status_output, results_table]
 
182
  )
183
  run_one_button.click(
184
+ fn=run_and_submit_one, outputs=[status_output, results_table]
 
185
  )
186
  return demo
187
 
188
+
189
  if __name__ == "__main__":
190
+ print("\n" + "-" * 30 + " App Starting " + "-" * 30)
191
  # Check for SPACE_HOST and SPACE_ID at startup for information
192
  space_host_startup = os.getenv("SPACE_HOST")
193
+ space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
194
 
195
  if space_host_startup:
196
  print(f"✅ SPACE_HOST found: {space_host_startup}")
 
198
  else:
199
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
200
 
201
+ if space_id_startup: # Print repo URLs if SPACE_ID is found
202
  print(f"✅ SPACE_ID found: {space_id_startup}")
203
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
204
+ print(
205
+ f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main"
206
+ )
207
  else:
208
+ print(
209
+ "ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined."
210
+ )
211
 
212
+ print("-" * (60 + len(" App Starting ")) + "\n")
213
 
214
  print("Launching Gradio Interface for Basic Agent Evaluation...")
215
  demo = build_gradio_interface()
216
+ demo.launch(debug=True, share=False)