Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,232 +1,142 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
-
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
-
# ---
|
| 12 |
-
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 14 |
-
|
| 15 |
class BasicAgent:
|
| 16 |
def __init__(self):
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 20 |
self.model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
model_name,
|
| 22 |
-
torch_dtype=
|
| 23 |
device_map="auto"
|
| 24 |
)
|
|
|
|
| 25 |
self.generator = pipeline(
|
| 26 |
"text-generation",
|
| 27 |
model=self.model,
|
| 28 |
tokenizer=self.tokenizer,
|
| 29 |
-
max_new_tokens=
|
| 30 |
-
temperature=0.0,
|
| 31 |
do_sample=False
|
| 32 |
)
|
| 33 |
|
| 34 |
def __call__(self, question: str) -> str:
|
| 35 |
-
print("
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
add_generation_prompt=True,
|
| 45 |
-
enable_thinking=False # or True, depending on mode
|
| 46 |
-
)
|
| 47 |
-
|
| 48 |
-
outputs = self.generator(prompt)
|
| 49 |
-
text = outputs[0]["generated_text"]
|
| 50 |
-
# Remove prompt prefix, get only the answer part
|
| 51 |
-
# The answer begins after prompt
|
| 52 |
-
answer = text[len(prompt):].strip()
|
| 53 |
-
# Return first line
|
| 54 |
answer = answer.split("\n")[0].strip()
|
|
|
|
|
|
|
|
|
|
| 55 |
return answer
|
| 56 |
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
"""
|
| 60 |
-
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 61 |
-
and displays the results.
|
| 62 |
-
"""
|
| 63 |
-
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 64 |
-
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 65 |
-
|
| 66 |
-
if profile:
|
| 67 |
-
username= f"{profile.username}"
|
| 68 |
-
print(f"User logged in: {username}")
|
| 69 |
-
else:
|
| 70 |
-
print("User not logged in.")
|
| 71 |
return "Please Login to Hugging Face with the button.", None
|
|
|
|
|
|
|
| 72 |
|
| 73 |
api_url = DEFAULT_API_URL
|
| 74 |
questions_url = f"{api_url}/questions"
|
| 75 |
submit_url = f"{api_url}/submit"
|
| 76 |
|
| 77 |
-
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 78 |
try:
|
| 79 |
agent = BasicAgent()
|
| 80 |
except Exception as e:
|
| 81 |
-
print(f"Error instantiating agent: {e}")
|
| 82 |
return f"Error initializing agent: {e}", None
|
| 83 |
-
|
| 84 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 85 |
-
print(agent_code)
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
print(f"Fetching questions from: {questions_url}")
|
| 89 |
try:
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
questions_data =
|
| 93 |
-
if not questions_data:
|
| 94 |
-
print("Fetched questions list is empty.")
|
| 95 |
-
return "Fetched questions list is empty or invalid format.", None
|
| 96 |
-
print(f"Fetched {len(questions_data)} questions.")
|
| 97 |
-
except requests.exceptions.RequestException as e:
|
| 98 |
-
print(f"Error fetching questions: {e}")
|
| 99 |
-
return f"Error fetching questions: {e}", None
|
| 100 |
-
except requests.exceptions.JSONDecodeError as e:
|
| 101 |
-
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 102 |
-
print(f"Response text: {response.text[:500]}")
|
| 103 |
-
return f"Error decoding server response for questions: {e}", None
|
| 104 |
except Exception as e:
|
| 105 |
-
|
| 106 |
-
return f"An unexpected error occurred fetching questions: {e}", None
|
| 107 |
|
| 108 |
-
# 3. Run your Agent
|
| 109 |
results_log = []
|
| 110 |
answers_payload = []
|
| 111 |
-
print(f"Running agent on {len(questions_data)} questions...")
|
| 112 |
for item in questions_data:
|
| 113 |
task_id = item.get("task_id")
|
| 114 |
question_text = item.get("question")
|
| 115 |
if not task_id or question_text is None:
|
| 116 |
-
print(f"Skipping item with missing task_id or question: {item}")
|
| 117 |
continue
|
| 118 |
try:
|
| 119 |
-
|
| 120 |
-
answers_payload.append({"task_id": task_id, "submitted_answer":
|
| 121 |
-
results_log.append({
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
except Exception as e:
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
if not answers_payload:
|
| 127 |
-
|
| 128 |
-
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
| 134 |
|
| 135 |
-
# 5. Submit
|
| 136 |
-
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 137 |
try:
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
f"Submission Successful!\n"
|
| 143 |
-
f"User: {
|
| 144 |
-
f"Overall Score: {
|
| 145 |
-
f"({
|
| 146 |
-
f"Message: {
|
| 147 |
)
|
| 148 |
-
|
| 149 |
-
results_df = pd.DataFrame(results_log)
|
| 150 |
-
return final_status, results_df
|
| 151 |
-
except requests.exceptions.HTTPError as e:
|
| 152 |
-
error_detail = f"Server responded with status {e.response.status_code}."
|
| 153 |
-
try:
|
| 154 |
-
error_json = e.response.json()
|
| 155 |
-
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
| 156 |
-
except requests.exceptions.JSONDecodeError:
|
| 157 |
-
error_detail += f" Response: {e.response.text[:500]}"
|
| 158 |
-
status_message = f"Submission Failed: {error_detail}"
|
| 159 |
-
print(status_message)
|
| 160 |
-
results_df = pd.DataFrame(results_log)
|
| 161 |
-
return status_message, results_df
|
| 162 |
-
except requests.exceptions.Timeout:
|
| 163 |
-
status_message = "Submission Failed: The request timed out."
|
| 164 |
-
print(status_message)
|
| 165 |
-
results_df = pd.DataFrame(results_log)
|
| 166 |
-
return status_message, results_df
|
| 167 |
-
except requests.exceptions.RequestException as e:
|
| 168 |
-
status_message = f"Submission Failed: Network error - {e}"
|
| 169 |
-
print(status_message)
|
| 170 |
-
results_df = pd.DataFrame(results_log)
|
| 171 |
-
return status_message, results_df
|
| 172 |
except Exception as e:
|
| 173 |
-
|
| 174 |
-
print(status_message)
|
| 175 |
-
results_df = pd.DataFrame(results_log)
|
| 176 |
-
return status_message, results_df
|
| 177 |
|
| 178 |
-
|
| 179 |
-
# --- Build Gradio Interface using Blocks ---
|
| 180 |
with gr.Blocks() as demo:
|
| 181 |
-
gr.Markdown("#
|
| 182 |
gr.Markdown(
|
| 183 |
"""
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 188 |
-
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 189 |
-
|
| 190 |
-
---
|
| 191 |
-
**Disclaimers:**
|
| 192 |
-
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).
|
| 193 |
-
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 seperate action or even to answer the questions in async.
|
| 194 |
"""
|
| 195 |
)
|
| 196 |
|
| 197 |
gr.LoginButton()
|
| 198 |
-
|
| 199 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 200 |
-
|
| 201 |
-
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 202 |
-
# Removed max_rows=10 from DataFrame constructor
|
| 203 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 204 |
|
| 205 |
-
run_button.click(
|
| 206 |
-
fn=run_and_submit_all,
|
| 207 |
-
outputs=[status_output, results_table]
|
| 208 |
-
)
|
| 209 |
|
| 210 |
if __name__ == "__main__":
|
| 211 |
-
|
| 212 |
-
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 213 |
-
space_host_startup = os.getenv("SPACE_HOST")
|
| 214 |
-
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
| 215 |
-
|
| 216 |
-
if space_host_startup:
|
| 217 |
-
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 218 |
-
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 219 |
-
else:
|
| 220 |
-
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 221 |
-
|
| 222 |
-
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
| 223 |
-
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 224 |
-
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 225 |
-
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 226 |
-
else:
|
| 227 |
-
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 228 |
-
|
| 229 |
-
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 230 |
-
|
| 231 |
-
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 232 |
-
demo.launch(debug=True, share=False)
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 6 |
+
import torch
|
| 7 |
|
|
|
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 10 |
|
| 11 |
+
# --- Agent Definition ---
|
|
|
|
|
|
|
|
|
|
| 12 |
class BasicAgent:
|
| 13 |
def __init__(self):
|
| 14 |
+
# Change this model to one you have access to
|
| 15 |
+
model_name = "Qwen/Qwen3-0.6B-MLX-bf16"
|
| 16 |
+
print(f"Loading model {model_name}")
|
| 17 |
+
# Load tokenizer and model
|
| 18 |
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 19 |
self.model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
model_name,
|
| 21 |
+
torch_dtype=torch.float16,
|
| 22 |
device_map="auto"
|
| 23 |
)
|
| 24 |
+
# Create generation pipeline
|
| 25 |
self.generator = pipeline(
|
| 26 |
"text-generation",
|
| 27 |
model=self.model,
|
| 28 |
tokenizer=self.tokenizer,
|
| 29 |
+
max_new_tokens=100,
|
| 30 |
+
temperature=0.0,
|
| 31 |
do_sample=False
|
| 32 |
)
|
| 33 |
|
| 34 |
def __call__(self, question: str) -> str:
|
| 35 |
+
print("Question:", question)
|
| 36 |
+
prompt = question.strip()
|
| 37 |
+
output = self.generator(prompt)[0]["generated_text"]
|
| 38 |
+
# Remove the prompt prefix so only the answer remains
|
| 39 |
+
if output.startswith(prompt):
|
| 40 |
+
answer = output[len(prompt):].strip()
|
| 41 |
+
else:
|
| 42 |
+
answer = output.strip()
|
| 43 |
+
# Take first line if multiple lines
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
answer = answer.split("\n")[0].strip()
|
| 45 |
+
# Optionally strip trailing punctuation
|
| 46 |
+
answer = answer.rstrip(" .,:;!?")
|
| 47 |
+
print("Answer:", answer)
|
| 48 |
return answer
|
| 49 |
|
| 50 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 51 |
+
space_id = os.getenv("SPACE_ID")
|
| 52 |
|
| 53 |
+
if not profile:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
return "Please Login to Hugging Face with the button.", None
|
| 55 |
+
username = profile.username
|
| 56 |
+
print("User:", username)
|
| 57 |
|
| 58 |
api_url = DEFAULT_API_URL
|
| 59 |
questions_url = f"{api_url}/questions"
|
| 60 |
submit_url = f"{api_url}/submit"
|
| 61 |
|
|
|
|
| 62 |
try:
|
| 63 |
agent = BasicAgent()
|
| 64 |
except Exception as e:
|
|
|
|
| 65 |
return f"Error initializing agent: {e}", None
|
| 66 |
+
|
| 67 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
|
|
|
| 68 |
|
| 69 |
+
# Fetch questions
|
|
|
|
| 70 |
try:
|
| 71 |
+
resp = requests.get(questions_url, timeout=15)
|
| 72 |
+
resp.raise_for_status()
|
| 73 |
+
questions_data = resp.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
except Exception as e:
|
| 75 |
+
return f"Error fetching questions: {e}", None
|
|
|
|
| 76 |
|
|
|
|
| 77 |
results_log = []
|
| 78 |
answers_payload = []
|
|
|
|
| 79 |
for item in questions_data:
|
| 80 |
task_id = item.get("task_id")
|
| 81 |
question_text = item.get("question")
|
| 82 |
if not task_id or question_text is None:
|
|
|
|
| 83 |
continue
|
| 84 |
try:
|
| 85 |
+
ans = agent(question_text)
|
| 86 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": ans})
|
| 87 |
+
results_log.append({
|
| 88 |
+
"Task ID": task_id,
|
| 89 |
+
"Question": question_text,
|
| 90 |
+
"Submitted Answer": ans
|
| 91 |
+
})
|
| 92 |
except Exception as e:
|
| 93 |
+
results_log.append({
|
| 94 |
+
"Task ID": task_id,
|
| 95 |
+
"Question": question_text,
|
| 96 |
+
"Submitted Answer": f"ERROR: {e}"
|
| 97 |
+
})
|
| 98 |
|
| 99 |
if not answers_payload:
|
| 100 |
+
return "Agent did not produce any answers.", pd.DataFrame(results_log)
|
|
|
|
| 101 |
|
| 102 |
+
submission_data = {
|
| 103 |
+
"username": username.strip(),
|
| 104 |
+
"agent_code": agent_code,
|
| 105 |
+
"answers": answers_payload
|
| 106 |
+
}
|
| 107 |
|
|
|
|
|
|
|
| 108 |
try:
|
| 109 |
+
post_resp = requests.post(submit_url, json=submission_data, timeout=60)
|
| 110 |
+
post_resp.raise_for_status()
|
| 111 |
+
result = post_resp.json()
|
| 112 |
+
status_text = (
|
| 113 |
f"Submission Successful!\n"
|
| 114 |
+
f"User: {result.get('username')}\n"
|
| 115 |
+
f"Overall Score: {result.get('score', 'N/A')}% "
|
| 116 |
+
f"({result.get('correct_count', '?')}/{result.get('total_attempted', '?')} correct)\n"
|
| 117 |
+
f"Message: {result.get('message', '')}"
|
| 118 |
)
|
| 119 |
+
return status_text, pd.DataFrame(results_log)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
except Exception as e:
|
| 121 |
+
return f"Submission Failed: {e}", pd.DataFrame(results_log)
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
+
# --- Gradio Interface ---
|
|
|
|
| 124 |
with gr.Blocks() as demo:
|
| 125 |
+
gr.Markdown("# Agent Evaluation Runner")
|
| 126 |
gr.Markdown(
|
| 127 |
"""
|
| 128 |
+
1. Login with Hugging Face
|
| 129 |
+
2. Click “Run Evaluation & Submit All Answers”
|
| 130 |
+
3. Wait for score and see your answers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
"""
|
| 132 |
)
|
| 133 |
|
| 134 |
gr.LoginButton()
|
|
|
|
| 135 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 136 |
+
status_out = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
|
|
|
|
|
|
| 137 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 138 |
|
| 139 |
+
run_button.click(fn=run_and_submit_all, outputs=[status_out, results_table])
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
if __name__ == "__main__":
|
| 142 |
+
demo.launch(debug=True, share=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|