9jini commited on
Commit
31dae8e
·
verified ·
1 Parent(s): 9eafd69

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +189 -193
app.py CHANGED
@@ -1,211 +1,207 @@
1
- import os # 导入操作系统模块
2
- import gradio as gr # 导入Gradio库用于创建Web界面
3
- import requests # 导入请求库用于API调用
4
- import inspect # 导入检查模块用于函数检查
5
- import pandas as pd # 导入pandas用于数据处理
6
-
7
- from smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIServerModel, WikipediaSearchTool # 从smolagents导入所需组件
8
-
9
- # (Keep Constants as is) # 保持常量不变
10
- # --- Constants --- # 常量部分
11
- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
12
- deepseek_api_key = "sk-04da63f756c9468182d278f52e33ad15"
13
- deepseek_api_base_url = "https://api.deepseek.com"
14
- # --- Basic Agent Definition ---
15
- # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
16
- class BasicAgent:
17
- def __init__(self):
18
- # model = OpenAIServerModel(model_id="gpt-4o")
19
- # 已经直接设置了 API Key,不需要再检查环境变量
20
- model = OpenAIServerModel(
21
- model_id="deepseek-chat",
22
- api_base=deepseek_api_base_url,
23
- api_headers={"Authorization": f"Bearer {deepseek_api_key}", "Content-Type": "application/json"}
24
- )
25
- search_tool = DuckDuckGoSearchTool()
26
- wiki_search = WikipediaSearchTool()
27
-
28
- self.agent = CodeAgent( # 初始化代码代理
29
- model = model, # 设置模型
30
- tools=[ # 设置可用工具列表
31
- search_tool, # 搜索工具
32
- wiki_search # 维基百科工具
33
  ]
34
  )
35
 
36
- def __call__(self, question: str) -> str: # 定义调用方法,使对象可直接调用
37
- return self.agent.run(question) # type: ignore # 运行代理并返回结果
38
 
