dlaima commited on
Commit
4856d2b
·
verified ·
1 Parent(s): e42b534

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -55
app.py CHANGED
@@ -9,7 +9,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,16 +18,13 @@ of numbers and/or strings. If you are asked for a number, don't use comma to wri
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")
@@ -37,24 +34,22 @@ class MyAgent:
37
  )
38
 
39
  def __call__(self, question: str) -> str:
40
- # Combine system prompt and user question as a single input
41
- full_prompt = f"{SYSTEM_PROMPT}\n\nUser question: {question}"
 
 
42
  try:
43
- output = self.model.run(full_prompt)
44
- return extract_final_answer(output)
45
  except Exception as e:
46
  import traceback
47
  traceback.print_exc()
48
  return f"AGENT ERROR: {e}"
49
 
 
50
 
51
- # ---------------- RUN AND SUBMIT ----------------
52
  def run_and_submit_all(profile: gr.OAuthProfile | None):
53
  space_id = os.getenv("SPACE_ID")
54
- api_url = DEFAULT_API_URL
55
- questions_url = f"{api_url}/questions"
56
- submit_url = f"{api_url}/submit"
57
-
58
  if profile:
59
  username = profile.username
60
  print(f"User logged in: {username}")
@@ -62,13 +57,16 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
62
  print("User not logged in.")
63
  return "Please Login to Hugging Face with the button.", None
64
 
 
 
 
 
65
  try:
66
  agent = MyAgent()
67
  except Exception as e:
68
  return f"Error initializing agent: {e}", None
69
 
70
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
71
- print(f"Fetching questions from: {questions_url}")
72
 
73
  try:
74
  response = requests.get(questions_url, timeout=15)
@@ -81,8 +79,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
81
 
82
  results_log = []
83
  answers_payload = []
84
-
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")
@@ -91,27 +87,14 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
91
  try:
92
  submitted_answer = agent(question_text)
93
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
94
- results_log.append({
95
- "Task ID": task_id,
96
- "Question": question_text,
97
- "Submitted Answer": submitted_answer
98
- })
99
  except Exception as e:
100
- results_log.append({
101
- "Task ID": task_id,
102
- "Question": question_text,
103
- "Submitted Answer": f"AGENT ERROR: {e}"
104
- })
105
 
106
  if not answers_payload:
107
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
108
 
109
- submission_data = {
110
- "username": username.strip(),
111
- "agent_code": agent_code,
112
- "answers": answers_payload
113
- }
114
-
115
  try:
116
  response = requests.post(submit_url, json=submission_data, timeout=60)
117
  response.raise_for_status()
@@ -138,33 +121,27 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
138
  except Exception as e:
139
  return f"An unexpected error occurred during submission: {e}", pd.DataFrame(results_log)
140
 
141
-
142
- # ---------------- UI ----------------
143
  with gr.Blocks() as demo:
144
  gr.Markdown("# Basic Agent Evaluation Runner")
145
- gr.Markdown(
146
- """
147
- **Instructions:**
148
- 1. Clone this space, modify code to define your agent's logic, tools, and packages.
149
- 2. Log in to your Hugging Face account using the button below.
150
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see your score.
151
-
152
- **Note:** Submitting can take some time.
153
- """
154
- )
155
 
156
  gr.LoginButton()
157
  run_button = gr.Button("Run Evaluation & Submit All Answers")
158
-
159
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
160
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
161
 
162
  run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
163
 
164
-
165
- # ---------------- MAIN ----------------
166
  if __name__ == "__main__":
167
- print("\n" + "-" * 30 + " App Starting " + "-" * 30)
168
  space_host = os.getenv("SPACE_HOST")
169
  space_id = os.getenv("SPACE_ID")
170
 
@@ -181,10 +158,10 @@ if __name__ == "__main__":
181
  else:
182
  print("ℹ️ SPACE_ID environment variable not found (running locally?).")
183
 
184
- print("-" * (60 + len(" App Starting ")) + "\n")
 
