Update app.py
Browse files
app.py
CHANGED
@@ -3,43 +3,22 @@ import gradio as gr
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
-
|
7 |
-
from langchain_core.messages import HumanMessage
|
8 |
-
import time
|
9 |
-
import csv
|
10 |
-
|
11 |
# (Keep Constants as is)
|
12 |
# --- Constants ---
|
13 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
14 |
|
15 |
# --- Basic Agent Definition ---
|
|
|
16 |
class BasicAgent:
|
17 |
def __init__(self):
|
18 |
print("BasicAgent initialized.")
|
19 |
-
self.agent = build_graph()
|
20 |
-
|
21 |
def __call__(self, question: str) -> str:
|
22 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
index = answer.find("FINAL ANSWER: ")
|
28 |
-
if index == -1:
|
29 |
-
return answer
|
30 |
-
return answer[index+14:]
|
31 |
-
|
32 |
-
# --- Upload answers solved locally ---
|
33 |
-
def csv_to_dict(file_path):
|
34 |
-
result = {}
|
35 |
-
with open(file_path, 'r') as file:
|
36 |
-
csv_reader = csv.reader(file)
|
37 |
-
header = next(csv_reader) # Skip header row
|
38 |
-
for row in csv_reader:
|
39 |
-
result[row[0]] = row[1]
|
40 |
-
return result
|
41 |
-
|
42 |
-
|
43 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
44 |
"""
|
45 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
@@ -53,24 +32,23 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
53 |
print(f"User logged in: {username}")
|
54 |
else:
|
55 |
print("User not logged in.")
|
56 |
-
return "Please
|
57 |
|
58 |
api_url = DEFAULT_API_URL
|
59 |
questions_url = f"{api_url}/questions"
|
60 |
submit_url = f"{api_url}/submit"
|
61 |
|
62 |
-
# 1. Instantiate Agent (modify this part to create your agent)
|
63 |
try:
|
64 |
agent = BasicAgent()
|
65 |
except Exception as e:
|
66 |
print(f"Error instantiating agent: {e}")
|
67 |
return f"Error initializing agent: {e}", None
|
68 |
-
|
69 |
-
# In the case of an app running as a Hugging Face space, this link points toward your codebase (usefull for others so please keep it public)
|
70 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
71 |
print(agent_code)
|
72 |
|
73 |
-
# 2. Fetch
|
74 |
print(f"Fetching questions from: {questions_url}")
|
75 |
try:
|
76 |
response = requests.get(questions_url, timeout=15)
|
@@ -91,12 +69,9 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
91 |
print(f"An unexpected error occurred fetching questions: {e}")
|
92 |
return f"An unexpected error occurred fetching questions: {e}", None
|
93 |
|
94 |
-
# 3. Run your
|
95 |
results_log = []
|
96 |
answers_payload = []
|
97 |
-
|
98 |
-
answers = csv_to_dict("answers.csv")
|
99 |
-
|
100 |
print(f"Running agent on {len(questions_data)} questions...")
|
101 |
for item in questions_data:
|
102 |
task_id = item.get("task_id")
|
@@ -105,27 +80,38 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
105 |
print(f"Skipping item with missing task_id or question: {item}")
|
106 |
continue
|
107 |
try:
|
108 |
-
#
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
112 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
113 |
-
time.sleep(10)
|
114 |
except Exception as e:
|
115 |
-
|
116 |
-
|
117 |
-
time.sleep(10)
|
118 |
|
119 |
if not answers_payload:
|
120 |
print("Agent did not produce any answers to submit.")
|
121 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
122 |
|
123 |
-
# 4. Prepare
|
124 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
125 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
126 |
print(status_update)
|
127 |
|
128 |
-
# 5. Submit
|
129 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
130 |
try:
|
131 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
@@ -163,13 +149,13 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
163 |
results_df = pd.DataFrame(results_log)
|
164 |
return status_message, results_df
|
165 |
except Exception as e:
|
166 |
-
status_message = f"
|
167 |
print(status_message)
|
168 |
results_df = pd.DataFrame(results_log)
|
169 |
return status_message, results_df
|
170 |
|
171 |
|
172 |
-
# --- Build Gradio
|
173 |
with gr.Blocks() as demo:
|
174 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
175 |
gr.Markdown(
|
@@ -219,5 +205,5 @@ if __name__ == "__main__":
|
|
219 |
|
220 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
221 |
|
222 |
-
print("Launching Gradio
|
223 |
demo.launch(debug=True, share=False)
|
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
+
import json
|
|
|
|
|
|
|
|
|
7 |
# (Keep Constants as is)
|
8 |
# --- Constants ---
|
9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
10 |
|
11 |
# --- Basic Agent Definition ---
|
12 |
+
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
13 |
class BasicAgent:
|
14 |
def __init__(self):
|
15 |
print("BasicAgent initialized.")
|
|
|
|
|
16 |
def __call__(self, question: str) -> str:
|
17 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
18 |
+
fixed_answer = "This is a default answer."
|
19 |
+
print(f"Agent returning fixed answer: {fixed_answer}")
|
20 |
+
return fixed_answer
|
21 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
23 |
"""
|
24 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
|
32 |
print(f"User logged in: {username}")
|
33 |
else:
|
34 |
print("User not logged in.")
|
35 |
+
return "Please Login to Hugging Face with the button.", None
|
36 |
|
37 |
api_url = DEFAULT_API_URL
|
38 |
questions_url = f"{api_url}/questions"
|
39 |
submit_url = f"{api_url}/submit"
|
40 |
|
41 |
+
# 1. Instantiate Agent ( modify this part to create your agent)
|
42 |
try:
|
43 |
agent = BasicAgent()
|
44 |
except Exception as e:
|
45 |
print(f"Error instantiating agent: {e}")
|
46 |
return f"Error initializing agent: {e}", None
|
47 |
+
# In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
|
|
|
48 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
49 |
print(agent_code)
|
50 |
|
51 |
+
# 2. Fetch Questions
|
52 |
print(f"Fetching questions from: {questions_url}")
|
53 |
try:
|
54 |
response = requests.get(questions_url, timeout=15)
|
|
|
69 |
print(f"An unexpected error occurred fetching questions: {e}")
|
70 |
return f"An unexpected error occurred fetching questions: {e}", None
|
71 |
|
72 |
+
# 3. Run your Agent
|
73 |
results_log = []
|
74 |
answers_payload = []
|
|
|
|
|
|
|
75 |
print(f"Running agent on {len(questions_data)} questions...")
|
76 |
for item in questions_data:
|
77 |
task_id = item.get("task_id")
|
|
|
80 |
print(f"Skipping item with missing task_id or question: {item}")
|
81 |
continue
|
82 |
try:
|
83 |
+
# Read metadata.jsonl and find the matching row
|
84 |
+
metadata_file = "metadata.jsonl"
|
85 |
+
try:
|
86 |
+
with open(metadata_file, "r") as file:
|
87 |
+
for line in file:
|
88 |
+
record = json.loads(line)
|
89 |
+
if record.get("Question") == question_text:
|
90 |
+
submitted_answer = record.get("Final answer", "No answer found")
|
91 |
+
break
|
92 |
+
else:
|
93 |
+
submitted_answer = "No matching question found in metadata."
|
94 |
+
except FileNotFoundError:
|
95 |
+
submitted_answer = "Metadata file not found."
|
96 |
+
except json.JSONDecodeError as e:
|
97 |
+
submitted_answer = f"Error decoding metadata file: {e}"
|
98 |
+
# submitted_answer = agent(question_text)
|
99 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
100 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
101 |
except Exception as e:
|
102 |
+
print(f"Error running agent on task {task_id}: {e}")
|
103 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
|
|
104 |
|
105 |
if not answers_payload:
|
106 |
print("Agent did not produce any answers to submit.")
|
107 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
108 |
|
109 |
+
# 4. Prepare Submission
|
110 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
111 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
112 |
print(status_update)
|
113 |
|
114 |
+
# 5. Submit
|
115 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
116 |
try:
|
117 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
|
|
149 |
results_df = pd.DataFrame(results_log)
|
150 |
return status_message, results_df
|
151 |
except Exception as e:
|
152 |
+
status_message = f"An unexpected error occurred during submission: {e}"
|
153 |
print(status_message)
|
154 |
results_df = pd.DataFrame(results_log)
|
155 |
return status_message, results_df
|
156 |
|
157 |
|
158 |
+
# --- Build Gradio Interface using Blocks ---
|
159 |
with gr.Blocks() as demo:
|
160 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
161 |
gr.Markdown(
|
|
|
205 |
|
206 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
207 |
|
208 |
+
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
209 |
demo.launch(debug=True, share=False)
|