Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,132 +4,173 @@ import os
|
|
4 |
import gradio as gr
|
5 |
import requests
|
6 |
import pandas as pd
|
|
|
7 |
from smolagents import CodeAgent, DuckDuckGoSearchTool
|
8 |
from smolagents.models import OpenAIServerModel
|
9 |
-
import openai
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
16 |
|
17 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
18 |
-
OPENAI_MODEL_ID = os.getenv("OPENAI_MODEL_ID", "gpt-4o")
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
""
|
|
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
return f"✅ Logged in as: {profile['username']}"
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
46 |
|
47 |
-
username = login_info["username"]
|
48 |
-
space_id = os.getenv("SPACE_ID", "")
|
49 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
|
|
50 |
|
|
|
51 |
try:
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
55 |
except Exception as e:
|
56 |
-
|
|
|
57 |
|
58 |
-
|
59 |
-
|
|
|
|
|
60 |
task_id = item.get("task_id")
|
61 |
-
|
62 |
-
if not task_id or
|
|
|
63 |
continue
|
64 |
-
prompt = answer_formatting_prompt.strip() + f"\n\nQUESTION: {question.strip()}"
|
65 |
try:
|
66 |
-
|
|
|
|
|
67 |
except Exception as e:
|
68 |
-
|
69 |
-
|
70 |
-
payload.append({"task_id": task_id, "submitted_answer": answer})
|
71 |
|
72 |
-
if not
|
73 |
-
|
|
|
74 |
|
|
|
|
|
75 |
try:
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
attempted = result.get("total_attempted", "?")
|
86 |
-
message = result.get("message", "")
|
87 |
-
return (
|
88 |
-
f"✅ Submission Successful!\nUser: {username}\nScore: {score}% "
|
89 |
-
f"({correct}/{attempted})\nMessage: {message}",
|
90 |
-
pd.DataFrame(results)
|
91 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
except Exception as e:
|
93 |
-
|
94 |
-
|
|
|
95 |
|
96 |
with gr.Blocks() as demo:
|
97 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
|
|
103 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
104 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
105 |
|
106 |
-
|
107 |
-
login_button.click(fn=show_profile, inputs=[login_button], outputs=[login_status])
|
108 |
-
|
109 |
-
# Run evaluation on click, pass login_button's state as input
|
110 |
-
run_button.click(fn=run_and_submit_all, inputs=[login_button], outputs=[status_output, results_table])
|
111 |
-
|
112 |
|
113 |
if __name__ == "__main__":
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
|
|
|
|
133 |
|
134 |
|
135 |
|
|
|
4 |
import gradio as gr
|
5 |
import requests
|
6 |
import pandas as pd
|
7 |
+
|
8 |
from smolagents import CodeAgent, DuckDuckGoSearchTool
|
9 |
from smolagents.models import OpenAIServerModel
|
|
|
10 |
|
11 |
+
# System prompt as per your instructions
|
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].
|
15 |
+
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list
|
16 |
+
of numbers and/or strings. 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. 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. 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."""
|
17 |
|
18 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
19 |
|
20 |
+
class MyAgent:
|
21 |
+
def __init__(self):
|
22 |
+
# Initialize model with system prompt
|
23 |
+
self.model = OpenAIServerModel(
|
24 |
+
model_id="gpt-4",
|
25 |
+
system_message=SYSTEM_PROMPT
|
26 |
+
)
|
27 |
+
self.agent = CodeAgent(
|
28 |
+
tools=[DuckDuckGoSearchTool()],
|
29 |
+
model=self.model
|
30 |
+
)
|
31 |
|
32 |
+
def __call__(self, question: str) -> str:
|
33 |
+
# Run agent on the question
|
34 |
+
return self.agent.run(question)
|
35 |
|
36 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
37 |
+
"""
|
38 |
+
Fetches questions, runs the agent, submits answers, returns status and results table.
|
39 |
+
"""
|
40 |
+
space_id = os.getenv("SPACE_ID")
|
41 |
|
42 |
+
if profile:
|
43 |
+
username = profile.username
|
44 |
+
print(f"User logged in: {username}")
|
45 |
+
else:
|
46 |
+
print("User not logged in.")
|
47 |
+
return "Please Login to Hugging Face with the button.", None
|
48 |
|
49 |
+
api_url = DEFAULT_API_URL
|
50 |
+
questions_url = f"{api_url}/questions"
|
51 |
+
submit_url = f"{api_url}/submit"
|
|
|
52 |
|
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 |
+
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 Exception as e:
|
72 |
+
print(f"Error fetching questions: {e}")
|
73 |
+
return f"Error fetching questions: {e}", None
|
74 |
|
75 |
+
results_log = []
|
76 |
+
answers_payload = []
|
77 |
+
print(f"Running agent on {len(questions_data)} questions...")
|
78 |
+
for item in questions_data:
|
79 |
task_id = item.get("task_id")
|
80 |
+
question_text = item.get("question")
|
81 |
+
if not task_id or question_text is None:
|
82 |
+
print(f"Skipping invalid item: {item}")
|
83 |
continue
|
|
|
84 |
try:
|
85 |
+
submitted_answer = agent(question_text)
|
86 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
87 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
88 |
except Exception as e:
|
89 |
+
print(f"Error running agent on task {task_id}: {e}")
|
90 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
|
|
91 |
|
92 |
+
if not answers_payload:
|
93 |
+
print("Agent did not produce any answers to submit.")
|
94 |
+
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
95 |
|
96 |
+
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
97 |
+
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
98 |
try:
|
99 |
+
response = requests.post(submit_url, json=submission_data, timeout=60)
|
100 |
+
response.raise_for_status()
|
101 |
+
result_data = response.json()
|
102 |
+
final_status = (
|
103 |
+
f"Submission Successful!\n"
|
104 |
+
f"User: {result_data.get('username')}\n"
|
105 |
+
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
106 |
+
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
107 |
+
f"Message: {result_data.get('message', 'No message received.')}"
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
)
|
109 |
+
print("Submission successful.")
|
110 |
+
results_df = pd.DataFrame(results_log)
|
111 |
+
return final_status, results_df
|
112 |
+
except requests.exceptions.HTTPError as e:
|
113 |
+
error_detail = f"Server responded with status {e.response.status_code}."
|
114 |
+
try:
|
115 |
+
error_json = e.response.json()
|
116 |
+
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
117 |
+
except Exception:
|
118 |
+
error_detail += f" Response: {e.response.text[:500]}"
|
119 |
+
status_message = f"Submission Failed: {error_detail}"
|
120 |
+
print(status_message)
|
121 |
+
return status_message, pd.DataFrame(results_log)
|
122 |
+
except requests.exceptions.Timeout:
|
123 |
+
status_message = "Submission Failed: The request timed out."
|
124 |
+
print(status_message)
|
125 |
+
return status_message, pd.DataFrame(results_log)
|
126 |
except Exception as e:
|
127 |
+
status_message = f"An unexpected error occurred during submission: {e}"
|
128 |
+
print(status_message)
|
129 |
+
return status_message, pd.DataFrame(results_log)
|
130 |
|
131 |
with gr.Blocks() as demo:
|
132 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
133 |
+
gr.Markdown(
|
134 |
+
"""
|
135 |
+
**Instructions:**
|
136 |
+
1. Clone this space, modify code to define your agent's logic, tools, and packages.
|
137 |
+
2. Log in to your Hugging Face account using the button below.
|
138 |
+
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see your score.
|
139 |
+
|
140 |
+
**Note:** Submitting can take some time.
|
141 |
+
"""
|
142 |
+
)
|
143 |
+
|
144 |
+
gr.LoginButton()
|
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(fn=run_and_submit_all, outputs=[status_output, results_table])
|
|
|
|
|
|
|
|
|
|
|
151 |
|
152 |
if __name__ == "__main__":
|
153 |
+
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
154 |
+
space_host = os.getenv("SPACE_HOST")
|
155 |
+
space_id = os.getenv("SPACE_ID")
|
156 |
+
|
157 |
+
if space_host:
|
158 |
+
print(f"✅ SPACE_HOST found: {space_host}")
|
159 |
+
print(f" Runtime URL should be: https://{space_host}.hf.space")
|
160 |
+
else:
|
161 |
+
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
162 |
+
|
163 |
+
if space_id:
|
164 |
+
print(f"✅ SPACE_ID found: {space_id}")
|
165 |
+
print(f" Repo URL: https://huggingface.co/spaces/{space_id}")
|
166 |
+
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id}/tree/main")
|
167 |
+
else:
|
168 |
+
print("ℹ️ SPACE_ID environment variable not found (running locally?).")
|
169 |
+
|
170 |
+
print("-"*(60 + len(" App Starting ")) + "\n")
|
171 |
+
|
172 |
+
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
173 |
+
demo.launch(debug=True, share=False)
|
174 |
|
175 |
|
176 |
|