185
  print("Launching Gradio Interface for Basic Agent Evaluation...")
186
  demo.launch(debug=True, share=False)
187
 
188
 
189
 
190
-
 
9
  from smolagents import CodeAgent, DuckDuckGoSearchTool
10
  from smolagents.models import OpenAIServerModel
11
 
12
+ # System prompt required by GAIA
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
+ # Extract FINAL ANSWER
22
 
23
+ def extract_final_answer(response: str) -> str:
24
+ match = re.search(r"FINAL ANSWER:\s*(.*)", response, re.IGNORECASE)
25
+ return match.group(1).strip() if match else response.strip()
 
 
 
26
 
27
+ # MyAgent class
 
28
  class MyAgent:
29
  def __init__(self):
30
  self.model = OpenAIServerModel(model_id="gpt-4")
 
34
  )
35
 
36
  def __call__(self, question: str) -> str:
37
+ messages = [
38
+ {"role": "system", "content": SYSTEM_PROMPT},
39
+ {"role": "user", "content": question}
40
+ ]
41
  try:
42
+ response = self.model.chat(messages)
43
+ return extract_final_answer(response)
44
  except Exception as e:
45
  import traceback
46
  traceback.print_exc()
47
  return f"AGENT ERROR: {e}"
48
 
49
+ # Evaluation and submission
50
 
 
51
  def run_and_submit_all(profile: gr.OAuthProfile | None):
52
  space_id = os.getenv("SPACE_ID")
 
 
 
 
53
  if profile:
54
  username = profile.username
55
  print(f"User logged in: {username}")
 
57
  print("User not logged in.")
58
  return "Please Login to Hugging Face with the button.", None
59
 
60
+ api_url = DEFAULT_API_URL
61
+ questions_url = f"{api_url}/questions"
62
+ submit_url = f"{api_url}/submit"
63
+
64
  try:
65
  agent = MyAgent()
66
  except Exception as e:
67
  return f"Error initializing agent: {e}", None
68
 
69
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
 
70
 
71
  try:
72
  response = requests.get(questions_url, timeout=15)
 
79
 
80
  results_log = []
81
  answers_payload = []
 
 
82
  for item in questions_data:
83
  task_id = item.get("task_id")
84
  question_text = item.get("question")
 
87
  try:
88
  submitted_answer = agent(question_text)
89
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
90
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
 
 
 
 
91
  except Exception as e:
92
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
 
 
 
93
 
94
  if not answers_payload:
95
  return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
96
 
97
+ submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
 
 
 
 
 
98
  try:
99
  response = requests.post(submit_url, json=submission_data, timeout=60)
100
  response.raise_for_status()
 
121
  except Exception as e:
122
  return f"An unexpected error occurred during submission: {e}", pd.DataFrame(results_log)
123
 
124
+ # Gradio UI
 
125
  with gr.Blocks() as demo:
126
  gr.Markdown("# Basic Agent Evaluation Runner")
127
+ gr.Markdown("""
128
+ **Instructions:**
129
+ 1. Clone this space, modify code to define your agent's logic, tools, and packages.
130
+ 2. Log in to your Hugging Face account using the button below.
131
+ 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see your score.
132
+
133
+ **Note:** Submitting can take some time.
134
+ """)
 
 
135
 
136
  gr.LoginButton()
137
  run_button = gr.Button("Run Evaluation & Submit All Answers")
 
138
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
139
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
140
 
141
  run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
142
 
 
 
143
  if __name__ == "__main__":
144
+ print("\n" + "-"*30 + " App Starting " + "-"*30)
145
  space_host = os.getenv("SPACE_HOST")
146
  space_id = os.getenv("SPACE_ID")
147
 
 
158
  else:
159
  print("ℹ️ SPACE_ID environment variable not found (running locally?).")
160
 
161
+ print("-"*(60 + len(" App Starting ")) + "\n")
162
+
163
  print("Launching Gradio Interface for Basic Agent Evaluation...")
164
  demo.launch(debug=True, share=False)
165
 
166
 
167