Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
|
2 |
|
3 |
import os
|
4 |
-
import re
|
5 |
import gradio as gr
|
6 |
import requests
|
7 |
import pandas as pd
|
@@ -9,6 +8,7 @@ import pandas as pd
|
|
9 |
from smolagents import CodeAgent, DuckDuckGoSearchTool
|
10 |
from smolagents.models import OpenAIServerModel
|
11 |
|
|
|
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,42 +17,24 @@ 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 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
class MyAgent:
|
25 |
def __init__(self):
|
26 |
-
self.model =
|
27 |
-
self.agent = CodeAgent(
|
28 |
-
tools=[DuckDuckGoSearchTool()],
|
29 |
-
model=self.model
|
30 |
-
)
|
31 |
|
32 |
def __call__(self, question: str) -> str:
|
33 |
-
|
34 |
-
{"role": "system", "content": SYSTEM_PROMPT},
|
35 |
-
{"role": "user", "content": question}
|
36 |
-
]
|
37 |
-
try:
|
38 |
-
response = self.model(messages)
|
39 |
-
# Extract the actual text from response
|
40 |
-
if isinstance(response, dict):
|
41 |
-
choices = response.get('choices')
|
42 |
-
if choices and len(choices) > 0:
|
43 |
-
text = choices[0].get('message', {}).get('content', '')
|
44 |
-
else:
|
45 |
-
text = ''
|
46 |
-
elif isinstance(response, str):
|
47 |
-
text = response
|
48 |
-
else:
|
49 |
-
text = str(response)
|
50 |
-
|
51 |
-
return extract_final_answer(text)
|
52 |
-
except Exception as e:
|
53 |
-
import traceback
|
54 |
-
traceback.print_exc()
|
55 |
-
return f"AGENT ERROR: {e}"
|
56 |
|
57 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
58 |
space_id = os.getenv("SPACE_ID")
|
@@ -71,22 +53,26 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
71 |
try:
|
72 |
agent = MyAgent()
|
73 |
except Exception as e:
|
|
|
74 |
return f"Error initializing agent: {e}", None
|
75 |
|
76 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
|
|
77 |
|
|
|
78 |
try:
|
79 |
response = requests.get(questions_url, timeout=15)
|
80 |
response.raise_for_status()
|
81 |
questions_data = response.json()
|
82 |
if not questions_data:
|
83 |
return "Fetched questions list is empty or invalid format.", None
|
|
|
84 |
except Exception as e:
|
85 |
return f"Error fetching questions: {e}", None
|
86 |
|
87 |
results_log = []
|
88 |
answers_payload = []
|
89 |
-
|
90 |
for item in questions_data:
|
91 |
task_id = item.get("task_id")
|
92 |
question_text = item.get("question")
|
@@ -97,12 +83,14 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
97 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
98 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
99 |
except Exception as e:
|
100 |
-
|
|
|
101 |
|
102 |
if not answers_payload:
|
103 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
104 |
|
105 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
|
|
106 |
try:
|
107 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
108 |
response.raise_for_status()
|
@@ -117,30 +105,26 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
117 |
results_df = pd.DataFrame(results_log)
|
118 |
return final_status, results_df
|
119 |
except requests.exceptions.HTTPError as e:
|
120 |
-
error_detail = f"Server responded with status {e.response.status_code}."
|
121 |
try:
|
122 |
-
|
123 |
-
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
124 |
except Exception:
|
125 |
-
|
126 |
-
return f"Submission Failed: {
|
127 |
except requests.exceptions.Timeout:
|
128 |
return "Submission Failed: The request timed out.", pd.DataFrame(results_log)
|
129 |
except Exception as e:
|
130 |
return f"An unexpected error occurred during submission: {e}", pd.DataFrame(results_log)
|
131 |
|
|
|
132 |
with gr.Blocks() as demo:
|
133 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
134 |
-
gr.Markdown(
|
135 |
-
"""
|
136 |
**Instructions:**
|
137 |
1. Clone this space, modify code to define your agent's logic, tools, and packages.
|
138 |
2. Log in to your Hugging Face account using the button below.
|
139 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see your score.
|
140 |
-
|
141 |
**Note:** Submitting can take some time.
|
142 |
-
|
143 |
-
)
|
144 |
|
145 |
gr.LoginButton()
|
146 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
@@ -169,7 +153,6 @@ if __name__ == "__main__":
|
|
169 |
print("ℹ️ SPACE_ID environment variable not found (running locally?).")
|
170 |
|
171 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
172 |
-
|
173 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
174 |
demo.launch(debug=True, share=False)
|
175 |
|
|
|
1 |
|
2 |
|
3 |
import os
|
|
|
4 |
import gradio as gr
|
5 |
import requests
|
6 |
import pandas as pd
|
|
|
8 |
from smolagents import CodeAgent, DuckDuckGoSearchTool
|
9 |
from smolagents.models import OpenAIServerModel
|
10 |
|
11 |
+
# Define the system prompt
|
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 |
|
18 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
19 |
|
20 |
+
# Patched model to prepend system prompt correctly
|
21 |
+
class PatchedOpenAIServerModel(OpenAIServerModel):
|
22 |
+
def generate(self, messages, stop_sequences=None, **kwargs):
|
23 |
+
if isinstance(messages, list):
|
24 |
+
if not any(m["role"] == "system" for m in messages):
|
25 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}] + messages
|
26 |
+
else:
|
27 |
+
raise TypeError("Expected 'messages' to be a list of message dicts")
|
28 |
+
|
29 |
+
return super().generate(messages=messages, stop_sequences=stop_sequences, **kwargs)
|
30 |
|
31 |
class MyAgent:
|
32 |
def __init__(self):
|
33 |
+
self.model = PatchedOpenAIServerModel(model_id="gpt-4")
|
34 |
+
self.agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=self.model)
|
|
|
|
|
|
|
35 |
|
36 |
def __call__(self, question: str) -> str:
|
37 |
+
return self.agent.run(question)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
40 |
space_id = os.getenv("SPACE_ID")
|
|
|
53 |
try:
|
54 |
agent = MyAgent()
|
55 |
except Exception as e:
|
56 |
+
print(f"Error initializing agent: {e}")
|
57 |
return f"Error initializing agent: {e}", None
|
58 |
|
59 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
60 |
+
print(f"Agent code URL: {agent_code}")
|
61 |
|
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 |
return "Fetched questions list is empty or invalid format.", None
|
69 |
+
print(f"Fetched {len(questions_data)} questions.")
|
70 |
except Exception as e:
|
71 |
return f"Error fetching questions: {e}", None
|
72 |
|
73 |
results_log = []
|
74 |
answers_payload = []
|
75 |
+
print(f"Running agent on {len(questions_data)} questions...")
|
76 |
for item in questions_data:
|
77 |
task_id = item.get("task_id")
|
78 |
question_text = item.get("question")
|
|
|
83 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
84 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
85 |
except Exception as e:
|
86 |
+
error_msg = f"AGENT ERROR: {e}"
|
87 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": error_msg})
|
88 |
|
89 |
if not answers_payload:
|
90 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
91 |
|
92 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
93 |
+
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
94 |
try:
|
95 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
96 |
response.raise_for_status()
|
|
|
105 |
results_df = pd.DataFrame(results_log)
|
106 |
return final_status, results_df
|
107 |
except requests.exceptions.HTTPError as e:
|
|
|
108 |
try:
|
109 |
+
detail = e.response.json().get("detail", e.response.text)
|
|
|
110 |
except Exception:
|
111 |
+
detail = e.response.text[:500]
|
112 |
+
return f"Submission Failed: {detail}", pd.DataFrame(results_log)
|
113 |
except requests.exceptions.Timeout:
|
114 |
return "Submission Failed: The request timed out.", pd.DataFrame(results_log)
|
115 |
except Exception as e:
|
116 |
return f"An unexpected error occurred during submission: {e}", pd.DataFrame(results_log)
|
117 |
|
118 |
+
# Gradio UI setup
|
119 |
with gr.Blocks() as demo:
|
120 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
121 |
+
gr.Markdown("""
|
|
|
122 |
**Instructions:**
|
123 |
1. Clone this space, modify code to define your agent's logic, tools, and packages.
|
124 |
2. Log in to your Hugging Face account using the button below.
|
125 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see your score.
|
|
|
126 |
**Note:** Submitting can take some time.
|
127 |
+
""")
|
|
|
128 |
|
129 |
gr.LoginButton()
|
130 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
|
|
153 |
print("ℹ️ SPACE_ID environment variable not found (running locally?).")
|
154 |
|
155 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
|
|
156 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
157 |
demo.launch(debug=True, share=False)
|
158 |
|