dlaima commited on
Commit
9ccf47b
·
verified ·
1 Parent(s): bb78253

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -39
app.py CHANGED
@@ -1,6 +1,7 @@
1
 
2
 
3
  import os
 
4
  import gradio as gr
5
  import requests
6
  import pandas as pd
@@ -8,7 +9,7 @@ import pandas as pd
8
  from smolagents import CodeAgent, DuckDuckGoSearchTool
9
  from smolagents.models import OpenAIServerModel
10
 
11
- # System prompt as per your instructions
12
  SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question.
13
  Report your thoughts, and finish your answer with the following template:
14
  FINAL ANSWER: [YOUR FINAL ANSWER].
@@ -17,12 +18,19 @@ of numbers and/or strings. If you are asked for a number, don't use comma to wri
17
 
18
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
19
 
 
 
 
 
 
 
 
 
 
 
20
  class MyAgent:
21
  def __init__(self):
22
- # Initialize model WITHOUT system_message or system_prompt
23
- self.model = OpenAIServerModel(
24
- model_id="gpt-4"
25
- )
26
  self.agent = CodeAgent(
27
  tools=[DuckDuckGoSearchTool()],
28
  model=self.model
@@ -34,18 +42,21 @@ class MyAgent:
34
  {"role": "user", "content": question}
35
  ]
36
  try:
37
- return self.model.run(messages)
 
 
38
  except Exception as e:
39
  import traceback
40
  traceback.print_exc()
41
  return f"AGENT ERROR: {e}"
42
 
43
 
 
44
  def run_and_submit_all(profile: gr.OAuthProfile | None):
45
- """
46
- Fetches questions, runs the agent, submits answers, returns status and results table.
47
- """
48
  space_id = os.getenv("SPACE_ID")
 
 
 
49
 
50
  if profile:
51
  username = profile.username
@@ -54,55 +65,56 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
54
  print("User not logged in.")
55
  return "Please Login to Hugging Face with the button.", None
56
 
57
- api_url = DEFAULT_API_URL
58
- questions_url = f"{api_url}/questions"
59
- submit_url = f"{api_url}/submit"
60
-
61
  try:
62
  agent = MyAgent()
63
  except Exception as e:
64
- print(f"Error initializing agent: {e}")
65
  return f"Error initializing agent: {e}", None
66
 
67
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
68
- print(f"Agent code URL: {agent_code}")
69
-
70
  print(f"Fetching questions from: {questions_url}")
 
71
  try:
72
  response = requests.get(questions_url, timeout=15)
73
  response.raise_for_status()
74
  questions_data = response.json()
75
  if not questions_data:
76
- print("Fetched questions list is empty.")
77
  return "Fetched questions list is empty or invalid format.", None
78
- print(f"Fetched {len(questions_data)} questions.")
79
  except Exception as e:
80
- print(f"Error fetching questions: {e}")
81
  return f"Error fetching questions: {e}", None
82
 
83
  results_log = []
84
  answers_payload = []
 
85
  print(f"Running agent on {len(questions_data)} questions...")
86
  for item in questions_data:
87
  task_id = item.get("task_id")
88
  question_text = item.get("question")
89
  if not task_id or question_text is None:
90
- print(f"Skipping invalid item: {item}")
91
  continue
92
  try:
93
  submitted_answer = agent(question_text)
94
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
95
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
 
 
 
 
96
  except Exception as e:
97
- print(f"Error running agent on task {task_id}: {e}")
98
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
 
 
99
 
100
  if not answers_payload:
101
- print("Agent did not produce any answers to submit.")
102
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
103
 
104
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
105
- print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
 
 
 
 
106
  try:
107
  response = requests.post(submit_url, json=submission_data, timeout=60)
108
  response.raise_for_status()
@@ -114,7 +126,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
114
  f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
115
  f"Message: {result_data.get('message', 'No message received.')}"
116
  )
117
- print("Submission successful.")
118
  results_df = pd.DataFrame(results_log)
119
  return final_status, results_df
120
  except requests.exceptions.HTTPError as e:
@@ -124,18 +135,14 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
124
  error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
125
  except Exception:
126
  error_detail += f" Response: {e.response.text[:500]}"
127
- status_message = f"Submission Failed: {error_detail}"
128
- print(status_message)
129
- return status_message, pd.DataFrame(results_log)
130
  except requests.exceptions.Timeout:
131
- status_message = "Submission Failed: The request timed out."
132
- print(status_message)
133
- return status_message, pd.DataFrame(results_log)
134
  except Exception as e:
135
- status_message = f"An unexpected error occurred during submission: {e}"
136
- print(status_message)
137
- return status_message, pd.DataFrame(results_log)
138
 
 
139
  with gr.Blocks() as demo:
140
  gr.Markdown("# Basic Agent Evaluation Runner")
