Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,52 +1,29 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
-
import inspect
|
5 |
import pandas as pd
|
6 |
-
from
|
7 |
-
from transformers import pipeline
|
8 |
|
9 |
# --- Constants ---
|
10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
11 |
|
12 |
-
# ---
|
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.
|
|
|
|
|
34 |
|
35 |
def __call__(self, question: str) -> str:
|
36 |
-
print(f"SmolAgentWrapper received: {question}")
|
37 |
return self.agent.run(question)
|
38 |
|
39 |
-
|
|
|
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}"
|
50 |
print(f"User logged in: {username}")
|
51 |
else:
|
52 |
print("User not logged in.")
|
@@ -56,65 +33,51 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
56 |
questions_url = f"{api_url}/questions"
|
57 |
submit_url = f"{api_url}/submit"
|
58 |
|
59 |
-
#
|
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 |
|
68 |
-
#
|
69 |
-
print(f"Fetching questions from: {questions_url}")
|
70 |
try:
|
71 |
response = requests.get(questions_url, timeout=15)
|
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}")
|
80 |
-
return f"Error fetching questions: {e}", None
|
81 |
-
except requests.exceptions.JSONDecodeError as e:
|
82 |
-
print(f"Error decoding JSON response from questions endpoint: {e}")
|
83 |
-
print(f"Response text: {response.text[:500]}")
|
84 |
-
return f"Error decoding server response for questions: {e}", None
|
85 |
except Exception as e:
|
86 |
-
|
87 |
-
return f"An unexpected error occurred fetching questions: {e}", None
|
88 |
|
89 |
-
#
|
90 |
results_log = []
|
91 |
answers_payload = []
|
92 |
-
|
93 |
for item in questions_data:
|
94 |
task_id = item.get("task_id")
|
95 |
question_text = item.get("question")
|
96 |
if not task_id or question_text is None:
|
97 |
-
print(f"Skipping item with missing task_id or question: {item}")
|
98 |
continue
|
99 |
try:
|
100 |
submitted_answer = agent(question_text)
|
101 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
102 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
103 |
except Exception as e:
|
104 |
-
|
105 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
106 |
|
107 |
if not answers_payload:
|
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 |
-
#
|
112 |
-
submission_data = {
|
113 |
-
|
114 |
-
|
|
|
|
|
115 |
|
116 |
-
# 5. Submit
|
117 |
-
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
118 |
try:
|
119 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
120 |
response.raise_for_status()
|
@@ -126,55 +89,28 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
126 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
127 |
f"Message: {result_data.get('message', 'No message received.')}"
|
128 |
)
|
129 |
-
print("Submission successful.")
|
130 |
results_df = pd.DataFrame(results_log)
|
131 |
return final_status, results_df
|
132 |
-
except requests.exceptions.HTTPError as e:
|
133 |
-
error_detail = f"Server responded with status {e.response.status_code}."
|
134 |
-
try:
|
135 |
-
error_json = e.response.json()
|
136 |
-
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
137 |
-
except requests.exceptions.JSONDecodeError:
|
138 |
-
error_detail += f" Response: {e.response.text[:500]}"
|
139 |
-
status_message = f"Submission Failed: {error_detail}"
|
140 |
-
print(status_message)
|
141 |
-
results_df = pd.DataFrame(results_log)
|
142 |
-
return status_message, results_df
|
143 |
-
except requests.exceptions.Timeout:
|
144 |
-
status_message = "Submission Failed: The request timed out."
|
145 |
-
print(status_message)
|
146 |
-
results_df = pd.DataFrame(results_log)
|
147 |
-
return status_message, results_df
|
148 |
-
except requests.exceptions.RequestException as e:
|
149 |
-
status_message = f"Submission Failed: Network error - {e}"
|
150 |
-
print(status_message)
|
151 |
-
results_df = pd.DataFrame(results_log)
|
152 |
-
return status_message, results_df
|
153 |
except Exception as e:
|
154 |
-
|
155 |
-
print(status_message)
|
156 |
-
results_df = pd.DataFrame(results_log)
|
157 |
-
return status_message, results_df
|
158 |
|
159 |
|
160 |
-
# ---
|
161 |
with gr.Blocks() as demo:
|
162 |
-
gr.Markdown("#
|
163 |
gr.Markdown(
|
164 |
"""
|
165 |
**Instructions:**
|
166 |
-
1.
|
167 |
-
2.
|
168 |
-
|
169 |
-
|
170 |
-
|
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 |
|
@@ -184,5 +120,7 @@ with gr.Blocks() as demo:
|
|
184 |
)
|
185 |
|
186 |
if __name__ == "__main__":
|
187 |
-
print("
|
|
|
|
|
188 |
demo.launch(debug=True, share=False)
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import requests
|
|
|
4 |
import pandas as pd
|
5 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel
|
|
|
6 |
|
7 |
# --- Constants ---
|
8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
9 |
|
10 |
+
# --- Define Agent ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
class SmolAgentWrapper:
|
12 |
def __init__(self):
|
13 |
+
self.model = InferenceClientModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct", provider="together") # Uses flan-t5-base via HF Inference API
|
14 |
+
self.tools = [DuckDuckGoSearchTool()] # You can add more tools here
|
15 |
+
self.agent = CodeAgent(model=self.model, tools=self.tools)
|
16 |
|
17 |
def __call__(self, question: str) -> str:
|
|
|
18 |
return self.agent.run(question)
|
19 |
|
20 |
+
|
21 |
+
# --- Evaluation Logic ---
|
22 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
|
|
|
|
|
|
|
|
23 |
space_id = os.getenv("SPACE_ID")
|
24 |
|
25 |
if profile:
|
26 |
+
username = f"{profile.username}"
|
27 |
print(f"User logged in: {username}")
|
28 |
else:
|
29 |
print("User not logged in.")
|
|
|
33 |
questions_url = f"{api_url}/questions"
|
34 |
submit_url = f"{api_url}/submit"
|
35 |
|
36 |
+
# Create the agent
|
37 |
try:
|
38 |
agent = SmolAgentWrapper()
|
39 |
except Exception as e:
|
|
|
40 |
return f"Error initializing agent: {e}", None
|
41 |
+
|
42 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
|
|
43 |
|
44 |
+
# Fetch questions
|
|
|
45 |
try:
|
46 |
response = requests.get(questions_url, timeout=15)
|
47 |
response.raise_for_status()
|
48 |
questions_data = response.json()
|
49 |
if not questions_data:
|
|
|
50 |
return "Fetched questions list is empty or invalid format.", None
|
51 |
print(f"Fetched {len(questions_data)} questions.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
except Exception as e:
|
53 |
+
return f"Error fetching questions: {e}", None
|
|
|
54 |
|
55 |
+
# Run agent
|
56 |
results_log = []
|
57 |
answers_payload = []
|
58 |
+
|
59 |
for item in questions_data:
|
60 |
task_id = item.get("task_id")
|
61 |
question_text = item.get("question")
|
62 |
if not task_id or question_text is None:
|
|
|
63 |
continue
|
64 |
try:
|
65 |
submitted_answer = agent(question_text)
|
66 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
67 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
68 |
except Exception as e:
|
69 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
|
|
70 |
|
71 |
if not answers_payload:
|
|
|
72 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
73 |
|
74 |
+
# Submit answers
|
75 |
+
submission_data = {
|
76 |
+
"username": username.strip(),
|
77 |
+
"agent_code": agent_code,
|
78 |
+
"answers": answers_payload
|
79 |
+
}
|
80 |
|
|
|
|
|
81 |
try:
|
82 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
83 |
response.raise_for_status()
|
|
|
89 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
90 |
f"Message: {result_data.get('message', 'No message received.')}"
|
91 |
)
|
|
|
92 |
results_df = pd.DataFrame(results_log)
|
93 |
return final_status, results_df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
except Exception as e:
|
95 |
+
return f"Submission Failed: {e}", pd.DataFrame(results_log)
|
|
|
|
|
|
|
96 |
|
97 |
|
98 |
+
# --- Gradio Interface ---
|
99 |
with gr.Blocks() as demo:
|
100 |
+
gr.Markdown("# SmolAgent Evaluation Runner (Flan-T5 + DuckDuckGo Tool)")
|
101 |
gr.Markdown(
|
102 |
"""
|
103 |
**Instructions:**
|
104 |
+
1. Log in to Hugging Face with the button below.
|
105 |
+
2. Click the button to run all GAIA questions through the SmolAgent.
|
106 |
+
3. Results will be submitted automatically and your score will be shown.
|
107 |
+
|
108 |
+
**Note:** Model runs on Hugging Face Inference API using `flan-t5-base`, optimized for CPU.
|
109 |
"""
|
110 |
)
|
111 |
|
112 |
gr.LoginButton()
|
|
|
113 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
|
|
114 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
115 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
116 |
|
|
|
120 |
)
|
121 |
|
122 |
if __name__ == "__main__":
|
123 |
+
print("-" * 60)
|
124 |
+
print("Launching SmolAgent Space...")
|
125 |
+
print("-" * 60)
|
126 |
demo.launch(debug=True, share=False)
|