39
- def run_and_submit_all( profile: gr.OAuthProfile | None): # 运行并提交所有回答的函数
40
  """
41
  Fetches all questions, runs the BasicAgent on them, submits all answers,
42
  and displays the results.
43
- """
44
- # --- Determine HF Space Runtime URL and Repo URL ---
45
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code # 获取SPACE_ID用于发送代码链接
46
-
47
- if profile: # 如果用户已登录
48
- username= f"{profile.username}" # 获取用户名
49
- print(f"User logged in: {username}") # 打印已登录的用户名
50
- else: # 如果用户未登录
51
- print("User not logged in.") # 打印用户未登录
52
- return "Please Login to Hugging Face with the button.", None # 返回请求登录的消息
53
-
54
- api_url = DEFAULT_API_URL # 设置API URL
55
- questions_url = f"{api_url}/questions" # 构建获取问题的URL
56
- submit_url = f"{api_url}/submit" # 构建提交答案的URL
57
-
58
- # 1. Instantiate Agent ( modify this part to create your agent) # 1. 实例化代理(修改此部分来创建你的代理)
59
- try: # 尝试
60
- agent = BasicAgent() # 创建基本代理实例
61
- except Exception as e: # 捕获异常
62
- print(f"Error instantiating agent: {e}") # 打印代理实例化错误
63
- return f"Error initializing agent: {e}", None # 返回初始化错误信息
64
- # 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) # 当应用程序作为Hugging Face空间运行时,此链接指向你的代码库(对其他人有用,请保持公开)
65
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" # 构建代码库URL
66
- print(agent_code) # 打印代码库URL
67
-
68
- # 2. Fetch Questions # 2. 获取问题
69
- print(f"Fetching questions from: {questions_url}") # 打印正在从哪个URL获取问题
70
- try: # 尝试
71
- response = requests.get(questions_url, timeout=15) # 发送GET请求获取问题,超时设置为15秒
72
- response.raise_for_status() # 检查HTTP错误
73
- questions_data = response.json() # 解析JSON响应
74
- if not questions_data: # 如果问题列表为空
75
- print("Fetched questions list is empty.") # 打印问题列表为空
76
- return "Fetched questions list is empty or invalid format.", None # 返回问题列表为空或格式无效的消息
77
- print(f"Fetched {len(questions_data)} questions.") # 打印获取到的问题数量
78
- except requests.exceptions.RequestException as e: # 捕获请求异常
79
- print(f"Error fetching questions: {e}") # 打印获取问题错误
80
- return f"Error fetching questions: {e}", None # 返回获取问题错误
81
- except requests.exceptions.JSONDecodeError as e: # 捕获JSON解码错误
82
- print(f"Error decoding JSON response from questions endpoint: {e}") # 打印JSON解码错误
83
- print(f"Response text: {response.text[:500]}") # 打印响应文本(前500个字符)
84
- return f"Error decoding server response for questions: {e}", None # 返回服务器响应解码错误
85
- except Exception as e: # 捕获其他异常
86
- print(f"An unexpected error occurred fetching questions: {e}") # 打印获取问题时发生意外错误
87
- return f"An unexpected error occurred fetching questions: {e}", None # 返回获取问题时发生意外错误
88
-
89
- # 3. Run your Agent # 3. 运行你的代理
90
- results_log = [] # 初始化结果日志列表
91
- answers_payload = [] # 初始化答案载荷列表
92
- print(f"Running agent on {len(questions_data)} questions...") # 打印正在多少个问题上运行代理
93
- for item in questions_data: # 遍历每个问题数据项
94
- task_id = item.get("task_id") # 获取任务ID
95
- question_text = item.get("question") # 获取问题文本
96
- if not task_id or question_text is None: # 如果任务ID或问题文本缺失
97
- print(f"Skipping item with missing task_id or question: {item}") # 打印跳过缺少任务ID或问题的项
98
- continue # 继续下一个循环
99
- try: # 尝试
100
- submitted_answer = agent(question_text) # 使用代理回答问题
101
- answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer}) # 将答案添加到载荷中
102
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer}) # 将结果添加到日志中
103
- except Exception as e: # 捕获异常
104
- print(f"Error running agent on task {task_id}: {e}") # 打印在任务上运行代理时出错
105
- results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"}) # 将错误添加到结果日志中
106
-
107
- if not answers_payload: # 如果答案载荷为空
108
- print("Agent did not produce any answers to submit.") # 打印代理没有产生任何答案可提交
109
- return "Agent did not produce any answers to submit.", pd.DataFrame(results_log) # 返回代理没有产生任何答案可提交
110
-
111
- # 4. Prepare Submission # 4. 准备提交
112
- submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload} # 准备提交数据
113
- status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..." # 构建状态更新消息
114
- print(status_update) # 打印状态更新
115
-
116
- # 5. Submit # 5. 提交
117
- print(f"Submitting {len(answers_payload)} answers to: {submit_url}") # 打印正在将多少个答案提交到哪个URL
118
- try: # 尝试
119
- response = requests.post(submit_url, json=submission_data, timeout=60) # 发送POST请求提交数据,超时设置为60秒
120
- response.raise_for_status() # 检查HTTP错误
121
- result_data = response.json() # 解析JSON响应
122
- final_status = ( # 构建最终状态消息
123
- f"Submission Successful!\n" # 提交成功
124
- f"User: {result_data.get('username')}\n" # 用户名
125
- f"Overall Score: {result_data.get('score', 'N/A')}% " # 总分
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
- print("Submission successful.") # 打印提交成功
130
- results_df = pd.DataFrame(results_log) # 创建结果数据框
131
- return final_status, results_df # 返回最终状态和结果数据框
132
- except requests.exceptions.HTTPError as e: # 捕获HTTP错误
133
- error_detail = f"Server responded with status {e.response.status_code}." # 服务器响应状态
134
- try: # 尝试
135
- error_json = e.response.json() # 解析错误响应的JSON
136
- error_detail += f" Detail: {error_json.get('detail', e.response.text)}" # 添加错误详情
137
- except requests.exceptions.JSONDecodeError: # 捕获JSON解码错误
138
- error_detail += f" Response: {e.response.text[:500]}" # 添加原始响应文本
139
- status_message = f"Submission Failed: {error_detail}" # 构建提交失败消息
140
- print(status_message) # 打印状态消息
141
- results_df = pd.DataFrame(results_log) # 创建结果数据框
142
- return status_message, results_df # 返回状态消息和结果数据框
143
- except requests.exceptions.Timeout: # 捕获超时异常
144
- status_message = "Submission Failed: The request timed out." # 构建请求超时消息
145
- print(status_message) # 打印状态消息
146
- results_df = pd.DataFrame(results_log) # 创建结果数据框
147
- return status_message, results_df # 返回状态消息和结果数据框
148
- except requests.exceptions.RequestException as e: # 捕获请求异常
149
- status_message = f"Submission Failed: Network error - {e}" # 构建网络错误消息
150
- print(status_message) # 打印状态消息
151
- results_df = pd.DataFrame(results_log) # 创建结果数据框
152
- return status_message, results_df # 返回状态消息和结果数据框
153
- except Exception as e: # 捕获其他异常
154
- status_message = f"An unexpected error occurred during submission: {e}" # 构建意外错误消息
155
- print(status_message) # 打印状态消息
156
- results_df = pd.DataFrame(results_log) # ���建结果数据框
157
- return status_message, results_df # 返回状态消息和结果数据框
158
-
159
- # --- Build Gradio Interface using Blocks ---
160
- with gr.Blocks() as demo: # 创建Gradio Blocks界面
161
- gr.Markdown("# Basic Agent Evaluation Runner") # 添加标题
162
- gr.Markdown( # 添加说明文本
 
163
  """
164
- Modified by niku.... # 由niku修改
165
- **Instructions:** # 指示
166
- 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ... # 请克隆此空间,然后修改代码以定义代理逻辑、工具和必要的包等
167
- 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission. # 使用下面的按钮登录您的Hugging Face账户。这将使用您的HF用户名进行提交
168
- 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score. # 点击"运行评估并提交所有答案"以获取问题,运行您的代理,提交答案,并查看分数
 
169
  ---
170
- **Disclaimers:** # 免责声明
171
- Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions). # 点击"提交"按钮后,可能需要相当长的时间(这是代理回答所有问题所需的时间)
172
- This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async. # 此空间提供了一个基本设置,故意设计成不是最优的,以鼓励您开发自己的更强大的解决方案。例如,对于提交按钮的延迟过程,一个解决方案可能是缓存答案并在单独的操作中提交,或者甚至异步回答问题
 
173
  """
174
  )
175
 
176
- gr.LoginButton() # 添加登录按钮
177
 
178
- run_button = gr.Button("Run Evaluation & Submit All Answers") # 添加运行评估按钮
179
 
180
- status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False) # 添加状态输出文本框
181
- # Removed max_rows=10 from DataFrame constructor # 从DataFrame构造函数中移除了max_rows=10
182
- results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True) # 添加结果表格
183
 