141
  gr.Markdown(
@@ -157,8 +164,10 @@ with gr.Blocks() as demo:
157
 
158
  run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
159
 
 
 
160
  if __name__ == "__main__":
161
- print("\n" + "-"*30 + " App Starting " + "-"*30)
162
  space_host = os.getenv("SPACE_HOST")
163
  space_id = os.getenv("SPACE_ID")
164
 
@@ -175,8 +184,7 @@ if __name__ == "__main__":
175
  else:
176
  print("ℹ️ SPACE_ID environment variable not found (running locally?).")
177
 
178
- print("-"*(60 + len(" App Starting ")) + "\n")
179
-
180
  print("Launching Gradio Interface for Basic Agent Evaluation...")
181
  demo.launch(debug=True, share=False)
182
 
 
1
 
2
 
3
  import os
4
+ import re
5
  import gradio as gr
6
  import requests
7
  import pandas as pd
 
9
  from smolagents import CodeAgent, DuckDuckGoSearchTool
10
  from smolagents.models import OpenAIServerModel
11
 
12
+ # ---------------- SYSTEM PROMPT ----------------
13
  SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question.
14
  Report your thoughts, and finish your answer with the following template:
15
  FINAL ANSWER: [YOUR FINAL ANSWER].
 
18
 
19
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
20
 
21
+
22
+ # ---------------- FINAL ANSWER EXTRACTOR ----------------
23
+ def extract_final_answer(output: str) -> str:
24
+ match = re.search(r"FINAL ANSWER:\s*(.*)", output, re.IGNORECASE)
25
+ if match:
26
+ return match.group(1).strip()
27
+ return f"PARSE ERROR: FINAL ANSWER not found in output: {output}"
28
+
29
+
30
+ # ---------------- AGENT WRAPPER ----------------
31
  class MyAgent:
32
  def __init__(self):
33
+ self.model = OpenAIServerModel(model_id="gpt-4")
 
 
 
34
  self.agent = CodeAgent(
35
  tools=[DuckDuckGoSearchTool()],
36
  model=self.model
 
42
  {"role": "user", "content": question}
43
  ]
44
  try:
45
+ response = self.model.chat(messages)
46
+ raw_output = response["content"]
47
+ return extract_final_answer(raw_output)
48
  except Exception as e:
49
  import traceback
50
  traceback.print_exc()
51
  return f"AGENT ERROR: {e}"
52
 
53
 
54
+ # ---------------- RUN AND SUBMIT ----------------
55
  def run_and_submit_all(profile: gr.OAuthProfile | None):
 
 
 
56
  space_id = os.getenv("SPACE_ID")
57
+ api_url = DEFAULT_API_URL
58
+ questions_url = f"{api_url}/questions"
59
+ submit_url = f"{api_url}/submit"
60
 
61
  if profile:
62
  username = profile.username
 
65
  print("User not logged in.")
66
  return "Please Login to Hugging Face with the button.", None
67
 
 
 
 
 
68
  try:
69
  agent = MyAgent()
70
  except Exception as e:
 
71
  return f"Error initializing agent: {e}", None
72
 
73
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
 
 
74
  print(f"Fetching questions from: {questions_url}")
75
+
76
  try:
77
  response = requests.get(questions_url, timeout=15)
78
  response.raise_for_status()
79
  questions_data = response.json()
80
  if not questions_data:
 
81
  return "Fetched questions list is empty or invalid format.", None
 
82
  except Exception as e:
 
83
  return f"Error fetching questions: {e}", None
84
 
85
  results_log = []
86
  answers_payload = []
87
+
88
  print(f"Running agent on {len(questions_data)} questions...")
89
  for item in questions_data:
90
  task_id = item.get("task_id")
91
  question_text = item.get("question")
92
  if not task_id or question_text is None:
 
93
  continue
94
  try:
95
  submitted_answer = agent(question_text)
96
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
97
+ results_log.append({
98
+ "Task ID": task_id,
99
+ "Question": question_text,
100
+ "Submitted Answer": submitted_answer
101
+ })
102
  except Exception as e:
103
+ results_log.append({
104
+ "Task ID": task_id,
105
+ "Question": question_text,
106
+ "Submitted Answer": f"AGENT ERROR: {e}"
107
+ })
108
 
109
  if not answers_payload:
 
110
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
111
 
112
+ submission_data = {
113
+ "username": username.strip(),
114
+ "agent_code": agent_code,
115
+ "answers": answers_payload
116
+ }
117
+
118
  try:
119
  response = requests.post(submit_url, json=submission_data, timeout=60)
120
  response.raise_for_status()
 
126
  f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
127
  f"Message: {result_data.get('message', 'No message received.')}"
128
  )
 
129
  results_df = pd.DataFrame(results_log)
130
  return final_status, results_df
131
  except requests.exceptions.HTTPError as e:
 
135
  error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
136
  except Exception:
137
  error_detail += f" Response: {e.response.text[:500]}"
138
+ return f"Submission Failed: {error_detail}", pd.DataFrame(results_log)
 
 
139
  except requests.exceptions.Timeout:
140
+ return "Submission Failed: The request timed out.", pd.DataFrame(results_log)
 
 
141
  except Exception as e:
142
+ return f"An unexpected error occurred during submission: {e}", pd.DataFrame(results_log)
143
+
 
144
 
145
+ # ---------------- UI ----------------
146
  with gr.Blocks() as demo:
147
  gr.Markdown("# Basic Agent Evaluation Runner")
148
  gr.Markdown(
 
164
 
165
  run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
166
 
167
+
168
+ # ---------------- MAIN ----------------
169
  if __name__ == "__main__":
170
+ print("\n" + "-" * 30 + " App Starting " + "-" * 30)
171
  space_host = os.getenv("SPACE_HOST")
172
  space_id = os.getenv("SPACE_ID")
173
 
 
184
  else:
185
  print("ℹ️ SPACE_ID environment variable not found (running locally?).")
186
 
187
+ print("-" * (60 + len(" App Starting ")) + "\n")
 
188
  print("Launching Gradio Interface for Basic Agent Evaluation...")
189
  demo.launch(debug=True, share=False)
190