Nioi commited on
Commit
a83dcc7
·
1 Parent(s): c5eab2c

implemented test on random question without submit

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py CHANGED
@@ -19,6 +19,71 @@ class BasicAgent:
19
  answer = self.agent.run(question)
20
  print(f"Agent returning answer: {answer}")
21
  return answer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  def run_and_submit_all( profile: gr.OAuthProfile | None):
24
  """
@@ -163,6 +228,8 @@ with gr.Blocks() as demo:
163
 
164
  run_button = gr.Button("Run Evaluation & Submit All Answers")
165
 
 
 
166
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
167
  # Removed max_rows=10 from DataFrame constructor
168
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
@@ -172,6 +239,11 @@ with gr.Blocks() as demo:
172
  outputs=[status_output, results_table]
173
  )
174
 
 
 
 
 
 
175
  if __name__ == "__main__":
176
  print("\n" + "-"*30 + " App Starting " + "-"*30)
177
  # Check for SPACE_HOST and SPACE_ID at startup for information
 
19
  answer = self.agent.run(question)
20
  print(f"Agent returning answer: {answer}")
21
  return answer
22
+
23
+ def run_random_question(profile: gr.OAuthProfile | None):
24
+ space_id = os.getenv("SPACE_ID")
25
+
26
+ if profile:
27
+ username= f"{profile.username}"
28
+ print(f"User logged in: {username}")
29
+ else:
30
+ print("User not logged in.")
31
+ return "Please Login to Hugging Face with the button.", None
32
+
33
+ api_url = DEFAULT_API_URL
34
+ random_question_url = f"{api_url}//random-question"
35
+
36
+ try:
37
+ agent = BasicAgent()
38
+ except Exception as e:
39
+ print(f"Error instantiating agent: {e}")
40
+ return f"Error initializing agent: {e}", None
41
+ # In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
42
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
43
+ print(agent_code)
44
+
45
+ print(f"Fetching questions from: {random_question_url}")
46
+ try:
47
+ response = requests.get(random_question_url, timeout=15)
48
+ response.raise_for_status()
49
+ questions_data = response.json()
50
+ if not questions_data:
51
+ print("Fetched questions list is empty.")
52
+ return "Fetched questions list is empty or invalid format.", None
53
+ print(f"Fetched {len(questions_data)} questions.")
54
+ except requests.exceptions.RequestException as e:
55
+ print(f"Error fetching questions: {e}")
56
+ return f"Error fetching questions: {e}", None
57
+ except requests.exceptions.JSONDecodeError as e:
58
+ print(f"Error decoding JSON response from questions endpoint: {e}")
59
+ print(f"Response text: {response.text[:500]}")
60
+ return f"Error decoding server response for questions: {e}", None
61
+ except Exception as e:
62
+ print(f"An unexpected error occurred fetching questions: {e}")
63
+ return f"An unexpected error occurred fetching questions: {e}", None
64
+
65
+ results_log = []
66
+ answers_payload = []
67
+ print(f"Running agent on {len(questions_data)} questions...")
68
+ for item in questions_data:
69
+ task_id = item.get("task_id")
70
+ question_text = item.get("question")
71
+ if not task_id or question_text is None:
72
+ print(f"Skipping item with missing task_id or question: {item}")
73
+ continue
74
+ try:
75
+ submitted_answer = agent(question_text)
76
+ answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
77
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
78
+ except Exception as e:
79
+ print(f"Error running agent on task {task_id}: {e}")
80
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
81
+
82
+ if not answers_payload:
83
+ print("Agent did not produce any answers to submit.")
84
+ return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
85
+
86
+ return "Test done successfully", pd.DataFrame(results_log)
87
 
88
  def run_and_submit_all( profile: gr.OAuthProfile | None):
89
  """
 
228
 
229
  run_button = gr.Button("Run Evaluation & Submit All Answers")
230
 
231
+ test_button = gr.Button("Run Test on a random question")
232
+
233
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
234
  # Removed max_rows=10 from DataFrame constructor
235
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
 
239
  outputs=[status_output, results_table]
240
  )
241
 
242
+ test_button.click(
243
+ fn=run_random_question,
244
+ outputs=[status_output, results_table]
245
+ )
246
+
247
  if __name__ == "__main__":
248
  print("\n" + "-"*30 + " App Starting " + "-"*30)
249
  # Check for SPACE_HOST and SPACE_ID at startup for information