yooke commited on
Commit
7f756b6
·
verified ·
1 Parent(s): 2a9a4dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -65
app.py CHANGED
@@ -1,137 +1,192 @@
1
  import os
 
2
  import gradio as gr
3
  import requests
 
4
  import pandas as pd
 
 
 
5
 
6
- from smolagents import (
7
- ToolCallingAgent,
8
- CodeAgent,
9
- DuckDuckGoSearchTool,
10
- OpenAIServerModel
11
- )
12
-
13
  # --- Constants ---
14
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
 
 
 
 
15
 
16
  # --- Basic Agent Definition ---
 
17
  class BasicAgent:
18
  def __init__(self):
19
- self.model = OpenAIServerModel(
20
- model_id='deepseek-chat',
21
- api_base="https://api.deepseek.com",
22
- api_key=os.environ["DEEPSEEK_API_KEY"],
23
- )
24
- self.agent = ToolCallingAgent(
25
- tools=[DuckDuckGoSearchTool()],
 
 
 
 
 
 
26
  model=self.model,
 
27
  add_base_tools=True
28
  )
 
 
 
 
29
 
30
- # 修改系统提示词,允许更详细的回答
31
- self.agent.prompt_templates['system_prompt'] = """You are a general AI assistant. I will ask you a question.
32
- Your primary goal is to answer the question accurately and thoroughly.
33
- The 'content' field of your response message, even if it's just your thought process before a tool call, MUST be a single string or null. It MUST NOT be a list or any other sequence.
34
- If you are providing a direct answer without using any tools, your response content MUST be a single string.
35
-
36
- For complex questions that require detailed explanations (like describing benchmarks, datasets, or technical concepts), provide a comprehensive answer with all relevant details.
37
-
38
- Finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
39
- YOUR FINAL ANSWER can be detailed and comprehensive when the question requires it, especially for questions about technical topics, benchmarks, datasets, or concepts that need thorough explanation.
40
- """
41
-
42
- def __call__(self, question: str) -> str:
43
- # 移除所有打印语句,直接返回答案
44
- return self.agent.run(question)
45
 
46
- def run_and_submit_all(profile: gr.OAuthProfile | None):
47
  """
48
  Fetches all questions, runs the BasicAgent on them, submits all answers,
49
  and displays the results.
50
  """
51
  # --- Determine HF Space Runtime URL and Repo URL ---
52
- space_id = os.getenv("SPACE_ID")
53
 
54
  if profile:
55
- username = f"{profile.username}"
 
56
  else:
57
- return "请使用下方按钮登录Hugging Face。", None
 
58
 
59
  api_url = DEFAULT_API_URL
60
  questions_url = f"{api_url}/questions"
61
  submit_url = f"{api_url}/submit"
62
 
63
- # 1. 实例化代理
64
  try:
65
  agent = BasicAgent()
66
  except Exception as e:
67
- return f"初始化代理时出错: {e}", None
68
-
 
69
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
 
70
 
71
- # 2. 获取问题
 
72
  try:
73
  response = requests.get(questions_url, timeout=15)
74
  response.raise_for_status()
75
  questions_data = response.json()
76
  if not questions_data:
77
- return "获取的问题列表为空或格式无效。", None
 
 
 
 
 
 
 
 
 
78
  except Exception as e:
79
- return f"获取问题时出错: {e}", None
 
80
 
81
- # 3. 运行代理
82
  results_log = []
83
  answers_payload = []
84
-
85
- for i, item in enumerate(questions_data):
86
  task_id = item.get("task_id")
87
  question_text = item.get("question")
88
  if not task_id or question_text is None:
 
89
  continue
90
  try:
91
- submitted_answer = agent(question_text)
92
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
93
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
94
  except Exception as e:
95
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
 
 
96
 
97
  if not answers_payload:
98
- return "代理未能生成任何答案。", pd.DataFrame(results_log)
 
99
 
100
- # 4. 准备提交
101
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
 
 
102
 
103
- # 5. 提交
 
104
  try:
105
  response = requests.post(submit_url, json=submission_data, timeout=60)
106
  response.raise_for_status()
107
  result_data = response.json()
108
  final_status = (
109
- f"提交成功!\n"
110
- f"用户: {result_data.get('username')}\n"
111
- f"总分: {result_data.get('score', 'N/A')}% "
112
- f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} 正确)\n"
113
- f"消息: {result_data.get('message', '未收到消息。')}"
114
  )
 
115
  results_df = pd.DataFrame(results_log)
116
  return final_status, results_df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  except Exception as e:
118
- status_message = f"提交失败: {e}"
 
119
  results_df = pd.DataFrame(results_log)
120
  return status_message, results_df
121
 
122
 
123
-
124
  # --- Build Gradio Interface using Blocks ---
125
  with gr.Blocks() as demo:
