Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,12 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
-
import inspect
|
5 |
import pandas as pd
|
6 |
|
7 |
from smolagents import (
|
8 |
ToolCallingAgent,
|
9 |
CodeAgent,
|
10 |
DuckDuckGoSearchTool,
|
11 |
-
InferenceClientModel,
|
12 |
-
HfApiModel,
|
13 |
OpenAIServerModel
|
14 |
)
|
15 |
|
@@ -30,24 +27,21 @@ class BasicAgent:
|
|
30 |
add_base_tools=True
|
31 |
)
|
32 |
|
|
|
33 |
self.agent.prompt_templates['system_prompt'] = """You are a general AI assistant. I will ask you a question.
|
34 |
-
Your primary goal is to answer the question
|
35 |
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.
|
36 |
If you are providing a direct answer without using any tools, your response content MUST be a single string.
|
37 |
-
|
|
|
|
|
38 |
Finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER].
|
39 |
-
YOUR FINAL ANSWER
|
40 |
-
If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise.
|
41 |
-
If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
|
42 |
-
If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
|
43 |
"""
|
44 |
-
|
45 |
-
|
46 |
def __call__(self, question: str) -> str:
|
47 |
-
#
|
48 |
-
|
49 |
-
# 移除打印语句
|
50 |
-
return fixed_answer
|
51 |
|
52 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
53 |
"""
|
@@ -55,132 +49,89 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
55 |
and displays the results.
|
56 |
"""
|
57 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
58 |
-
space_id = os.getenv("SPACE_ID")
|
59 |
|
60 |
if profile:
|
61 |
-
username= f"{profile.username}"
|
62 |
-
# 移除打印语句
|
63 |
else:
|
64 |
-
|
65 |
-
return "Please Login to Hugging Face with the button.", None
|
66 |
|
67 |
api_url = DEFAULT_API_URL
|
68 |
questions_url = f"{api_url}/questions"
|
69 |
submit_url = f"{api_url}/submit"
|
70 |
|
71 |
-
# 1.
|
72 |
try:
|
73 |
agent = BasicAgent()
|
74 |
except Exception as e:
|
75 |
-
|
76 |
-
|
77 |
-
# In the case of an app running as a hugging Face space, this link points toward your codebase
|
78 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
79 |
-
# 移除打印语句
|
80 |
|
81 |
-
# 2.
|
82 |
-
# 移除打印语句
|
83 |
try:
|
84 |
response = requests.get(questions_url, timeout=15)
|
85 |
response.raise_for_status()
|
86 |
questions_data = response.json()
|
87 |
if not questions_data:
|
88 |
-
|
89 |
-
return "Fetched questions list is empty or invalid format.", None
|
90 |
-
# 移除打印语句
|
91 |
-
except requests.exceptions.RequestException as e:
|
92 |
-
# 移除打印语句
|
93 |
-
return f"Error fetching questions: {e}", None
|
94 |
-
except requests.exceptions.JSONDecodeError as e:
|
95 |
-
# 移除打印语句
|
96 |
-
# 移除打印语句
|
97 |
-
return f"Error decoding server response for questions: {e}", None
|
98 |
except Exception as e:
|
99 |
-
|
100 |
-
return f"An unexpected error occurred fetching questions: {e}", None
|
101 |
|
102 |
-
# 3.
|
103 |
results_log = []
|
104 |
answers_payload = []
|
105 |
-
|
106 |
for i, item in enumerate(questions_data):
|
107 |
task_id = item.get("task_id")
|
108 |
question_text = item.get("question")
|
109 |
if not task_id or question_text is None:
|
110 |
-
# 移除打印语句
|
111 |
continue
|
112 |
try:
|
113 |
submitted_answer = agent(question_text)
|
114 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
115 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
116 |
except Exception as e:
|
117 |
-
|
118 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
119 |
-
|
120 |
|
121 |
if not answers_payload:
|
122 |
-
|
123 |
-
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
124 |
|
125 |
-
# 4.
|
126 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
127 |
-
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
128 |
-
# 移除打印语句
|
129 |
|
130 |
-
# 5.
|
131 |
-
# 移除打印语句
|
132 |
try:
|
133 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
134 |
response.raise_for_status()
|
135 |
result_data = response.json()
|
136 |
final_status = (
|
137 |
-
f"
|
138 |
-
f"
|
139 |
-
f"
|
140 |
-
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')}
|
141 |
-
f"
|
142 |
)
|
143 |
-
# 移除打印语句
|
144 |
results_df = pd.DataFrame(results_log)
|
145 |
return final_status, results_df
|
146 |
-
except requests.exceptions.HTTPError as e:
|
147 |
-
error_detail = f"Server responded with status {e.response.status_code}."
|
148 |
-
try:
|
149 |
-
error_json = e.response.json()
|
150 |
-
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
151 |
-
except requests.exceptions.JSONDecodeError:
|
152 |
-
error_detail += f" Response: {e.response.text[:500]}"
|
153 |
-
status_message = f"Submission Failed: {error_detail}"
|
154 |
-
# 移除打印语句
|
155 |
-
results_df = pd.DataFrame(results_log)
|
156 |
-
return status_message, results_df
|
157 |
-
except requests.exceptions.Timeout:
|
158 |
-
status_message = "Submission Failed: The request timed out."
|
159 |
-
# 移除打印语句
|
160 |
-
results_df = pd.DataFrame(results_log)
|
161 |
-
return status_message, results_df
|
162 |
-
except requests.exceptions.RequestException as e:
|
163 |
-
status_message = f"Submission Failed: Network error - {e}"
|
164 |
-
# 移除打印语句
|
165 |
-
results_df = pd.DataFrame(results_log)
|
166 |
-
return status_message, results_df
|
167 |
except Exception as e:
|
168 |
-
status_message = f"
|
169 |
-
# 移除打印语句
|
170 |
results_df = pd.DataFrame(results_log)
|
171 |
return status_message, results_df
|
172 |
|
173 |
|
|
|
174 |
# --- Build Gradio Interface using Blocks ---
|
175 |
with gr.Blocks() as demo:
|
176 |
-
gr.Markdown("#
|
177 |
gr.Markdown(
|
178 |
"""
|
179 |
**Instructions:**
|
180 |
|
181 |
-
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
182 |
-
2.
|
183 |
-
3.
|
|
|
184 |
|
185 |
---
|
186 |
**Disclaimers:**
|
@@ -202,5 +153,25 @@ with gr.Blocks() as demo:
|
|
202 |
)
|
203 |
|
204 |
if __name__ == "__main__":
|
205 |
-
|
206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
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 |
"""
|
|
|
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:**
|
|
|
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}")
|
163 |
+
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
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)
|