184
- run_button.click( # 设置按钮点击事件
185
- fn=run_and_submit_all, # 点击时运行的函数
186
- outputs=[status_output, results_table] # 输出组件
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 # 启动时检查SPACE_HOST和SPACE_ID信息
192
- space_host_startup = os.getenv("SPACE_HOST") # 获取SPACE_HOST环境变量
193
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup # 获取SPACE_ID环境变量
194
-
195
- if space_host_startup: # 如果找到SPACE_HOST
196
- print(f"✅ SPACE_HOST found: {space_host_startup}") # 打印找到SPACE_HOST
197
- print(f" Runtime URL should be: https://{space_host_startup}.hf.space") # 打印运行时URL
198
- else: # 如果没找到SPACE_HOST
199
- print("ℹ️ SPACE_HOST environment variable not found (running locally?).") # 打印SPACE_HOST环境变量未找到
200
-
201
- if space_id_startup: # Print repo URLs if SPACE_ID is found # 如果找到SPACE_ID,打印代码库URL
202
- print(f"✅ SPACE_ID found: {space_id_startup}") # 打印找到SPACE_ID
203
- print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}") # 打印代码库URL
204
- print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main") # 打印代码库树URL
205
- else: # 如果没找到SPACE_ID
206
- print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.") # 打印SPACE_ID环境变量未找到
207
-
208
- print("-"*(60 + len(" App Starting ")) + "\n") # 打印结束分隔线
209
-
210
- print("Launching Gradio Interface for Basic Agent Evaluation...") # 打印正在启动Gradio界面
211
- demo.launch(debug=True, share=False) # 启动Gradio演示,启用调试模式,不共享
 