126
- gr.Markdown("# LangGraph Agent Evaluation Runner") # Updated title
127
  gr.Markdown(
128
  """
129
  **Instructions:**
130
 
131
- 1. Please clone this space, then modify the code in `agent.py` and `app.py` to define your agent's logic, the tools, the necessary packages, etc ...
132
- 2. **Make sure you have your `DEEPSEEK_API_KEY` set as a Space Secret.**
133
- 3. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
134
- 4. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
135
 
136
  ---
137
  **Disclaimers:**
@@ -145,6 +200,7 @@ with gr.Blocks() as demo:
145
  run_button = gr.Button("Run Evaluation & Submit All Answers")
146
 
147
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
 
148
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
149
 
150
  run_button.click(
@@ -153,10 +209,10 @@ with gr.Blocks() as demo:
153
  )
154
 
155
  if __name__ == "__main__":
156
- print("\n" + "-" * 30 + " App Starting " + "-" * 30)
157
  # Check for SPACE_HOST and SPACE_ID at startup for information
158
  space_host_startup = os.getenv("SPACE_HOST")
159
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
160
 
161
  if space_host_startup:
162
  print(f"✅ SPACE_HOST found: {space_host_startup}")
@@ -164,14 +220,14 @@ if __name__ == "__main__":
164
  else:
165
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
166
 
167
- if space_id_startup: # Print repo URLs if SPACE_ID is found
168
  print(f"✅ SPACE_ID found: {space_id_startup}")
169
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
170
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
171
  else:
172
  print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
173
 
174
- print("-" * (60 + len(" App Starting ")) + "\n")
175
 
176
- print("Launching Gradio Interface for LangGraph Agent Evaluation...") # Updated message
177
- demo.launch(debug=True, share=False, auth=None)
 
1
  import os
2
+ import traceback
3
  import gradio as gr
4
  import requests
5
+ import inspect
6
  import pandas as pd
7
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, PythonInterpreterTool, HfApiModel, LiteLLMModel, WikipediaSearchTool
8
+ from huggingface_hub import login
9
+ import time
10
 
11
+ # (Keep Constants as is)
 
 
 
 
 
 
12
  # --- Constants ---
13
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
14
+ #HF_TOKEN = os.getenv("HF_TOKEN")
15
+ #login(HF_TOKEN)
16
+ model = LiteLLMModel(model_id="deepseek-chat",
17
+ api_key=os.getenv("DEEPSEEK_API_KEY"),
18
+ base_url="https://api.deepseek.com")
19
 
20
  # --- Basic Agent Definition ---
21
+ # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
22
  class BasicAgent:
23
  def __init__(self):
24
+ print("BasicAgent initialized.")
25
+ # Set up model
26
+ self.model = model
27
+
28
+ # Initialize custom tools
29
+ self.tools = [
30
+ PythonInterpreterTool(),
31
+ DuckDuckGoSearchTool(),
32
+ WikipediaSearchTool(),
33
+ ]
34
+
35
+ # Create agent
36
+ self.agent = CodeAgent(
37
  model=self.model,
38
+ tools=self.tools,
39
  add_base_tools=True
40
  )
41
+
42
+ def answer_question(self, question: str) -> str:
43
+ """Process a question and return the answer"""
44
+ print(f"Processing question: {question[:50]}..." if len(question) > 50 else question)
45
 
46
+ try:
47
+ result = self.agent.run(question)
48
+ # Ensure result is always a string
49
+ if not isinstance(result, str):
50
+ result = str(result)
51
+ return result
52
+ except Exception as e:
53
+ print(traceback.format_exc())
54
+ error_msg = f"Error running agent: {str(e)}"
55
+ return f"I encountered an issue while processing your question: {str(e)}"
 
 
 
 
 
56
 
57
+ def run_and_submit_all( profile: gr.OAuthProfile | None):
58
  """
59
  Fetches all questions, runs the BasicAgent on them, submits all answers,
60
  and displays the results.
61
  """
62
  # --- Determine HF Space Runtime URL and Repo URL ---
63
+ space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
64
 
65
  if profile:
66
+ username= f"{profile.username}"
67
+ print(f"User logged in: {username}")
68
  else:
69
+ print("User not logged in.")
70
+ return "Please Login to Hugging Face with the button.", None
71
 
72
  api_url = DEFAULT_API_URL
73
  questions_url = f"{api_url}/questions"
74
  submit_url = f"{api_url}/submit"
75
 
76
+ # 1. Instantiate Agent ( modify this part to create your agent)
77
  try:
78
  agent = BasicAgent()
79
  except Exception as e:
80
+ print(f"Error instantiating agent: {e}")
81
+ return f"Error initializing agent: {e}", None
82
+ # 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)
83
  agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
84
+ print(agent_code)
85
 
86
+ # 2. Fetch Questions
87
+ print(f"Fetching questions from: {questions_url}")
88
  try:
89
  response = requests.get(questions_url, timeout=15)
90
  response.raise_for_status()
91
  questions_data = response.json()
92
  if not questions_data:
93
+ print("Fetched questions list is empty.")
94
+ return "Fetched questions list is empty or invalid format.", None
95
+ print(f"Fetched {len(questions_data)} questions.")
96
+ except requests.exceptions.RequestException as e:
97
+ print(f"Error fetching questions: {e}")
98
+ return f"Error fetching questions: {e}", None
99
+ except requests.exceptions.JSONDecodeError as e:
100
+ print(f"Error decoding JSON response from questions endpoint: {e}")
101
+ print(f"Response text: {response.text[:500]}")
102
+ return f"Error decoding server response for questions: {e}", None
103
  except Exception as e:
104
+ print(f"An unexpected error occurred fetching questions: {e}")
105
+ return f"An unexpected error occurred fetching questions: {e}", None
106
 
107
+ # 3. Run your Agent
108
  results_log = []
109
  answers_payload = []
110
+ print(f"Running agent on {len(questions_data)} questions...")
111
+ for item in questions_data:
112
  task_id = item.get("task_id")
113
  question_text = item.get("question")
114
  if not task_id or question_text is None:
115
+ print(f"Skipping item with missing task_id or question: {item}")
116
  continue
117
  try:
118
+ submitted_answer = agent.answer_question(question_text)
119
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
120
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
121
  except Exception as e:
122
+ print(f"Error running agent on task {task_id}: {e}")
123
+ results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
124
+
125
+ time.sleep(45)
126
 
127
  if not answers_payload:
128
+ print("Agent did not produce any answers to submit.")
129
+ return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
130
 
131
+ # 4. Prepare Submission
132
  submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
133
+ status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
134
+ print(status_update)
135
 
136
+ # 5. Submit
137
+ print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
138
  try:
139
  response = requests.post(submit_url, json=submission_data, timeout=60)
140
  response.raise_for_status()
141
  result_data = response.json()
142
  final_status = (
143
+ f"Submission Successful!\n"
144
+ f"User: {result_data.get('username')}\n"
145
+ f"Overall Score: {result_data.get('score', 'N/A')}% "
146
+ f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
147
+ f"Message: {result_data.get('message', 'No message received.')}"
148
  )
149
+ print("Submission successful.")
150
  results_df = pd.DataFrame(results_log)
151
  return final_status, results_df
152
+ except requests.exceptions.HTTPError as e:
153
+ error_detail = f"Server responded with status {e.response.status_code}."
154
+ try:
155
+ error_json = e.response.json()
156
+ error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
157
+ except requests.exceptions.JSONDecodeError:
158
+ error_detail += f" Response: {e.response.text[:500]}"
159
+ status_message = f"Submission Failed: {error_detail}"
160
+ print(status_message)
161
+ results_df = pd.DataFrame(results_log)
162
+ return status_message, results_df
163
+ except requests.exceptions.Timeout:
164
+ status_message = "Submission Failed: The request timed out."
165
+ print(status_message)
166
+ results_df = pd.DataFrame(results_log)
167
+ return status_message, results_df
168
+ except requests.exceptions.RequestException as e:
169
+ status_message = f"Submission Failed: Network error - {e}"
170
+ print(status_message)
171
+ results_df = pd.DataFrame(results_log)
172
+ return status_message, results_df
173
  except Exception as e:
174
+ status_message = f"An unexpected error occurred during submission: {e}"
175
+ print(status_message)
176
  results_df = pd.DataFrame(results_log)
177
  return status_message, results_df
178
 
179
 
 
180
  # --- Build Gradio Interface using Blocks ---
181
  with gr.Blocks() as demo:
182
+ gr.Markdown("# Basic Agent Evaluation Runner")
183
  gr.Markdown(
184
  """
185
  **Instructions:**
186
 
187
+ 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
188
+ 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
189
+ 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
 
190
 
191
  ---
192
  **Disclaimers:**
 
200
  run_button = gr.Button("Run Evaluation & Submit All Answers")
201
 
202
  status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
203
+ # Removed max_rows=10 from DataFrame constructor
204
  results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
205
 
206
  run_button.click(
 
209
  )
210
 
211
  if __name__ == "__main__":
212
+ print("\n" + "-"*30 + " App Starting " + "-"*30)
213
  # Check for SPACE_HOST and SPACE_ID at startup for information
214
  space_host_startup = os.getenv("SPACE_HOST")
215
+ space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
216
 
217
  if space_host_startup:
218
  print(f"✅ SPACE_HOST found: {space_host_startup}")
 
220
  else:
221
  print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
222
 
223
+ if space_id_startup: # Print repo URLs if SPACE_ID is found
224
  print(f"✅ SPACE_ID found: {space_id_startup}")
225
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
226
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
227
  else:
228
  print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
229
 
230
+ print("-"*(60 + len(" App Starting ")) + "\n")
231
 
232
+ print("Launching Gradio Interface for Basic Agent Evaluation...")
233
+ demo.launch(debug=True, share=False)