Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,56 +3,47 @@ import gradio as gr
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
-
import
|
7 |
from transformers import pipeline
|
8 |
-
|
9 |
# --- Constants ---
|
10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
11 |
|
12 |
-
# ---
|
13 |
-
#
|
14 |
-
|
15 |
-
def __init__(self):
|
16 |
-
print("BasicAgent initialized.")
|
17 |
-
def __call__(self, question: str) -> str:
|
18 |
-
return "This is a default answer."
|
19 |
|
20 |
-
|
|
|
21 |
def __init__(self):
|
22 |
-
|
23 |
-
|
24 |
-
return f"You asked: {question}"
|
25 |
|
26 |
-
class WikipediaAgent:
|
27 |
-
def __init__(self):
|
28 |
-
wikipedia.set_lang("en")
|
29 |
def __call__(self, question: str) -> str:
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
38 |
def __init__(self):
|
39 |
-
|
40 |
-
self.model = pipeline("text2text-generation", model="google/flan-t5-base", device=-1) # CPU only
|
41 |
|
42 |
def __call__(self, question: str) -> str:
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
def run_and_submit_all(agent_name: str, profile: gr.OAuthProfile | None = None):
|
50 |
"""
|
51 |
-
Fetches all questions, runs the
|
52 |
and displays the results.
|
53 |
"""
|
54 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
55 |
-
space_id = os.getenv("SPACE_ID")
|
56 |
|
57 |
if profile:
|
58 |
username= f"{profile.username}"
|
@@ -65,22 +56,12 @@ def run_and_submit_all(agent_name: str, profile: gr.OAuthProfile | None = None):
|
|
65 |
questions_url = f"{api_url}/questions"
|
66 |
submit_url = f"{api_url}/submit"
|
67 |
|
68 |
-
# 1. Instantiate Agent (
|
69 |
try:
|
70 |
-
|
71 |
-
agent = BasicAgent()
|
72 |
-
elif agent_name == "EchoAgent":
|
73 |
-
agent = EchoAgent()
|
74 |
-
elif agent_name == "WikipediaAgent":
|
75 |
-
agent = WikipediaAgent()
|
76 |
-
elif agent_name == "TransformersAgent":
|
77 |
-
agent = TransformersAgent()
|
78 |
-
else:
|
79 |
-
return f"Unknown agent selected: {agent_name}", None
|
80 |
except Exception as e:
|
81 |
print(f"Error instantiating agent: {e}")
|
82 |
return f"Error initializing agent: {e}", None
|
83 |
-
# In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
|
84 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
85 |
print(agent_code)
|
86 |
|
@@ -91,8 +72,8 @@ def run_and_submit_all(agent_name: str, profile: gr.OAuthProfile | None = None):
|
|
91 |
response.raise_for_status()
|
92 |
questions_data = response.json()
|
93 |
if not questions_data:
|
94 |
-
|
95 |
-
|
96 |
print(f"Fetched {len(questions_data)} questions.")
|
97 |
except requests.exceptions.RequestException as e:
|
98 |
print(f"Error fetching questions: {e}")
|
@@ -127,7 +108,7 @@ def run_and_submit_all(agent_name: str, profile: gr.OAuthProfile | None = None):
|
|
127 |
print("Agent did not produce any answers to submit.")
|
128 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
129 |
|
130 |
-
# 4. Prepare Submission
|
131 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
132 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
133 |
print(status_update)
|
@@ -178,46 +159,30 @@ def run_and_submit_all(agent_name: str, profile: gr.OAuthProfile | None = None):
|
|
178 |
|
179 |
# --- Build Gradio Interface using Blocks ---
|
180 |
with gr.Blocks() as demo:
|
181 |
-
gr.Markdown("#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
182 |
|
183 |
gr.LoginButton()
|
184 |
-
|
185 |
-
agent_dropdown = gr.Dropdown(
|
186 |
-
choices=["BasicAgent", "EchoAgent", "WikipediaAgent", "TransformersAgent"],
|
187 |
-
label="Select Agent",
|
188 |
-
value="BasicAgent"
|
189 |
-
)
|
190 |
|
191 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
192 |
-
|
193 |
-
|
|
|
194 |
|
195 |
run_button.click(
|
196 |
fn=run_and_submit_all,
|
197 |
-
inputs=[agent_dropdown],
|
198 |
outputs=[status_output, results_table]
|
199 |
)
|
200 |
|
201 |
if __name__ == "__main__":
|
202 |
-
print("
|
203 |
-
|
204 |
-
space_host_startup = os.getenv("SPACE_HOST")
|
205 |
-
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
206 |
-
|
207 |
-
if space_host_startup:
|
208 |
-
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
209 |
-
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
210 |
-
else:
|
211 |
-
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
212 |
-
|
213 |
-
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
214 |
-
print(f"✅ SPACE_ID found: {space_id_startup}")
|
215 |
-
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
216 |
-
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
217 |
-
else:
|
218 |
-
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
219 |
-
|
220 |
-
print("-"*(60 + len(" App Starting ")) + "\n")
|
221 |
-
|
222 |
-
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
223 |
-
demo.launch(debug=True, share=False)
|
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
+
from smolagent.agents import SimpleAgent
|
7 |
from transformers import pipeline
|
8 |
+
|
9 |
# --- Constants ---
|
10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
11 |
|
12 |
+
# --- FLAN-T5 Agent Definition ---
|
13 |
+
# Load the FLAN-T5 base model for question-answering
|
14 |
+
qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-base")
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
# Define a tool that wraps the FLAN-T5 model
|
17 |
+
class FlanT5Tool:
|
18 |
def __init__(self):
|
19 |
+
self.name = "flan_t5_qa"
|
20 |
+
self.description = "Answers questions using FLAN-T5 base."
|
|
|
21 |
|
|
|
|
|
|
|
22 |
def __call__(self, question: str) -> str:
|
23 |
+
print(f"FlanT5Tool received: {question}")
|
24 |
+
response = qa_pipeline(question, max_new_tokens=100)
|
25 |
+
return response[0]['generated_text']
|
26 |
+
|
27 |
+
# Instantiate the tool
|
28 |
+
flan_tool = FlanT5Tool()
|
29 |
+
|
30 |
+
# Create a simple agent with the FLAN-T5 tool
|
31 |
+
class SmolAgentWrapper:
|
32 |
def __init__(self):
|
33 |
+
self.agent = SimpleAgent(tools=[flan_tool])
|
|
|
34 |
|
35 |
def __call__(self, question: str) -> str:
|
36 |
+
print(f"SmolAgentWrapper received: {question}")
|
37 |
+
return self.agent.run(question)
|
38 |
+
|
39 |
+
# --- Run and Submit Logic ---
|
40 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
|
|
41 |
"""
|
42 |
+
Fetches all questions, runs the agent on them, submits all answers,
|
43 |
and displays the results.
|
44 |
"""
|
45 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
46 |
+
space_id = os.getenv("SPACE_ID")
|
47 |
|
48 |
if profile:
|
49 |
username= f"{profile.username}"
|
|
|
56 |
questions_url = f"{api_url}/questions"
|
57 |
submit_url = f"{api_url}/submit"
|
58 |
|
59 |
+
# 1. Instantiate Agent (with FLAN-T5)
|
60 |
try:
|
61 |
+
agent = SmolAgentWrapper()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
except Exception as e:
|
63 |
print(f"Error instantiating agent: {e}")
|
64 |
return f"Error initializing agent: {e}", None
|
|
|
65 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
66 |
print(agent_code)
|
67 |
|
|
|
72 |
response.raise_for_status()
|
73 |
questions_data = response.json()
|
74 |
if not questions_data:
|
75 |
+
print("Fetched questions list is empty.")
|
76 |
+
return "Fetched questions list is empty or invalid format.", None
|
77 |
print(f"Fetched {len(questions_data)} questions.")
|
78 |
except requests.exceptions.RequestException as e:
|
79 |
print(f"Error fetching questions: {e}")
|
|
|
108 |
print("Agent did not produce any answers to submit.")
|
109 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
110 |
|
111 |
+
# 4. Prepare Submission
|
112 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
113 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
114 |
print(status_update)
|
|
|
159 |
|
160 |
# --- Build Gradio Interface using Blocks ---
|
161 |
with gr.Blocks() as demo:
|
162 |
+
gr.Markdown("# Smol Agent Evaluation Runner")
|
163 |
+
gr.Markdown(
|
164 |
+
"""
|
165 |
+
**Instructions:**
|
166 |
+
1. Log in to your Hugging Face account using the button below.
|
167 |
+
2. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
168 |
+
---
|
169 |
+
**Disclaimers:**
|
170 |
+
This space uses a small agent powered by FLAN-T5 base for answering questions.
|
171 |
+
"""
|
172 |
+
)
|
173 |
|
174 |
gr.LoginButton()
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
|
176 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
177 |
+
|
178 |
+
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
179 |
+
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
180 |
|
181 |
run_button.click(
|
182 |
fn=run_and_submit_all,
|
|
|
183 |
outputs=[status_output, results_table]
|
184 |
)
|
185 |
|
186 |
if __name__ == "__main__":
|
187 |
+
print("Launching Gradio Interface for Smol Agent Evaluation...")
|
188 |
+
demo.launch(debug=True, share=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|