Commit
·
dec881c
1
Parent(s):
e0f29bb
cached answers
Browse files
app.py
CHANGED
|
@@ -17,6 +17,8 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 17 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 18 |
|
| 19 |
|
|
|
|
|
|
|
| 20 |
class BasicAgent:
|
| 21 |
"""A langgraph agent."""
|
| 22 |
def __init__(self):
|
|
@@ -25,188 +27,120 @@ class BasicAgent:
|
|
| 25 |
|
| 26 |
def __call__(self, question: str) -> str:
|
| 27 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 28 |
-
# Wrap the question in a HumanMessage from langchain_core
|
| 29 |
messages = [HumanMessage(content=question)]
|
| 30 |
messages = self.graph.invoke({"messages": messages})
|
| 31 |
answer = messages['messages'][-1].content
|
| 32 |
return answer[14:]
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 38 |
-
and displays the results.
|
| 39 |
-
"""
|
| 40 |
-
space_id = os.getenv("SPACE_ID")
|
| 41 |
-
|
| 42 |
-
if profile:
|
| 43 |
-
username = f"{profile.username}"
|
| 44 |
-
print(f"User logged in: {username}")
|
| 45 |
-
else:
|
| 46 |
-
print("User not logged in.")
|
| 47 |
-
return "Please Login to Hugging Face with the button.", None
|
| 48 |
-
|
| 49 |
-
api_url = DEFAULT_API_URL
|
| 50 |
-
questions_url = f"{api_url}/questions"
|
| 51 |
-
submit_url = f"{api_url}/submit"
|
| 52 |
|
| 53 |
try:
|
| 54 |
agent = BasicAgent()
|
| 55 |
except Exception as e:
|
| 56 |
-
|
| 57 |
-
return f"Error initializing agent: {e}", None
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
|
| 62 |
-
print(f"Fetching questions from: {questions_url}")
|
| 63 |
try:
|
| 64 |
response = requests.get(questions_url, timeout=15)
|
| 65 |
-
response.raise_for_status()
|
| 66 |
questions_data = response.json()
|
| 67 |
-
if not questions_data:
|
| 68 |
-
print("Fetched questions list is empty.")
|
| 69 |
-
return "Fetched questions list is empty or invalid format.", None
|
| 70 |
-
print(f"Fetched {len(questions_data)} questions.")
|
| 71 |
-
except requests.exceptions.RequestException as e:
|
| 72 |
-
print(f"Error fetching questions: {e}")
|
| 73 |
-
return f"Error fetching questions: {e}", None
|
| 74 |
-
except requests.exceptions.JSONDecodeError as e:
|
| 75 |
-
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 76 |
-
print(f"Response text: {response.text[:500]}")
|
| 77 |
-
return f"Error decoding server response for questions: {e}", None
|
| 78 |
except Exception as e:
|
| 79 |
-
|
| 80 |
-
return f"An unexpected error occurred fetching questions: {e}", None
|
| 81 |
-
|
| 82 |
-
results_log = []
|
| 83 |
-
answers_payload = []
|
| 84 |
-
print(f"Running agent on {len(questions_data)} questions...")
|
| 85 |
|
| 86 |
with open("system_prompt.txt", "r", encoding="utf-8") as f:
|
| 87 |
system_prompt = f.read().strip()
|
| 88 |
|
| 89 |
for item in questions_data:
|
| 90 |
task_id = item.get("task_id")
|
| 91 |
-
|
| 92 |
file_name = item.get("file_name")
|
| 93 |
|
| 94 |
-
if not task_id or
|
| 95 |
-
print(f"Skipping item with missing task_id or question: {item}")
|
| 96 |
continue
|
| 97 |
|
| 98 |
try:
|
| 99 |
-
user_message =
|
| 100 |
if file_name:
|
| 101 |
user_message += f"\n\nFile to use: {file_name}"
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
results_log.append({"Task ID": task_id, "Question":
|
| 107 |
except Exception as e:
|
| 108 |
-
|
| 109 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 110 |
|
| 111 |
-
|
| 112 |
-
print("Agent did not produce any answers to submit.")
|
| 113 |
-
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
-
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 120 |
try:
|
| 121 |
-
response = requests.post(submit_url, json=
|
| 122 |
-
response.
|
| 123 |
-
result_data = response.json()
|
| 124 |
final_status = (
|
| 125 |
-
f"Submission Successful!\n"
|
| 126 |
-
f"
|
| 127 |
-
f"Overall Score: {result_data.get('score', 'N/A')}% "
|
| 128 |
-
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 129 |
-
f"Message: {result_data.get('message', 'No message received.')}"
|
| 130 |
)
|
| 131 |
-
|
| 132 |
-
results_df = pd.DataFrame(results_log)
|
| 133 |
-
return final_status, results_df
|
| 134 |
-
except requests.exceptions.HTTPError as e:
|
| 135 |
-
error_detail = f"Server responded with status {e.response.status_code}."
|
| 136 |
-
try:
|
| 137 |
-
error_json = e.response.json()
|
| 138 |
-
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
| 139 |
-
except requests.exceptions.JSONDecodeError:
|
| 140 |
-
error_detail += f" Response: {e.response.text[:500]}"
|
| 141 |
-
status_message = f"Submission Failed: {error_detail}"
|
| 142 |
-
print(status_message)
|
| 143 |
-
results_df = pd.DataFrame(results_log)
|
| 144 |
-
return status_message, results_df
|
| 145 |
-
except requests.exceptions.Timeout:
|
| 146 |
-
status_message = "Submission Failed: The request timed out."
|
| 147 |
-
print(status_message)
|
| 148 |
-
results_df = pd.DataFrame(results_log)
|
| 149 |
-
return status_message, results_df
|
| 150 |
-
except requests.exceptions.RequestException as e:
|
| 151 |
-
status_message = f"Submission Failed: Network error - {e}"
|
| 152 |
-
print(status_message)
|
| 153 |
-
results_df = pd.DataFrame(results_log)
|
| 154 |
-
return status_message, results_df
|
| 155 |
except Exception as e:
|
| 156 |
-
|
| 157 |
-
print(status_message)
|
| 158 |
-
results_df = pd.DataFrame(results_log)
|
| 159 |
-
return status_message, results_df
|
| 160 |
|
| 161 |
-
# ---
|
| 162 |
with gr.Blocks() as demo:
|
| 163 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 164 |
-
gr.Markdown(
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 170 |
-
---
|
| 171 |
-
**Disclaimers:**
|
| 172 |
-
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).
|
| 173 |
-
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.
|
| 174 |
-
"""
|
| 175 |
-
)
|
| 176 |
|
| 177 |
gr.LoginButton()
|
| 178 |
|
| 179 |
-
run_button = gr.Button("Run
|
|
|
|
| 180 |
|
| 181 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 182 |
-
# Removed max_rows=10 from DataFrame constructor
|
| 183 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 184 |
|
| 185 |
-
run_button.click(
|
| 186 |
-
|
| 187 |
-
outputs=[status_output, results_table]
|
| 188 |
-
)
|
| 189 |
|
| 190 |
if __name__ == "__main__":
|
| 191 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 192 |
-
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 193 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 194 |
-
space_id_startup = os.getenv("SPACE_ID")
|
| 195 |
|
| 196 |
if space_host_startup:
|
| 197 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 198 |
-
print(f" Runtime URL
|
| 199 |
else:
|
| 200 |
-
print("ℹ️ SPACE_HOST
|
| 201 |
|
| 202 |
-
if space_id_startup:
|
| 203 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 204 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 205 |
-
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 206 |
else:
|
| 207 |
-
print("ℹ️ SPACE_ID
|
| 208 |
-
|
| 209 |
-
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 210 |
|
| 211 |
-
print("Launching Gradio Interface
|
| 212 |
-
demo.launch(debug=True, share=False)
|
|
|
|
| 17 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 18 |
|
| 19 |
|
| 20 |
+
cached_answers = []
|
| 21 |
+
|
| 22 |
class BasicAgent:
|
| 23 |
"""A langgraph agent."""
|
| 24 |
def __init__(self):
|
|
|
|
| 27 |
|
| 28 |
def __call__(self, question: str) -> str:
|
| 29 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
|
|
|
| 30 |
messages = [HumanMessage(content=question)]
|
| 31 |
messages = self.graph.invoke({"messages": messages})
|
| 32 |
answer = messages['messages'][-1].content
|
| 33 |
return answer[14:]
|
| 34 |
|
| 35 |
+
def run_agent_only(profile: gr.OAuthProfile | None):
|
| 36 |
+
global cached_answers
|
| 37 |
+
cached_answers = []
|
| 38 |
+
results_log = []
|
| 39 |
|
| 40 |
+
if not profile:
|
| 41 |
+
return "Please login first.", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
try:
|
| 44 |
agent = BasicAgent()
|
| 45 |
except Exception as e:
|
| 46 |
+
return f"Agent Init Error: {e}", None
|
|
|
|
| 47 |
|
| 48 |
+
api_url = "https://agents-course-unit4-scoring.hf.space"
|
| 49 |
+
questions_url = f"{api_url}/questions"
|
| 50 |
|
|
|
|
| 51 |
try:
|
| 52 |
response = requests.get(questions_url, timeout=15)
|
|
|
|
| 53 |
questions_data = response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
except Exception as e:
|
| 55 |
+
return f"Error fetching questions: {e}", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
with open("system_prompt.txt", "r", encoding="utf-8") as f:
|
| 58 |
system_prompt = f.read().strip()
|
| 59 |
|
| 60 |
for item in questions_data:
|
| 61 |
task_id = item.get("task_id")
|
| 62 |
+
question = item.get("question")
|
| 63 |
file_name = item.get("file_name")
|
| 64 |
|
| 65 |
+
if not task_id or question is None:
|
|
|
|
| 66 |
continue
|
| 67 |
|
| 68 |
try:
|
| 69 |
+
user_message = question
|
| 70 |
if file_name:
|
| 71 |
user_message += f"\n\nFile to use: {file_name}"
|
| 72 |
|
| 73 |
+
full_input = system_prompt + "\n\n" + user_message
|
| 74 |
+
answer = agent(full_input)
|
| 75 |
+
cached_answers.append({"task_id": task_id, "submitted_answer": answer})
|
| 76 |
+
results_log.append({"Task ID": task_id, "Question": question, "Submitted Answer": answer})
|
| 77 |
except Exception as e:
|
| 78 |
+
results_log.append({"Task ID": task_id, "Question": question, "Submitted Answer": f"AGENT ERROR: {e}"})
|
|
|
|
| 79 |
|
| 80 |
+
return "Agent finished. Now click 'Submit Cached Answers'", pd.DataFrame(results_log)
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
def submit_cached_answers(profile: gr.OAuthProfile | None):
|
| 83 |
+
global cached_answers
|
| 84 |
+
if not profile or not cached_answers:
|
| 85 |
+
return "No cached answers to submit. Run the agent first.", None
|
| 86 |
+
|
| 87 |
+
space_id = os.getenv("SPACE_ID")
|
| 88 |
+
username = profile.username
|
| 89 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 90 |
+
|
| 91 |
+
payload = {
|
| 92 |
+
"username": username,
|
| 93 |
+
"agent_code": agent_code,
|
| 94 |
+
"answers": cached_answers
|
| 95 |
+
}
|
| 96 |
|
|
|
|
| 97 |
try:
|
| 98 |
+
response = requests.post(submit_url, json=payload, timeout=60)
|
| 99 |
+
result = response.json()
|
|
|
|
| 100 |
final_status = (
|
| 101 |
+
f"Submission Successful!\nUser: {result.get('username')}\n"
|
| 102 |
+
f"Score: {result.get('score', 'N/A')}% ({result.get('correct_count', '?')}/{result.get('total_attempted', '?')})"
|
|
|
|
|
|
|
|
|
|
| 103 |
)
|
| 104 |
+
return final_status, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
except Exception as e:
|
| 106 |
+
return f"Submission failed: {e}", None
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
+
# --- Gradio UI ---
|
| 109 |
with gr.Blocks() as demo:
|
| 110 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 111 |
+
gr.Markdown("""
|
| 112 |
+
**Instructions:**
|
| 113 |
+
1. Run the Agent to generate answers to all questions.
|
| 114 |
+
2. Then click 'Submit Cached Answers' to submit them for scoring.
|
| 115 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
gr.LoginButton()
|
| 118 |
|
| 119 |
+
run_button = gr.Button("🧠 Run Agent Only")
|
| 120 |
+
submit_button = gr.Button("📤 Submit Cached Answers")
|
| 121 |
|
| 122 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
|
|
|
| 123 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 124 |
|
| 125 |
+
run_button.click(fn=run_agent_only, outputs=[status_output, results_table])
|
| 126 |
+
submit_button.click(fn=submit_cached_answers, outputs=[status_output, results_table])
|
|
|
|
|
|
|
| 127 |
|
| 128 |
if __name__ == "__main__":
|
| 129 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
|
|
|
| 130 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 131 |
+
space_id_startup = os.getenv("SPACE_ID")
|
| 132 |
|
| 133 |
if space_host_startup:
|
| 134 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 135 |
+
print(f" Runtime URL: https://{space_host_startup}.hf.space")
|
| 136 |
else:
|
| 137 |
+
print("ℹ️ No SPACE_HOST found.")
|
| 138 |
|
| 139 |
+
if space_id_startup:
|
| 140 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 141 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
|
|
|
| 142 |
else:
|
| 143 |
+
print("ℹ️ No SPACE_ID found.")
|
|
|
|
|
|
|
| 144 |
|
| 145 |
+
print("Launching Gradio Interface...")
|
| 146 |
+
demo.launch(debug=True, share=False)
|