Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,197 +1,5 @@
|
|
1 |
-
import
|
2 |
-
import requests
|
3 |
-
import pandas as pd
|
4 |
-
import gradio as gr
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
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 |
-
import json
|
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 |
-
# Call inference API with raw_response=True to get the raw Response object
|
31 |
-
response = self.inference_api(inputs=prompt, raw_response=True)
|
32 |
-
|
33 |
-
# Check if the response is a string
|
34 |
-
if isinstance(response, str):
|
35 |
-
return response.strip()
|
36 |
-
|
37 |
-
# Parse the JSON response if the response is not a string
|
38 |
-
if hasattr(response, 'json'):
|
39 |
-
json_response = response.json()
|
40 |
-
# Extract the relevant information from the JSON response
|
41 |
-
# This depends on the structure of the JSON response from the API
|
42 |
-
# For example, if the response contains a 'generated_text' field:
|
43 |
-
if isinstance(json_response, list) and len(json_response) > 0:
|
44 |
-
return json_response[0].get('generated_text', '').strip()
|
45 |
-
elif isinstance(json_response, dict):
|
46 |
-
return json_response.get('generated_text', '').strip()
|
47 |
-
|
48 |
-
# Fallback: return the raw response text if JSON parsing fails
|
49 |
-
if hasattr(response, 'text'):
|
50 |
-
return response.text.strip()
|
51 |
-
|
52 |
-
# If none of the above conditions are met, return an empty string or handle the error appropriately
|
53 |
-
return ""
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
61 |
-
space_id = os.getenv("SPACE_ID")
|
62 |
-
|
63 |
-
if not profile:
|
64 |
-
return "Please Login to Hugging Face with the button.", None
|
65 |
-
username = profile.username
|
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 dataset and filter for docs
|
73 |
-
knowledge_base = datasets.load_dataset("m-ric/huggingface_doc", split="train")
|
74 |
-
knowledge_base = knowledge_base.filter(lambda row: row["source"].startswith("huggingface/transformers"))
|
75 |
-
source_docs = [Document(page_content=doc["text"], metadata={"source": doc["source"].split("/")[1]}) for doc in knowledge_base]
|
76 |
-
|
77 |
-
text_splitter = RecursiveCharacterTextSplitter(
|
78 |
-
chunk_size=500,
|
79 |
-
chunk_overlap=50,
|
80 |
-
add_start_index=True,
|
81 |
-
strip_whitespace=True,
|
82 |
-
separators=["\n\n", "\n", ".", " ", ""],
|
83 |
-
)
|
84 |
-
docs_processed = text_splitter.split_documents(source_docs)
|
85 |
-
|
86 |
-
class RetrieverTool(Tool):
|
87 |
-
name = "retriever"
|
88 |
-
description = (
|
89 |
-
"Uses lexical search to retrieve relevant parts of transformers documentation."
|
90 |
-
)
|
91 |
-
inputs = {"query": {"type": "string", "description": "Search query"}}
|
92 |
-
output_type = "string"
|
93 |
-
|
94 |
-
def __init__(self, docs, **kwargs):
|
95 |
-
super().__init__(**kwargs)
|
96 |
-
self.retriever = BM25Retriever.from_documents(docs, k=10)
|
97 |
-
|
98 |
-
def forward(self, query: str) -> str:
|
99 |
-
docs = self.retriever.invoke(query)
|
100 |
-
return "\nRetrieved documents:\n" + "".join(
|
101 |
-
[f"\n\n===== Document {i} =====\n{doc.page_content}" for i, doc in enumerate(docs)]
|
102 |
-
)
|
103 |
-
|
104 |
-
retriever_tool = RetrieverTool(docs_processed)
|
105 |
-
|
106 |
-
# Instantiate HuggingFace InferenceApi
|
107 |
-
inference_api = InferenceApi(repo_id="Qwen/Qwen2.5-VL-7B-Instruct", token=hf_token)
|
108 |
-
hf_wrapper = HuggingFaceInferenceWrapper(inference_api)
|
109 |
-
|
110 |
-
# Use the wrapper with CodeAgent
|
111 |
-
agent = CodeAgent(
|
112 |
-
tools=[retriever_tool],
|
113 |
-
model=hf_wrapper,
|
114 |
-
max_steps=4,
|
115 |
-
verbosity_level=2,
|
116 |
-
stream_outputs=False, # MUST be False for this wrapper
|
117 |
-
)
|
118 |
-
|
119 |
-
except Exception as e:
|
120 |
-
return f"Error initializing agent: {e}", None
|
121 |
-
|
122 |
-
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "Code repo URL not available"
|
123 |
-
|
124 |
-
# Fetch questions
|
125 |
-
try:
|
126 |
-
response = requests.get(questions_url, timeout=15)
|
127 |
-
response.raise_for_status()
|
128 |
-
questions_data = response.json()
|
129 |
-
if not questions_data:
|
130 |
-
return "Fetched questions list is empty or invalid format.", None
|
131 |
-
except Exception as e:
|
132 |
-
return f"Error fetching questions: {e}", None
|
133 |
-
|
134 |
-
# Run agent on questions
|
135 |
-
results_log = []
|
136 |
-
answers_payload = []
|
137 |
-
|
138 |
-
for item in questions_data:
|
139 |
-
task_id = item.get("task_id")
|
140 |
-
question_text = item.get("question")
|
141 |
-
if not task_id or question_text is None:
|
142 |
-
continue
|
143 |
-
try:
|
144 |
-
submitted_answer = agent.run(question_text)
|
145 |
-
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
146 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
147 |
-
except Exception as e:
|
148 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
149 |
-
|
150 |
-
if not answers_payload:
|
151 |
-
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
152 |
-
|
153 |
-
# Prepare submission
|
154 |
-
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
155 |
-
|
156 |
-
# Submit answers
|
157 |
-
try:
|
158 |
-
response = requests.post(submit_url, json=submission_data, timeout=60)
|
159 |
-
response.raise_for_status()
|
160 |
-
result_data = response.json()
|
161 |
-
final_status = (
|
162 |
-
f"Submission Successful!\n"
|
163 |
-
f"User: {result_data.get('username')}\n"
|
164 |
-
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
165 |
-
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
166 |
-
f"Message: {result_data.get('message', 'No message received.')}"
|
167 |
-
)
|
168 |
-
results_df = pd.DataFrame(results_log)
|
169 |
-
return final_status, results_df
|
170 |
-
except Exception as e:
|
171 |
-
return f"Submission failed: {e}", pd.DataFrame(results_log)
|
172 |
-
|
173 |
-
|
174 |
-
# Gradio Interface
|
175 |
-
with gr.Blocks() as demo:
|
176 |
-
gr.Markdown("# Basic Agent Evaluation Runner")
|
177 |
-
gr.Markdown(
|
178 |
-
"""
|
179 |
-
**Instructions:**
|
180 |
-
1. Log in to your Hugging Face account using the button below.
|
181 |
-
2. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, and submit answers.
|
182 |
-
"""
|
183 |
-
)
|
184 |
-
|
185 |
-
gr.LoginButton()
|
186 |
-
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
187 |
-
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
188 |
-
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
189 |
-
|
190 |
-
run_button.click(
|
191 |
-
fn=run_and_submit_all,
|
192 |
-
outputs=[status_output, results_table]
|
193 |
-
)
|
194 |
-
|
195 |
-
|
196 |
-
if __name__ == "__main__":
|
197 |
-
demo.launch(debug=True, share=False)
|
|
|
1 |
+
from huggingface_hub import InferenceApi
|
|
|
|
|
|
|
2 |
|
3 |
+
inference_api = InferenceApi(repo_id="Qwen/Qwen1.5-1.8B-Chat", token="hf_your_token")
|
4 |
+
llm = HuggingFaceInferenceWrapper(inference_api)
|
5 |
+
print(llm.generate("What is the capital of France?"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|