Update app.py
Browse files
app.py
CHANGED
@@ -3,32 +3,107 @@ 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 |
# --- Basic Agent Definition ---
|
12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
13 |
class BasicAgent:
|
14 |
def __init__(self):
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
"""
|
24 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
25 |
and displays the results.
|
26 |
"""
|
27 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
28 |
-
space_id = os.getenv("SPACE_ID")
|
29 |
|
30 |
if profile:
|
31 |
-
username= f"{profile.username}"
|
32 |
print(f"User logged in: {username}")
|
33 |
else:
|
34 |
print("User not logged in.")
|
@@ -55,16 +130,16 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
55 |
response.raise_for_status()
|
56 |
questions_data = response.json()
|
57 |
if not questions_data:
|
58 |
-
|
59 |
-
|
60 |
print(f"Fetched {len(questions_data)} questions.")
|
61 |
except requests.exceptions.RequestException as e:
|
62 |
print(f"Error fetching questions: {e}")
|
63 |
return f"Error fetching questions: {e}", None
|
64 |
except requests.exceptions.JSONDecodeError as e:
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
except Exception as e:
|
69 |
print(f"An unexpected error occurred fetching questions: {e}")
|
70 |
return f"An unexpected error occurred fetching questions: {e}", None
|
@@ -81,18 +156,36 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
81 |
continue
|
82 |
try:
|
83 |
submitted_answer = agent(question_text)
|
84 |
-
answers_payload.append(
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
except Exception as e:
|
87 |
-
|
88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
if not answers_payload:
|
91 |
print("Agent did not produce any answers to submit.")
|
92 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
93 |
|
94 |
-
# 4. Prepare Submission
|
95 |
-
submission_data = {
|
|
|
|
|
|
|
|
|
96 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
97 |
print(status_update)
|
98 |
|
@@ -162,20 +255,19 @@ with gr.Blocks() as demo:
|
|
162 |
|
163 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
164 |
|
165 |
-
status_output = gr.Textbox(
|
|
|
|
|
166 |
# Removed max_rows=10 from DataFrame constructor
|
167 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
168 |
|
169 |
-
run_button.click(
|
170 |
-
fn=run_and_submit_all,
|
171 |
-
outputs=[status_output, results_table]
|
172 |
-
)
|
173 |
|
174 |
if __name__ == "__main__":
|
175 |
-
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
176 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
177 |
space_host_startup = os.getenv("SPACE_HOST")
|
178 |
-
space_id_startup = os.getenv("SPACE_ID")
|
179 |
|
180 |
if space_host_startup:
|
181 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
@@ -183,14 +275,18 @@ if __name__ == "__main__":
|
|
183 |
else:
|
184 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
185 |
|
186 |
-
if space_id_startup:
|
187 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
188 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
189 |
-
print(
|
|
|
|
|
190 |
else:
|
191 |
-
print(
|
|
|
|
|
192 |
|
193 |
-
print("-"*(60 + len(" App Starting ")) + "\n")
|
194 |
|
195 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
196 |
-
demo.launch(debug=True, share=False)
|
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
from openai import OpenAI
|
8 |
+
from tenacity import retry, stop_after_attempt, wait_exponential
|
9 |
+
|
10 |
+
# Load environment variables
|
11 |
+
load_dotenv()
|
12 |
|
13 |
# (Keep Constants as is)
|
14 |
# --- Constants ---
|
15 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
16 |
+
OPENAI_MODEL = "openai/gpt-4.1-nano" # or "gpt-3.5-turbo" based on your preference
|
17 |
+
|
18 |
|
19 |
# --- Basic Agent Definition ---
|
20 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
21 |
class BasicAgent:
|
22 |
def __init__(self):
|
23 |
+
"""Initialize the agent with OpenAI client and setup."""
|
24 |
+
print("BasicAgent initializing...")
|
25 |
+
self.client = OpenAI(api_key="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",base_url="https://models.github.ai/inference")
|
26 |
+
if not os.getenv("OPENAI_API_KEY"):
|
27 |
+
raise ValueError("OPENAI_API_KEY environment variable is not set")
|
28 |
+
print("BasicAgent initialized successfully.")
|
29 |
+
|
30 |
+
@retry(
|
31 |
+
stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10)
|
32 |
+
)
|
33 |
+
def _get_completion(self, prompt: str) -> str:
|
34 |
+
"""Get completion from OpenAI with retry logic."""
|
35 |
+
try:
|
36 |
+
response = self.client.chat.completions.create(
|
37 |
+
model=OPENAI_MODEL,
|
38 |
+
messages=[
|
39 |
+
{
|
40 |
+
"role": "system",
|
41 |
+
"content": """You are a helpful AI assistant designed to answer questions from the GAIA benchmark.
|
42 |
+
Follow these guidelines:
|
43 |
+
1. Provide clear, concise, and accurate answers
|
44 |
+
2. If a question requires specific steps or calculations, show them clearly
|
45 |
+
3. Format your response in a clean, readable way
|
46 |
+
4. Be precise and avoid ambiguity
|
47 |
+
5. If you're not completely sure about an answer, state your confidence level
|
48 |
+
Remember: Your answers will be evaluated through exact matching.""",
|
49 |
+
},
|
50 |
+
{"role": "user", "content": prompt},
|
51 |
+
],
|
52 |
+
temperature=0.2, # Lower temperature for more consistent outputs
|
53 |
+
max_tokens=1000,
|
54 |
+
)
|
55 |
+
return response.choices[0].message.content.strip()
|
56 |
+
except Exception as e:
|
57 |
+
print(f"Error in OpenAI API call: {e}")
|
58 |
+
raise
|
59 |
+
|
60 |
+
def _preprocess_question(self, question: str) -> str:
|
61 |
+
"""Preprocess the question to enhance clarity and context."""
|
62 |
+
enhanced_prompt = f"""Please analyze and answer the following question from the GAIA benchmark.
|
63 |
+
Question: {question}
|
64 |
+
|
65 |
+
Provide a clear, specific answer that can be evaluated through exact matching.
|
66 |
+
If the question requires multiple steps, please show your reasoning but ensure the final answer is clearly stated.
|
67 |
+
"""
|
68 |
+
return enhanced_prompt
|
69 |
+
|
70 |
def __call__(self, question: str) -> str:
|
71 |
+
"""Process the question and return an answer."""
|
72 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
|
|
|
|
|
|
73 |
|
74 |
+
try:
|
75 |
+
# Preprocess the question
|
76 |
+
enhanced_prompt = self._preprocess_question(question)
|
77 |
+
|
78 |
+
# Get completion from OpenAI
|
79 |
+
response = self._get_completion(enhanced_prompt)
|
80 |
+
|
81 |
+
# Extract the final answer
|
82 |
+
# If the response contains multiple lines or explanations,
|
83 |
+
# we'll try to extract just the final answer
|
84 |
+
answer_lines = response.strip().split("\n")
|
85 |
+
final_answer = answer_lines[-1].strip()
|
86 |
+
|
87 |
+
# Log the response for debugging
|
88 |
+
print(f"Agent generated answer: {final_answer[:100]}...")
|
89 |
+
|
90 |
+
return final_answer
|
91 |
+
|
92 |
+
except Exception as e:
|
93 |
+
print(f"Error processing question: {e}")
|
94 |
+
return f"Error: {str(e)}"
|
95 |
+
|
96 |
+
|
97 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
98 |
"""
|
99 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
100 |
and displays the results.
|
101 |
"""
|
102 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
103 |
+
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
104 |
|
105 |
if profile:
|
106 |
+
username = f"{profile.username}"
|
107 |
print(f"User logged in: {username}")
|
108 |
else:
|
109 |
print("User not logged in.")
|
|
|
130 |
response.raise_for_status()
|
131 |
questions_data = response.json()
|
132 |
if not questions_data:
|
133 |
+
print("Fetched questions list is empty.")
|
134 |
+
return "Fetched questions list is empty or invalid format.", None
|
135 |
print(f"Fetched {len(questions_data)} questions.")
|
136 |
except requests.exceptions.RequestException as e:
|
137 |
print(f"Error fetching questions: {e}")
|
138 |
return f"Error fetching questions: {e}", None
|
139 |
except requests.exceptions.JSONDecodeError as e:
|
140 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
141 |
+
print(f"Response text: {response.text[:500]}")
|
142 |
+
return f"Error decoding server response for questions: {e}", None
|
143 |
except Exception as e:
|
144 |
print(f"An unexpected error occurred fetching questions: {e}")
|
145 |
return f"An unexpected error occurred fetching questions: {e}", None
|
|
|
156 |
continue
|
157 |
try:
|
158 |
submitted_answer = agent(question_text)
|
159 |
+
answers_payload.append(
|
160 |
+
{"task_id": task_id, "submitted_answer": submitted_answer}
|
161 |
+
)
|
162 |
+
results_log.append(
|
163 |
+
{
|
164 |
+
"Task ID": task_id,
|
165 |
+
"Question": question_text,
|
166 |
+
"Submitted Answer": submitted_answer,
|
167 |
+
}
|
168 |
+
)
|
169 |
except Exception as e:
|
170 |
+
print(f"Error running agent on task {task_id}: {e}")
|
171 |
+
results_log.append(
|
172 |
+
{
|
173 |
+
"Task ID": task_id,
|
174 |
+
"Question": question_text,
|
175 |
+
"Submitted Answer": f"AGENT ERROR: {e}",
|
176 |
+
}
|
177 |
+
)
|
178 |
|
179 |
if not answers_payload:
|
180 |
print("Agent did not produce any answers to submit.")
|
181 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
182 |
|
183 |
+
# 4. Prepare Submission
|
184 |
+
submission_data = {
|
185 |
+
"username": username.strip(),
|
186 |
+
"agent_code": agent_code,
|
187 |
+
"answers": answers_payload,
|
188 |
+
}
|
189 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
190 |
print(status_update)
|
191 |
|
|
|
255 |
|
256 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
257 |
|
258 |
+
status_output = gr.Textbox(
|
259 |
+
label="Run Status / Submission Result", lines=5, interactive=False
|
260 |
+
)
|
261 |
# Removed max_rows=10 from DataFrame constructor
|
262 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
263 |
|
264 |
+
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
|
|
|
|
|
|
265 |
|
266 |
if __name__ == "__main__":
|
267 |
+
print("\n" + "-" * 30 + " App Starting " + "-" * 30)
|
268 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
269 |
space_host_startup = os.getenv("SPACE_HOST")
|
270 |
+
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
271 |
|
272 |
if space_host_startup:
|
273 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
|
|
275 |
else:
|
276 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
277 |
|
278 |
+
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
279 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
280 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
281 |
+
print(
|
282 |
+
f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main"
|
283 |
+
)
|
284 |
else:
|
285 |
+
print(
|
286 |
+
"ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined."
|
287 |
+
)
|
288 |
|
289 |
+
print("-" * (60 + len(" App Starting ")) + "\n")
|
290 |
|
291 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
292 |
+
demo.launch(debug=True, share=False)
|