Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,71 +4,51 @@ import os
|
|
4 |
import gradio as gr
|
5 |
import requests
|
6 |
import pandas as pd
|
7 |
-
import re
|
8 |
|
9 |
from smolagents import CodeAgent, DuckDuckGoSearchTool
|
10 |
-
from smolagents.models import
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
FINAL ANSWER: [YOUR FINAL ANSWER]
|
15 |
-
|
16 |
-
Answer rules:
|
17 |
-
- Numbers: no commas, units, or extra words. Just digits.
|
18 |
-
- Strings: lowercase, no articles or abbreviations.
|
19 |
-
- Lists: comma-separated, following the above.
|
20 |
-
|
21 |
-
Examples:
|
22 |
-
Q: What is 12 + 7?
|
23 |
-
A: 12 + 7 = 19
|
24 |
-
FINAL ANSWER: 19
|
25 |
-
|
26 |
-
Q: Name three European capital cities.
|
27 |
-
A: They are Amsterdam, Berlin, and Rome.
|
28 |
-
FINAL ANSWER: amsterdam, berlin, rome
|
29 |
-
|
30 |
-
Q: What is the square root of 81?
|
31 |
-
A: \u221a81 = 9
|
32 |
-
FINAL ANSWER: 9
|
33 |
-
|
34 |
-
Now answer the following:
|
35 |
-
"""
|
36 |
|
37 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
38 |
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
40 |
def generate(self, messages, stop_sequences=None, **kwargs):
|
41 |
if isinstance(messages, list):
|
42 |
if not any(m["role"] == "system" for m in messages):
|
43 |
-
messages = [{"role": "system", "content":
|
44 |
else:
|
45 |
raise TypeError("Expected 'messages' to be a list of message dicts")
|
46 |
|
47 |
return super().generate(messages=messages, stop_sequences=stop_sequences, **kwargs)
|
48 |
|
|
|
49 |
class MyAgent:
|
50 |
def __init__(self):
|
51 |
-
self.model =
|
52 |
self.agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=self.model)
|
53 |
|
54 |
def __call__(self, question: str) -> str:
|
55 |
return self.agent.run(question)
|
56 |
|
57 |
-
def extract_final_answer(output: str) -> str:
|
58 |
-
if "FINAL ANSWER:" in output:
|
59 |
-
return output.split("FINAL ANSWER:")[-1].strip().rstrip('.')
|
60 |
-
return output.strip()
|
61 |
-
|
62 |
-
def sanitize_answer(ans: str) -> str:
|
63 |
-
ans = re.sub(r'\$|%|,', '', ans)
|
64 |
-
ans = ans.strip().rstrip('.')
|
65 |
-
return ans
|
66 |
-
|
67 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
68 |
space_id = os.getenv("SPACE_ID")
|
69 |
|
70 |
if profile:
|
71 |
-
username = profile.username
|
72 |
print(f"User logged in: {username}")
|
73 |
else:
|
74 |
print("User not logged in.")
|
@@ -107,9 +87,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
107 |
if not task_id or question_text is None:
|
108 |
continue
|
109 |
try:
|
110 |
-
|
111 |
-
extracted = extract_final_answer(raw_output)
|
112 |
-
submitted_answer = sanitize_answer(extracted)
|
113 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
114 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
115 |
except Exception as e:
|
@@ -119,7 +97,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
119 |
if not answers_payload:
|
120 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
121 |
|
122 |
-
submission_data = {"username": username, "agent_code": agent_code, "answers": answers_payload}
|
123 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
124 |
try:
|
125 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
@@ -145,6 +123,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
145 |
except Exception as e:
|
146 |
return f"An unexpected error occurred during submission: {e}", pd.DataFrame(results_log)
|
147 |
|
|
|
148 |
with gr.Blocks() as demo:
|
149 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
150 |
gr.Markdown("""
|
|
|
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 LocalModel
|
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 concisely.
|
14 |
+
Your answer should be a number OR as few words as possible OR a comma separated list
|
15 |
+
of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such
|
16 |
+
as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations
|
17 |
+
(e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list,
|
18 |
+
apply the above rules depending of whether the element to be put in the list is a number or a string."""
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
22 |
|
23 |
+
# Gemini model wrapper
|
24 |
+
class GeminiFlashModel(LocalModel):
|
25 |
+
def __init__(self, model_id="gemini-2.0-flash", api_url=None):
|
26 |
+
super().__init__(model_id=model_id, api_url=api_url)
|
27 |
+
self.system_prompt = SYSTEM_PROMPT
|
28 |
+
|
29 |
def generate(self, messages, stop_sequences=None, **kwargs):
|
30 |
if isinstance(messages, list):
|
31 |
if not any(m["role"] == "system" for m in messages):
|
32 |
+
messages = [{"role": "system", "content": self.system_prompt}] + messages
|
33 |
else:
|
34 |
raise TypeError("Expected 'messages' to be a list of message dicts")
|
35 |
|
36 |
return super().generate(messages=messages, stop_sequences=stop_sequences, **kwargs)
|
37 |
|
38 |
+
# Agent definition
|
39 |
class MyAgent:
|
40 |
def __init__(self):
|
41 |
+
self.model = GeminiFlashModel(model_id="gemini-2.0-flash")
|
42 |
self.agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=self.model)
|
43 |
|
44 |
def __call__(self, question: str) -> str:
|
45 |
return self.agent.run(question)
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
48 |
space_id = os.getenv("SPACE_ID")
|
49 |
|
50 |
if profile:
|
51 |
+
username = profile.username
|
52 |
print(f"User logged in: {username}")
|
53 |
else:
|
54 |
print("User not logged in.")
|
|
|
87 |
if not task_id or question_text is None:
|
88 |
continue
|
89 |
try:
|
90 |
+
submitted_answer = agent(question_text)
|
|
|
|
|
91 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
92 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
93 |
except Exception as e:
|
|
|
97 |
if not answers_payload:
|
98 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
99 |
|
100 |
+
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
101 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
102 |
try:
|
103 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
|
|
123 |
except Exception as e:
|
124 |
return f"An unexpected error occurred during submission: {e}", pd.DataFrame(results_log)
|
125 |
|
126 |
+
# Gradio UI setup
|
127 |
with gr.Blocks() as demo:
|
128 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
129 |
gr.Markdown("""
|