Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,45 +5,56 @@ import gradio as gr
|
|
5 |
import requests
|
6 |
import pandas as pd
|
7 |
|
|
|
8 |
from smolagents import CodeAgent, DuckDuckGoSearchTool
|
9 |
-
from smolagents.models import
|
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
|
14 |
-
Your answer should be a number OR as few words as possible OR a comma
|
15 |
-
|
16 |
-
|
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(
|
25 |
-
def __init__(self,
|
26 |
-
|
27 |
-
self.
|
|
|
|
|
|
|
|
|
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":
|
33 |
else:
|
34 |
raise TypeError("Expected 'messages' to be a list of message dicts")
|
35 |
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
# Agent
|
39 |
class MyAgent:
|
40 |
def __init__(self):
|
41 |
-
self.model = GeminiFlashModel(
|
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 |
|
@@ -52,7 +63,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
52 |
print(f"User logged in: {username}")
|
53 |
else:
|
54 |
print("User not logged in.")
|
55 |
-
return "Please
|
56 |
|
57 |
api_url = DEFAULT_API_URL
|
58 |
questions_url = f"{api_url}/questions"
|
@@ -61,12 +72,9 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
61 |
try:
|
62 |
agent = MyAgent()
|
63 |
except Exception as e:
|
64 |
-
print(f"Error initializing agent: {e}")
|
65 |
return f"Error initializing agent: {e}", None
|
66 |
|
67 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
68 |
-
print(f"Agent code URL: {agent_code}")
|
69 |
-
|
70 |
print(f"Fetching questions from: {questions_url}")
|
71 |
try:
|
72 |
response = requests.get(questions_url, timeout=15)
|
@@ -80,7 +88,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
80 |
|
81 |
results_log = []
|
82 |
answers_payload = []
|
83 |
-
print(f"Running agent on {len(questions_data)} questions...")
|
84 |
for item in questions_data:
|
85 |
task_id = item.get("task_id")
|
86 |
question_text = item.get("question")
|
@@ -97,8 +104,12 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
97 |
if not answers_payload:
|
98 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
99 |
|
100 |
-
submission_data = {
|
101 |
-
|
|
|
|
|
|
|
|
|
102 |
try:
|
103 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
104 |
response.raise_for_status()
|
@@ -123,15 +134,14 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
123 |
except Exception as e:
|
124 |
return f"An unexpected error occurred during submission: {e}", pd.DataFrame(results_log)
|
125 |
|
126 |
-
# Gradio UI
|
127 |
with gr.Blocks() as demo:
|
128 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
129 |
gr.Markdown("""
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
**Note:** Submitting can take some time.
|
135 |
""")
|
136 |
|
137 |
gr.LoginButton()
|
@@ -143,26 +153,22 @@ with gr.Blocks() as demo:
|
|
143 |
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
144 |
|
145 |
if __name__ == "__main__":
|
146 |
-
print("\n" + "
|
147 |
space_host = os.getenv("SPACE_HOST")
|
148 |
space_id = os.getenv("SPACE_ID")
|
149 |
|
150 |
if space_host:
|
151 |
-
print(f"✅ SPACE_HOST
|
152 |
-
print(f" Runtime URL should be: https://{space_host}.hf.space")
|
153 |
else:
|
154 |
-
print("ℹ️ SPACE_HOST
|
155 |
|
156 |
if space_id:
|
157 |
-
print(f"✅ SPACE_ID
|
158 |
-
print(f" Repo URL: https://huggingface.co/spaces/{space_id}")
|
159 |
-
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id}/tree/main")
|
160 |
else:
|
161 |
-
print("ℹ️ SPACE_ID
|
162 |
|
163 |
-
print("-"*(60 + len(" App Starting ")) + "\n")
|
164 |
-
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
165 |
demo.launch(debug=True, share=False)
|
166 |
|
167 |
|
168 |
|
|
|
|
5 |
import requests
|
6 |
import pandas as pd
|
7 |
|
8 |
+
import google.generativeai as genai
|
9 |
from smolagents import CodeAgent, DuckDuckGoSearchTool
|
10 |
+
from smolagents.models.base import BaseModel
|
11 |
|
12 |
# Define the system prompt
|
13 |
SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question.
|
14 |
+
Report your thoughts, and finish your answer with just the answer — no prefixes like "FINAL ANSWER:".
|
15 |
+
Your answer should be a number OR as few words as possible OR a comma-separated list of numbers and/or strings.
|
16 |
+
If you're asked for a number, don’t use commas or units like $ or %, unless specified.
|
17 |
+
If you're asked for a string, don’t use articles or abbreviations (e.g. for cities), and write digits in plain text unless told otherwise."""
|
|
|
|
|
|
|
18 |
|
19 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
20 |
|
21 |
# Gemini model wrapper
|
22 |
+
class GeminiFlashModel(BaseModel):
|
23 |
+
def __init__(self, model_name="gemini-1.5-flash", api_key=None):
|
24 |
+
self.model_name = model_name
|
25 |
+
self.api_key = api_key or os.getenv("GOOGLE_API_KEY")
|
26 |
+
if not self.api_key:
|
27 |
+
raise ValueError("GOOGLE_API_KEY is not set in environment variables.")
|
28 |
+
genai.configure(api_key=self.api_key)
|
29 |
+
self.model = genai.GenerativeModel(model_name)
|
30 |
|
31 |
def generate(self, messages, stop_sequences=None, **kwargs):
|
32 |
+
# Insert system prompt if missing
|
33 |
if isinstance(messages, list):
|
34 |
if not any(m["role"] == "system" for m in messages):
|
35 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}] + messages
|
36 |
else:
|
37 |
raise TypeError("Expected 'messages' to be a list of message dicts")
|
38 |
|
39 |
+
# Convert messages to a single string (Gemini expects plain prompt)
|
40 |
+
prompt = "\n".join([f"{m['role'].capitalize()}: {m['content']}" for m in messages])
|
41 |
+
|
42 |
+
try:
|
43 |
+
response = self.model.generate_content(prompt)
|
44 |
+
return response.text.strip()
|
45 |
+
except Exception as e:
|
46 |
+
return f"GENERATION ERROR: {e}"
|
47 |
|
48 |
+
# Agent using Gemini-1.5-flash
|
49 |
class MyAgent:
|
50 |
def __init__(self):
|
51 |
+
self.model = GeminiFlashModel(model_name="gemini-1.5-flash")
|
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 |
+
# Evaluation & submission flow
|
58 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
59 |
space_id = os.getenv("SPACE_ID")
|
60 |
|
|
|
63 |
print(f"User logged in: {username}")
|
64 |
else:
|
65 |
print("User not logged in.")
|
66 |
+
return "Please login to Hugging Face with the button.", None
|
67 |
|
68 |
api_url = DEFAULT_API_URL
|
69 |
questions_url = f"{api_url}/questions"
|
|
|
72 |
try:
|
73 |
agent = MyAgent()
|
74 |
except Exception as e:
|
|
|
75 |
return f"Error initializing agent: {e}", None
|
76 |
|
77 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
|
|
|
|
78 |
print(f"Fetching questions from: {questions_url}")
|
79 |
try:
|
80 |
response = requests.get(questions_url, timeout=15)
|
|
|
88 |
|
89 |
results_log = []
|
90 |
answers_payload = []
|
|
|
91 |
for item in questions_data:
|
92 |
task_id = item.get("task_id")
|
93 |
question_text = item.get("question")
|
|
|
104 |
if not answers_payload:
|
105 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
106 |
|
107 |
+
submission_data = {
|
108 |
+
"username": username.strip(),
|
109 |
+
"agent_code": agent_code,
|
110 |
+
"answers": answers_payload
|
111 |
+
}
|
112 |
+
|
113 |
try:
|
114 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
115 |
response.raise_for_status()
|
|
|
134 |
except Exception as e:
|
135 |
return f"An unexpected error occurred during submission: {e}", pd.DataFrame(results_log)
|
136 |
|
137 |
+
# Gradio UI
|
138 |
with gr.Blocks() as demo:
|
139 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
140 |
gr.Markdown("""
|
141 |
+
**Instructions:**
|
142 |
+
1. Clone this space and modify it to define your agent's logic.
|
143 |
+
2. Log in with Hugging Face.
|
144 |
+
3. Click 'Run Evaluation & Submit All Answers' to run and submit.
|
|
|
145 |
""")
|
146 |
|
147 |
gr.LoginButton()
|
|
|
153 |
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
154 |
|
155 |
if __name__ == "__main__":
|
156 |
+
print("\n" + "="*10 + " App Startup " + "="*10)
|
157 |
space_host = os.getenv("SPACE_HOST")
|
158 |
space_id = os.getenv("SPACE_ID")
|
159 |
|
160 |
if space_host:
|
161 |
+
print(f"✅ SPACE_HOST: {space_host} -> https://{space_host}.hf.space")
|
|
|
162 |
else:
|
163 |
+
print("ℹ️ SPACE_HOST not set.")
|
164 |
|
165 |
if space_id:
|
166 |
+
print(f"✅ SPACE_ID: {space_id}")
|
|
|
|
|
167 |
else:
|
168 |
+
print("ℹ️ SPACE_ID not set.")
|
169 |
|
|
|
|
|
170 |
demo.launch(debug=True, share=False)
|
171 |
|
172 |
|
173 |
|
174 |
+
|