1
+ import os
2
+ import gradio as gr
3
+ import requests
4
+ import inspect
5
+ import pandas as pd
6
+
7
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIServerModel, WikipediaSearchTool
8
+
9
+ # (Keep Constants as is)
10
+ # --- Constants ---
11
+ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
12
+
13
+ # --- Basic Agent Definition ---
14
+ # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
15
+ class BasicAgent:
16
+ def __init__(self):
17
+ model = OpenAIServerModel(model_id="deepseek-chat",api_key="sk-04da63f756c9468182d278f52e33ad15", api_base="https://api.deepseek.com")
18
+
19
+ search_tool = DuckDuckGoSearchTool()
20
+ wiki_search = WikipediaSearchTool()
21
+
22
+ self.agent = CodeAgent(
23
+ model = model,
24
+ tools=[
25
+ search_tool,
 
 
 
 
 
 
 
26
  ]
27
  )
28
 
29
+ def __call__(self, question: str) -> str:
30
+ return self.agent.run(question) # type: ignore
31
 
32
+ def run_and_submit_all( profile: gr.OAuthProfile | None):
33
  """
34
  Fetches all questions, runs the BasicAgent on them, submits all answers,
35
  and displays the results.
36
+ """
37
+ # --- Determine HF Space Runtime URL and Repo URL ---
38
+ space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
39
+
40
+ if profile:
41
+ username= f"{profile.username}"
42
+ print(f"User logged in: {username}")
43
+ else:
44
+ print("User not logged in.")
45
+ return "Please Login to Hugging Face with the button.", None
46
+
47
+ api_url = DEFAULT_API_URL
48
+ questions_url = f"{api_url}/questions"
49
+ submit_url = f"{api_url}/submit"
50
+
51
+ # 1. Instantiate Agent ( modify this part to create your agent)
52
+ try:
53
+ agent = BasicAgent()
54
+ except Exception as e:
55
+ print(f"Error instantiating agent: {e}")
56
+ return f"Error initializing agent: {e}", None
57
+ # 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)
58
+ agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
59
+ print(agent_code)
60
+
61
+ # 2. Fetch Questions
62
+ print(f"Fetching questions from: {questions_url}")
63
+ try:
64
+ response = requests.get(questions_url, timeout=15)
65
+ response.raise_for_status()
66
+ questions_data = response.json()
67
+ if not questions_data:
68
+ print("Fetched questions list is empty.")
69
+ return "Fetched questions list is empty or invalid format.", None
70
+ print(f"Fetched {len(questions_data)} questions.")
71
+ except requests.exceptions.RequestException as e:
72
+ print(f"Error fetching questions: {e}")
73
+ return f"Error fetching questions: {e}", None
74
+ except requests.exceptions.JSONDecodeError as e:
75
+ print(f"Error decoding JSON response from questions endpoint: {e}")
76
+ print(f"Response text: {response.text[:500]}")
77
+ return f"Error decoding server response for questions: {e}", None
78
+ except Exception as e:
79
+ print(f"An unexpected error occurred fetching questions: {e}")
80
+ return f"An unexpected error occurred fetching questions: {e}", None
81
+
82
+ # 3. Run your Agent
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 item with missing task_id or question: {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
+ # 4. Prepare Submission
105
+ submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
106
+ status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
107
+ print(status_update)
108
+
109
+ # 5. Submit
110
+ print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
111
+ try:
112
+ response = requests.post(submit_url, json=submission_data, timeout=60)
113
+ response.raise_for_status()
114
+ result_data = response.json()
115
+ final_status = (
116
+ f"Submission Successful!\n"
117
+ f"User: {result_data.get('username')}\n"
118
+ f"Overall Score: {result_data.get('score', 'N/A')}% "
119
+ f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
120
+ f"Message: {result_data.get('message', 'No message received.')}"
121
  )
122
+ print("Submission successful.")
123
+ results_df = pd.DataFrame(results_log)
124
+ return final_status, results_df
125
+ except requests.exceptions.HTTPError as e:
126
+ error_detail = f"Server responded with status {e.response.status_code}."
127
+ try:
128
+ error_json = e.response.json()
129
+ error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
130
+ except requests.exceptions.JSONDecodeError:
131
+ error_detail += f" Response: {e.response.text[:500]}"
132
+ status_message = f"Submission Failed: {error_detail}"
133
+ print(status_message)
134
+ results_df = pd.DataFrame(results_log)
135
+ return status_message, results_df
136
+ except requests.exceptions.Timeout:
137
+ status_message = "Submission Failed: The request timed out."
138
+ print(status_message)
139
+ results_df = pd.DataFrame(results_log)
140
+ return status_message, results_df
141
+ except requests.exceptions.RequestException as e:
142
+ status_message = f"Submission Failed: Network error - {e}"
143
+ print(status_message)
144
+ results_df = pd.DataFrame(results_log)
145
+ return status_message, results_df
146
+ except Exception as e:
147
+ status_message = f"An unexpected error occurred during submission: {e}"
148
+ print(status_message)
149
+ results_df = pd.DataFrame(results_log)
150
+ return status_message, results_df
151
+
152
+
153
+ # --- Build Gradio Interface using Blocks ---
154
+ with gr.Blocks() as demo:
155
+ gr.Markdown("# Basic Agent Evaluation Runner")
156
+ gr.Markdown(
157
  """
158
+ **Instructions:**
159
+
160
+ 1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
161
+ 2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
162
+ 3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
163
+
164
  ---
165
+ **Disclaimers:**
166
+ Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
167
+ This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a seperate action or even to answer the questions in async.
168
+ Please note that this version requires an OpenAI Key to run.
169
  """
170
  )
171
 
172
+ gr.LoginButton()
173
 
174
+ run_button = gr.Button("Run Evaluation & Submit All Answers")
175
 
176
+ status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
177
+ # Removed max_rows=10 from DataFrame constructor
178
+ results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
179
 
180
+ run_button.click(
181
+ fn=run_and_submit_all,
182
+ outputs=[status_output, results_table]
183
  )
184
 
185
+ if __name__ == "__main__":
186
+ print("\n" + "-"*30 + " App Starting " + "-"*30)
187
+ # Check for SPACE_HOST and SPACE_ID at startup for information
188
+ space_host_startup = os.getenv("SPACE_HOST")
189
+ space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
190
+
191
+ if space_host_startup:
192
+ print(f"✅ SPACE_HOST found: {space_host_startup}")
193
+ print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
194
+ else:
195
+ print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
196
+
197
+ if space_id_startup: # Print repo URLs if SPACE_ID is found
198
+ print(f"✅ SPACE_ID found: {space_id_startup}")
199
+ print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
200
+ print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
201
+ else:
202
+ print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
203
+
204
+ print("-"*(60 + len(" App Starting ")) + "\n")
205
+
206
+ print("Launching Gradio Interface for Basic Agent Evaluation...")
207
+ demo.launch(debug=True, share=False)