Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -9,13 +9,27 @@ from langchain.text_splitter import RecursiveCharacterTextSplitter
|
|
9 |
from langchain_community.retrievers import BM25Retriever
|
10 |
|
11 |
from smolagents import Tool, CodeAgent
|
|
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
|
16 |
hf_token = os.getenv("HUGGINGFACE_API_KEY")
|
17 |
print("Token from env var:", hf_token)
|
18 |
-
os.environ["HUGGINGFACE_API_KEY"] = hf_token
|
19 |
|
20 |
if hf_token:
|
21 |
os.environ["HUGGINGFACE_API_KEY"] = hf_token
|
@@ -26,38 +40,29 @@ else:
|
|
26 |
print("HUGGINGFACE_API_KEY in env:", "HUGGINGFACE_API_KEY" in os.environ)
|
27 |
print("HUGGINGFACE_API_KEY value (masked):", os.environ.get("HUGGINGFACE_API_KEY", "")[:5] + "...")
|
28 |
|
29 |
-
|
30 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
31 |
-
""
|
32 |
-
Fetches all questions, runs the agent on them, submits all answers,
|
33 |
-
and displays the results.
|
34 |
-
"""
|
35 |
-
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
36 |
|
37 |
if profile:
|
38 |
-
username =
|
39 |
print(f"User logged in: {username}")
|
40 |
else:
|
41 |
print("User not logged in.")
|
42 |
return "Please Login to Hugging Face with the button.", None
|
43 |
|
44 |
-
api_url = "https://agents-course-unit4-scoring.hf.space"
|
45 |
questions_url = f"{api_url}/questions"
|
46 |
submit_url = f"{api_url}/submit"
|
47 |
|
48 |
-
# --- Instantiate Agent ---
|
49 |
try:
|
50 |
-
# Load knowledge base dataset and filter
|
51 |
knowledge_base = datasets.load_dataset("m-ric/huggingface_doc", split="train")
|
52 |
knowledge_base = knowledge_base.filter(lambda row: row["source"].startswith("huggingface/transformers"))
|
53 |
|
54 |
-
# Create source Documents for retriever
|
55 |
source_docs = [
|
56 |
Document(page_content=doc["text"], metadata={"source": doc["source"].split("/")[1]})
|
57 |
for doc in knowledge_base
|
58 |
]
|
59 |
|
60 |
-
# Split documents into chunks
|
61 |
text_splitter = RecursiveCharacterTextSplitter(
|
62 |
chunk_size=500,
|
63 |
chunk_overlap=50,
|
@@ -67,7 +72,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
67 |
)
|
68 |
docs_processed = text_splitter.split_documents(source_docs)
|
69 |
|
70 |
-
# Define the Retriever tool
|
71 |
class RetrieverTool(Tool):
|
72 |
name = "retriever"
|
73 |
description = (
|
@@ -98,27 +102,17 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
98 |
|
99 |
retriever_tool = RetrieverTool(docs_processed)
|
100 |
|
101 |
-
# Initialize
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
class HuggingFaceInferenceWrapper:
|
106 |
-
def __init__(self, client):
|
107 |
-
self.client = client
|
108 |
-
|
109 |
-
def __call__(self, prompt: str):
|
110 |
-
response = self.client(inputs=prompt)
|
111 |
-
if isinstance(response, dict):
|
112 |
-
# Usually HF text generation returns {'generated_text': "..."}
|
113 |
-
return response.get("generated_text") or str(response)
|
114 |
-
return str(response)
|
115 |
|
116 |
agent = CodeAgent(
|
117 |
tools=[retriever_tool],
|
118 |
-
model=
|
119 |
max_steps=4,
|
120 |
verbosity_level=2,
|
121 |
-
stream_outputs=False,
|
122 |
)
|
123 |
|
124 |
except Exception as e:
|
@@ -127,53 +121,34 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
127 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Code repo URL not available"
|
128 |
print(agent_code)
|
129 |
|
130 |
-
# --- Fetch Questions ---
|
131 |
-
print(f"Fetching questions from: {questions_url}")
|
132 |
try:
|
133 |
response = requests.get(questions_url, timeout=15)
|
134 |
response.raise_for_status()
|
135 |
questions_data = response.json()
|
136 |
if not questions_data:
|
137 |
-
print("Fetched questions list is empty.")
|
138 |
return "Fetched questions list is empty or invalid format.", None
|
139 |
-
print(f"Fetched {len(questions_data)} questions.")
|
140 |
-
except requests.exceptions.RequestException as e:
|
141 |
-
print(f"Error fetching questions: {e}")
|
142 |
-
return f"Error fetching questions: {e}", None
|
143 |
except Exception as e:
|
144 |
-
|
145 |
-
return f"An unexpected error occurred fetching questions: {e}", None
|
146 |
|
147 |
-
# --- Run Agent on Questions ---
|
148 |
results_log = []
|
149 |
answers_payload = []
|
150 |
-
print(f"Running agent on {len(questions_data)} questions...")
|
151 |
for item in questions_data:
|
152 |
task_id = item.get("task_id")
|
153 |
question_text = item.get("question")
|
154 |
if not task_id or question_text is None:
|
155 |
-
print(f"Skipping item with missing task_id or question: {item}")
|
156 |
continue
|
157 |
try:
|
158 |
-
|
159 |
-
submitted_answer = agent.run(question_text) # Use .run() for smolagents CodeAgent
|
160 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
161 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
162 |
except Exception as e:
|
163 |
-
print(f"Error running agent on task {task_id}: {e}")
|
164 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
165 |
|
166 |
if not answers_payload:
|
167 |
-
print("Agent did not produce any answers to submit.")
|
168 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
169 |
|
170 |
-
# --- Prepare Submission ---
|
171 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
172 |
-
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
173 |
-
print(status_update)
|
174 |
|
175 |
-
# --- Submit Answers ---
|
176 |
-
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
177 |
try:
|
178 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
179 |
response.raise_for_status()
|
@@ -185,38 +160,14 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
185 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
186 |
f"Message: {result_data.get('message', 'No message received.')}"
|
187 |
)
|
188 |
-
print("Submission successful.")
|
189 |
results_df = pd.DataFrame(results_log)
|
190 |
return final_status, results_df
|
191 |
-
except requests.exceptions.HTTPError as e:
|
192 |
-
error_detail = f"Server responded with status {e.response.status_code}."
|
193 |
-
try:
|
194 |
-
error_json = e.response.json()
|
195 |
-
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
196 |
-
except requests.exceptions.JSONDecodeError:
|
197 |
-
error_detail += f" Response: {e.response.text[:500]}"
|
198 |
-
status_message = f"Submission Failed: {error_detail}"
|
199 |
-
print(status_message)
|
200 |
-
results_df = pd.DataFrame(results_log)
|
201 |
-
return status_message, results_df
|
202 |
-
except requests.exceptions.Timeout:
|
203 |
-
status_message = "Submission Failed: The request timed out."
|
204 |
-
print(status_message)
|
205 |
-
results_df = pd.DataFrame(results_log)
|
206 |
-
return status_message, results_df
|
207 |
-
except requests.exceptions.RequestException as e:
|
208 |
-
status_message = f"Submission Failed: Network error - {e}"
|
209 |
-
print(status_message)
|
210 |
-
results_df = pd.DataFrame(results_log)
|
211 |
-
return status_message, results_df
|
212 |
except Exception as e:
|
213 |
-
status_message = f"
|
214 |
-
print(status_message)
|
215 |
results_df = pd.DataFrame(results_log)
|
216 |
return status_message, results_df
|
217 |
|
218 |
|
219 |
-
# --- Build Gradio Interface using Blocks ---
|
220 |
with gr.Blocks() as demo:
|
221 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
222 |
gr.Markdown(
|
@@ -261,6 +212,9 @@ if __name__ == "__main__":
|
|
261 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
262 |
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
263 |
else:
|
264 |
-
print("ℹ️ SPACE_ID environment variable not found (running locally?).")
|
|
|
|
|
265 |
|
266 |
-
|
|
|
|
9 |
from langchain_community.retrievers import BM25Retriever
|
10 |
|
11 |
from smolagents import Tool, CodeAgent
|
12 |
+
from huggingface_hub.inference_api import InferenceApi
|
13 |
|
14 |
+
|
15 |
+
# Wrapper class to adapt HuggingFace Inference API to have .generate()
|
16 |
+
class HuggingFaceInferenceWrapper:
|
17 |
+
def __init__(self, inference_api):
|
18 |
+
self.inference_api = inference_api
|
19 |
+
|
20 |
+
def generate(self, prompt: str, **kwargs) -> str:
|
21 |
+
# Call the inference API with prompt, return generated text
|
22 |
+
response = self.inference_api(inputs=prompt)
|
23 |
+
if isinstance(response, dict) and "generated_text" in response:
|
24 |
+
return response["generated_text"]
|
25 |
+
elif isinstance(response, str):
|
26 |
+
return response
|
27 |
+
else:
|
28 |
+
raise ValueError(f"Unexpected response format: {response}")
|
29 |
|
30 |
|
31 |
hf_token = os.getenv("HUGGINGFACE_API_KEY")
|
32 |
print("Token from env var:", hf_token)
|
|
|
33 |
|
34 |
if hf_token:
|
35 |
os.environ["HUGGINGFACE_API_KEY"] = hf_token
|
|
|
40 |
print("HUGGINGFACE_API_KEY in env:", "HUGGINGFACE_API_KEY" in os.environ)
|
41 |
print("HUGGINGFACE_API_KEY value (masked):", os.environ.get("HUGGINGFACE_API_KEY", "")[:5] + "...")
|
42 |
|
|
|
43 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
44 |
+
space_id = os.getenv("SPACE_ID")
|
|
|
|
|
|
|
|
|
45 |
|
46 |
if profile:
|
47 |
+
username = profile.username
|
48 |
print(f"User logged in: {username}")
|
49 |
else:
|
50 |
print("User not logged in.")
|
51 |
return "Please Login to Hugging Face with the button.", None
|
52 |
|
53 |
+
api_url = "https://agents-course-unit4-scoring.hf.space"
|
54 |
questions_url = f"{api_url}/questions"
|
55 |
submit_url = f"{api_url}/submit"
|
56 |
|
|
|
57 |
try:
|
|
|
58 |
knowledge_base = datasets.load_dataset("m-ric/huggingface_doc", split="train")
|
59 |
knowledge_base = knowledge_base.filter(lambda row: row["source"].startswith("huggingface/transformers"))
|
60 |
|
|
|
61 |
source_docs = [
|
62 |
Document(page_content=doc["text"], metadata={"source": doc["source"].split("/")[1]})
|
63 |
for doc in knowledge_base
|
64 |
]
|
65 |
|
|
|
66 |
text_splitter = RecursiveCharacterTextSplitter(
|
67 |
chunk_size=500,
|
68 |
chunk_overlap=50,
|
|
|
72 |
)
|
73 |
docs_processed = text_splitter.split_documents(source_docs)
|
74 |
|
|
|
75 |
class RetrieverTool(Tool):
|
76 |
name = "retriever"
|
77 |
description = (
|
|
|
102 |
|
103 |
retriever_tool = RetrieverTool(docs_processed)
|
104 |
|
105 |
+
# Initialize HuggingFace InferenceApi
|
106 |
+
inference_api = InferenceApi(repo_id="Qwen/Qwen2.5-VL-7B-Instruct", token=hf_token)
|
107 |
+
# Wrap it so it supports .generate()
|
108 |
+
model_wrapper = HuggingFaceInferenceWrapper(inference_api)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
agent = CodeAgent(
|
111 |
tools=[retriever_tool],
|
112 |
+
model=model_wrapper,
|
113 |
max_steps=4,
|
114 |
verbosity_level=2,
|
115 |
+
stream_outputs=False, # Set False because this model doesn't support streaming here
|
116 |
)
|
117 |
|
118 |
except Exception as e:
|
|
|
121 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Code repo URL not available"
|
122 |
print(agent_code)
|
123 |
|
|
|
|
|
124 |
try:
|
125 |
response = requests.get(questions_url, timeout=15)
|
126 |
response.raise_for_status()
|
127 |
questions_data = response.json()
|
128 |
if not questions_data:
|
|
|
129 |
return "Fetched questions list is empty or invalid format.", None
|
|
|
|
|
|
|
|
|
130 |
except Exception as e:
|
131 |
+
return f"Error fetching questions: {e}", None
|
|
|
132 |
|
|
|
133 |
results_log = []
|
134 |
answers_payload = []
|
|
|
135 |
for item in questions_data:
|
136 |
task_id = item.get("task_id")
|
137 |
question_text = item.get("question")
|
138 |
if not task_id or question_text is None:
|
|
|
139 |
continue
|
140 |
try:
|
141 |
+
submitted_answer = agent.run(question_text)
|
|
|
142 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
143 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
144 |
except Exception as e:
|
|
|
145 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
146 |
|
147 |
if not answers_payload:
|
|
|
148 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
149 |
|
|
|
150 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
|
|
|
|
151 |
|
|
|
|
|
152 |
try:
|
153 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
154 |
response.raise_for_status()
|
|
|
160 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
161 |
f"Message: {result_data.get('message', 'No message received.')}"
|
162 |
)
|
|
|
163 |
results_df = pd.DataFrame(results_log)
|
164 |
return final_status, results_df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
except Exception as e:
|
166 |
+
status_message = f"Submission Failed: {e}"
|
|
|
167 |
results_df = pd.DataFrame(results_log)
|
168 |
return status_message, results_df
|
169 |
|
170 |
|
|
|
171 |
with gr.Blocks() as demo:
|
172 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
173 |
gr.Markdown(
|
|
|
212 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
213 |
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
214 |
else:
|
215 |
+
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
216 |
+
|
217 |
+
print("-" * (60 + len(" App Starting ")) + "\n")
|
218 |
|
219 |
+
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
220 |
+
demo.launch(debug=True, share=False)
|