Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import os
|
2 |
import requests
|
3 |
-
import json
|
4 |
import pandas as pd
|
5 |
import gradio as gr
|
6 |
|
@@ -9,76 +8,44 @@ from langchain.docstore.document import Document
|
|
9 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
10 |
from langchain_community.retrievers import BM25Retriever
|
11 |
|
12 |
-
from smolagents import Tool, CodeAgent
|
13 |
-
|
14 |
from huggingface_hub.inference_api import InferenceApi
|
15 |
|
16 |
-
|
|
|
17 |
hf_token = os.getenv("HUGGINGFACE_API_KEY")
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
else:
|
23 |
-
print("No HUGGINGFACE_API_KEY found in env.")
|
24 |
|
|
|
25 |
class HuggingFaceInferenceWrapper:
|
26 |
def __init__(self, inference_api):
|
27 |
self.inference_api = inference_api
|
28 |
|
29 |
def generate(self, prompt: str, **kwargs) -> str:
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
# requests.Response-like object
|
35 |
-
json_data = json.loads(response.content)
|
36 |
-
else:
|
37 |
-
# Sometimes response might be a string already
|
38 |
-
try:
|
39 |
-
json_data = json.loads(response)
|
40 |
-
except Exception:
|
41 |
-
# Fallback: return raw string response
|
42 |
-
return str(response)
|
43 |
-
|
44 |
-
# Extract generated_text from json
|
45 |
-
if isinstance(json_data, dict) and "generated_text" in json_data:
|
46 |
-
return json_data["generated_text"].strip()
|
47 |
-
elif (
|
48 |
-
isinstance(json_data, list)
|
49 |
-
and len(json_data) > 0
|
50 |
-
and "generated_text" in json_data[0]
|
51 |
-
):
|
52 |
-
return json_data[0]["generated_text"].strip()
|
53 |
-
else:
|
54 |
-
# fallback: return entire json as string
|
55 |
-
return str(json_data)
|
56 |
|
57 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
58 |
-
space_id = os.getenv("SPACE_ID")
|
59 |
|
60 |
-
if profile:
|
61 |
-
username = f"{profile.username}"
|
62 |
-
print(f"User logged in: {username}")
|
63 |
-
else:
|
64 |
-
print("User not logged in.")
|
65 |
return "Please Login to Hugging Face with the button.", None
|
|
|
66 |
|
67 |
-
api_url = "https://agents-course-unit4-scoring.hf.space"
|
68 |
questions_url = f"{api_url}/questions"
|
69 |
submit_url = f"{api_url}/submit"
|
70 |
|
71 |
try:
|
72 |
-
# Load
|
73 |
knowledge_base = datasets.load_dataset("m-ric/huggingface_doc", split="train")
|
74 |
-
knowledge_base = knowledge_base.filter(
|
75 |
-
|
76 |
-
)
|
77 |
-
|
78 |
-
source_docs = [
|
79 |
-
Document(page_content=doc["text"], metadata={"source": doc["source"].split("/")[1]})
|
80 |
-
for doc in knowledge_base
|
81 |
-
]
|
82 |
|
83 |
text_splitter = RecursiveCharacterTextSplitter(
|
84 |
chunk_size=500,
|
@@ -92,16 +59,9 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
92 |
class RetrieverTool(Tool):
|
93 |
name = "retriever"
|
94 |
description = (
|
95 |
-
"Uses lexical search to retrieve relevant parts of transformers
|
96 |
)
|
97 |
-
inputs = {
|
98 |
-
"query": {
|
99 |
-
"type": "string",
|
100 |
-
"description": (
|
101 |
-
"The query to perform. Should be lexically close to your target documents."
|
102 |
-
),
|
103 |
-
}
|
104 |
-
}
|
105 |
output_type = "string"
|
106 |
|
107 |
def __init__(self, docs, **kwargs):
|
@@ -109,35 +69,32 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
109 |
self.retriever = BM25Retriever.from_documents(docs, k=10)
|
110 |
|
111 |
def forward(self, query: str) -> str:
|
112 |
-
assert isinstance(query, str), "Your search query must be a string"
|
113 |
docs = self.retriever.invoke(query)
|
114 |
return "\nRetrieved documents:\n" + "".join(
|
115 |
-
[f"\n\n===== Document {i} =====\n
|
116 |
)
|
117 |
|
118 |
retriever_tool = RetrieverTool(docs_processed)
|
119 |
|
120 |
-
# Instantiate HuggingFace
|
121 |
inference_api = InferenceApi(repo_id="Qwen/Qwen2.5-VL-7B-Instruct", token=hf_token)
|
122 |
-
|
123 |
|
124 |
-
#
|
125 |
agent = CodeAgent(
|
126 |
tools=[retriever_tool],
|
127 |
-
model=
|
128 |
max_steps=4,
|
129 |
verbosity_level=2,
|
130 |
-
stream_outputs=False, #
|
131 |
)
|
132 |
|
133 |
except Exception as e:
|
134 |
return f"Error initializing agent: {e}", None
|
135 |
|
136 |
-
agent_code =
|
137 |
-
f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Code repo URL not available"
|
138 |
-
)
|
139 |
-
print(agent_code)
|
140 |
|
|
|
141 |
try:
|
142 |
response = requests.get(questions_url, timeout=15)
|
143 |
response.raise_for_status()
|
@@ -147,6 +104,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
147 |
except Exception as e:
|
148 |
return f"Error fetching questions: {e}", None
|
149 |
|
|
|
150 |
results_log = []
|
151 |
answers_payload = []
|
152 |
|
@@ -165,9 +123,10 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
165 |
if not answers_payload:
|
166 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
167 |
|
|
|
168 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
169 |
-
print(f"Submitting {len(answers_payload)} answers...")
|
170 |
|
|
|
171 |
try:
|
172 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
173 |
response.raise_for_status()
|
@@ -182,31 +141,22 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
182 |
results_df = pd.DataFrame(results_log)
|
183 |
return final_status, results_df
|
184 |
except Exception as e:
|
185 |
-
|
186 |
-
results_df = pd.DataFrame(results_log)
|
187 |
-
return status_message, results_df
|
188 |
|
189 |
|
190 |
-
# Gradio
|
191 |
with gr.Blocks() as demo:
|
192 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
193 |
gr.Markdown(
|
194 |
"""
|
195 |
**Instructions:**
|
196 |
-
1.
|
197 |
-
2.
|
198 |
-
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
199 |
-
---
|
200 |
-
**Disclaimers:**
|
201 |
-
Once clicking on the "submit" button, it can take quite some time (this is the time for the agent to go through all the questions).
|
202 |
-
This space provides a basic setup and is intentionally sub-optimal to encourage you to develop your own, more robust solution. For instance for the delay process of the submit button, a solution could be to cache the answers and submit in a separate action or even to answer the questions asynchronously.
|
203 |
"""
|
204 |
)
|
205 |
|
206 |
gr.LoginButton()
|
207 |
-
|
208 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
209 |
-
|
210 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
211 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
212 |
|
@@ -217,24 +167,4 @@ with gr.Blocks() as demo:
|
|
217 |
|
218 |
|
219 |
if __name__ == "__main__":
|
220 |
-
print("\n" + "-" * 30 + " App Starting " + "-" * 30)
|
221 |
-
space_host_startup = os.getenv("SPACE_HOST")
|
222 |
-
space_id_startup = os.getenv("SPACE_ID")
|
223 |
-
|
224 |
-
if space_host_startup:
|
225 |
-
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
226 |
-
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
227 |
-
else:
|
228 |
-
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
229 |
-
|
230 |
-
if space_id_startup:
|
231 |
-
print(f"✅ SPACE_ID found: {space_id_startup}")
|
232 |
-
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
233 |
-
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
234 |
-
else:
|
235 |
-
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
236 |
-
|
237 |
-
print("-" * (60 + len(" App Starting ")) + "\n")
|
238 |
-
|
239 |
-
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
240 |
demo.launch(debug=True, share=False)
|
|
|
1 |
import os
|
2 |
import requests
|
|
|
3 |
import pandas as pd
|
4 |
import gradio as gr
|
5 |
|
|
|
8 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
9 |
from langchain_community.retrievers import BM25Retriever
|
10 |
|
11 |
+
from smolagents import Tool, CodeAgent, InferenceClientModel
|
|
|
12 |
from huggingface_hub.inference_api import InferenceApi
|
13 |
|
14 |
+
|
15 |
+
# Load your HF API token from environment
|
16 |
hf_token = os.getenv("HUGGINGFACE_API_KEY")
|
17 |
+
if not hf_token:
|
18 |
+
raise ValueError("HUGGINGFACE_API_KEY not set in environment variables")
|
19 |
+
os.environ["HUGGINGFACE_API_KEY"] = hf_token
|
20 |
+
|
|
|
|
|
21 |
|
22 |
+
# Define the HuggingFaceInferenceWrapper class correctly
|
23 |
class HuggingFaceInferenceWrapper:
|
24 |
def __init__(self, inference_api):
|
25 |
self.inference_api = inference_api
|
26 |
|
27 |
def generate(self, prompt: str, **kwargs) -> str:
|
28 |
+
# Call inference API - returns string directly
|
29 |
+
response = self.inference_api(inputs=prompt)
|
30 |
+
return response.strip()
|
31 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
34 |
+
space_id = os.getenv("SPACE_ID")
|
35 |
|
36 |
+
if not profile:
|
|
|
|
|
|
|
|
|
37 |
return "Please Login to Hugging Face with the button.", None
|
38 |
+
username = profile.username
|
39 |
|
40 |
+
api_url = "https://agents-course-unit4-scoring.hf.space"
|
41 |
questions_url = f"{api_url}/questions"
|
42 |
submit_url = f"{api_url}/submit"
|
43 |
|
44 |
try:
|
45 |
+
# Load dataset and filter for docs
|
46 |
knowledge_base = datasets.load_dataset("m-ric/huggingface_doc", split="train")
|
47 |
+
knowledge_base = knowledge_base.filter(lambda row: row["source"].startswith("huggingface/transformers"))
|
48 |
+
source_docs = [Document(page_content=doc["text"], metadata={"source": doc["source"].split("/")[1]}) for doc in knowledge_base]
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
text_splitter = RecursiveCharacterTextSplitter(
|
51 |
chunk_size=500,
|
|
|
59 |
class RetrieverTool(Tool):
|
60 |
name = "retriever"
|
61 |
description = (
|
62 |
+
"Uses lexical search to retrieve relevant parts of transformers documentation."
|
63 |
)
|
64 |
+
inputs = {"query": {"type": "string", "description": "Search query"}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
output_type = "string"
|
66 |
|
67 |
def __init__(self, docs, **kwargs):
|
|
|
69 |
self.retriever = BM25Retriever.from_documents(docs, k=10)
|
70 |
|
71 |
def forward(self, query: str) -> str:
|
|
|
72 |
docs = self.retriever.invoke(query)
|
73 |
return "\nRetrieved documents:\n" + "".join(
|
74 |
+
[f"\n\n===== Document {i} =====\n{doc.page_content}" for i, doc in enumerate(docs)]
|
75 |
)
|
76 |
|
77 |
retriever_tool = RetrieverTool(docs_processed)
|
78 |
|
79 |
+
# Instantiate HuggingFace InferenceApi
|
80 |
inference_api = InferenceApi(repo_id="Qwen/Qwen2.5-VL-7B-Instruct", token=hf_token)
|
81 |
+
hf_wrapper = HuggingFaceInferenceWrapper(inference_api)
|
82 |
|
83 |
+
# Use the wrapper with CodeAgent
|
84 |
agent = CodeAgent(
|
85 |
tools=[retriever_tool],
|
86 |
+
model=hf_wrapper,
|
87 |
max_steps=4,
|
88 |
verbosity_level=2,
|
89 |
+
stream_outputs=False, # MUST be False for this wrapper
|
90 |
)
|
91 |
|
92 |
except Exception as e:
|
93 |
return f"Error initializing agent: {e}", None
|
94 |
|
95 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Code repo URL not available"
|
|
|
|
|
|
|
96 |
|
97 |
+
# Fetch questions
|
98 |
try:
|
99 |
response = requests.get(questions_url, timeout=15)
|
100 |
response.raise_for_status()
|
|
|
104 |
except Exception as e:
|
105 |
return f"Error fetching questions: {e}", None
|
106 |
|
107 |
+
# Run agent on questions
|
108 |
results_log = []
|
109 |
answers_payload = []
|
110 |
|
|
|
123 |
if not answers_payload:
|
124 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
125 |
|
126 |
+
# Prepare submission
|
127 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
|
|
128 |
|
129 |
+
# Submit answers
|
130 |
try:
|
131 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
132 |
response.raise_for_status()
|
|
|
141 |
results_df = pd.DataFrame(results_log)
|
142 |
return final_status, results_df
|
143 |
except Exception as e:
|
144 |
+
return f"Submission failed: {e}", pd.DataFrame(results_log)
|
|
|
|
|
145 |
|
146 |
|
147 |
+
# Gradio Interface
|
148 |
with gr.Blocks() as demo:
|
149 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
150 |
gr.Markdown(
|
151 |
"""
|
152 |
**Instructions:**
|
153 |
+
1. Log in to your Hugging Face account using the button below.
|
154 |
+
2. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, and submit answers.
|
|
|
|
|
|
|
|
|
|
|
155 |
"""
|
156 |
)
|
157 |
|
158 |
gr.LoginButton()
|
|
|
159 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
|
|
160 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
161 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
162 |
|
|
|
167 |
|
168 |
|
169 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
demo.launch(debug=True, share=False)
|