Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,25 +13,54 @@ except ImportError:
|
|
| 13 |
# --- Constants ---
|
| 14 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 15 |
|
| 16 |
-
# --- Gemini Agent Definition ---
|
| 17 |
class GeminiAgent:
|
| 18 |
def __init__(self):
|
| 19 |
print("GeminiAgent initialized.")
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
raise ValueError("GEMINI_API_KEY environment variable not set.")
|
| 23 |
-
self.client = genai.Client(api_key=api_key)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
prompt = f"Answer concisely without explanation: {question}"
|
| 28 |
response = self.client.models.generate_content(
|
| 29 |
model="gemini-2.0-flash", contents=prompt
|
| 30 |
)
|
| 31 |
-
|
| 32 |
-
print(f"GeminiAgent returning answer: {answer}")
|
| 33 |
-
return answer
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 36 |
space_id = os.getenv("SPACE_ID")
|
| 37 |
|
|
@@ -68,11 +97,10 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 68 |
print(f"Error fetching questions: {e}")
|
| 69 |
return f"Error fetching questions: {e}", None
|
| 70 |
except requests.exceptions.JSONDecodeError as e:
|
| 71 |
-
print(f"Error decoding JSON response
|
| 72 |
-
print(f"Response text: {response.text[:500]}")
|
| 73 |
return f"Error decoding server response for questions: {e}", None
|
| 74 |
except Exception as e:
|
| 75 |
-
print(f"
|
| 76 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 77 |
|
| 78 |
results_log = []
|
|
@@ -93,7 +121,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 93 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 94 |
|
| 95 |
if not answers_payload:
|
| 96 |
-
print("Agent did not produce any answers to submit.")
|
| 97 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 98 |
|
| 99 |
submission_data = {
|
|
@@ -101,9 +128,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 101 |
"agent_code": agent_code,
|
| 102 |
"answers": answers_payload
|
| 103 |
}
|
| 104 |
-
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 105 |
-
print(status_update)
|
| 106 |
-
|
| 107 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 108 |
try:
|
| 109 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
|
@@ -116,64 +140,31 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 116 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 117 |
f"Message: {result_data.get('message', 'No message received.')}"
|
| 118 |
)
|
| 119 |
-
|
| 120 |
-
results_df = pd.DataFrame(results_log)
|
| 121 |
-
return final_status, results_df
|
| 122 |
-
except requests.exceptions.HTTPError as e:
|
| 123 |
-
error_detail = f"Server responded with status {e.response.status_code}."
|
| 124 |
-
try:
|
| 125 |
-
error_json = e.response.json()
|
| 126 |
-
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
| 127 |
-
except requests.exceptions.JSONDecodeError:
|
| 128 |
-
error_detail += f" Response: {e.response.text[:500]}"
|
| 129 |
-
status_message = f"Submission Failed: {error_detail}"
|
| 130 |
-
print(status_message)
|
| 131 |
-
return status_message, pd.DataFrame(results_log)
|
| 132 |
-
except requests.exceptions.Timeout:
|
| 133 |
-
status_message = "Submission Failed: The request timed out."
|
| 134 |
-
print(status_message)
|
| 135 |
-
return status_message, pd.DataFrame(results_log)
|
| 136 |
except requests.exceptions.RequestException as e:
|
| 137 |
-
status_message = f"Submission Failed:
|
| 138 |
-
print(status_message)
|
| 139 |
return status_message, pd.DataFrame(results_log)
|
| 140 |
except Exception as e:
|
| 141 |
-
|
| 142 |
-
print(status_message)
|
| 143 |
-
return status_message, pd.DataFrame(results_log)
|
| 144 |
|
| 145 |
-
# ---
|
| 146 |
with gr.Blocks() as demo:
|
| 147 |
gr.Markdown("# Gemini Agent Evaluation Runner")
|
| 148 |
-
gr.Markdown(
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
---
|
| 159 |
-
**Disclaimers:**
|
| 160 |
-
Once clicking on the "submit" button, it can take quite some time (this is the
|
| 161 |
-
time for the agent to go through all the questions).
|
| 162 |
-
This space provides a basic setup and is intentionally sub-optimal to
|
| 163 |
-
encourage you to develop your own, more robust solution. For instance, for the
|
| 164 |
-
delay process of the submit button, a solution could be to cache the answers and
|
| 165 |
-
submit in a separate action or even to answer the questions asynchronously.
|
| 166 |
-
"""
|
| 167 |
-
)
|
| 168 |
gr.LoginButton()
|
| 169 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 170 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 171 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 172 |
|
| 173 |
-
run_button.click(
|
| 174 |
-
fn=run_and_submit_all,
|
| 175 |
-
outputs=[status_output, results_table]
|
| 176 |
-
)
|
| 177 |
|
| 178 |
if __name__ == "__main__":
|
| 179 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
|
@@ -182,17 +173,17 @@ if __name__ == "__main__":
|
|
| 182 |
|
| 183 |
if space_host_startup:
|
| 184 |
print(f"β
SPACE_HOST found: {space_host_startup}")
|
| 185 |
-
print(f" Runtime URL
|
| 186 |
else:
|
| 187 |
-
print("βΉοΈ SPACE_HOST
|
| 188 |
|
| 189 |
if space_id_startup:
|
| 190 |
print(f"β
SPACE_ID found: {space_id_startup}")
|
| 191 |
-
print(f" Repo
|
| 192 |
-
print(f"
|
| 193 |
else:
|
| 194 |
-
print("βΉοΈ SPACE_ID
|
| 195 |
|
| 196 |
-
print("-"*
|
| 197 |
print("Launching Gradio Interface for Gemini Agent Evaluation...")
|
| 198 |
demo.launch(debug=True, share=False)
|
|
|
|
| 13 |
# --- Constants ---
|
| 14 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 15 |
|
| 16 |
+
# --- Gemini Agent Definition with Fallback ---
|
| 17 |
class GeminiAgent:
|
| 18 |
def __init__(self):
|
| 19 |
print("GeminiAgent initialized.")
|
| 20 |
+
self.primary_key = os.getenv("GEMINI_API_KEY")
|
| 21 |
+
self.backup_key = os.getenv("GEMINI_API_KEY_2")
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
if not self.primary_key and not self.backup_key:
|
| 24 |
+
raise ValueError("No Gemini API key found in environment variables.")
|
| 25 |
+
|
| 26 |
+
self.client = None
|
| 27 |
+
self.active_key = None
|
| 28 |
+
self._init_client(self.primary_key)
|
| 29 |
+
|
| 30 |
+
def _init_client(self, key):
|
| 31 |
+
try:
|
| 32 |
+
self.client = genai.Client(api_key=key)
|
| 33 |
+
self.active_key = key
|
| 34 |
+
print(f"β
Initialized Gemini client with key ending in ...{key[-4:]}")
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"β οΈ Failed to initialize Gemini client with key ending in ...{key[-4:]}: {e}")
|
| 37 |
+
self.client = None
|
| 38 |
+
|
| 39 |
+
def _generate_response(self, question):
|
| 40 |
prompt = f"Answer concisely without explanation: {question}"
|
| 41 |
response = self.client.models.generate_content(
|
| 42 |
model="gemini-2.0-flash", contents=prompt
|
| 43 |
)
|
| 44 |
+
return response.text.strip().rstrip(".!?")
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
def __call__(self, question: str) -> str:
|
| 47 |
+
print(f"GeminiAgent received question (first 50 chars): {question[:50]}...")
|
| 48 |
+
try:
|
| 49 |
+
return self._generate_response(question)
|
| 50 |
+
except Exception as e1:
|
| 51 |
+
print(f"β Primary key failed: {e1}")
|
| 52 |
+
if self.backup_key and self.backup_key != self.active_key:
|
| 53 |
+
try:
|
| 54 |
+
print("π Switching to backup key...")
|
| 55 |
+
self._init_client(self.backup_key)
|
| 56 |
+
return self._generate_response(question)
|
| 57 |
+
except Exception as e2:
|
| 58 |
+
print(f"β Backup key also failed: {e2}")
|
| 59 |
+
return f"ERROR: Gemini agent failed with both keys."
|
| 60 |
+
else:
|
| 61 |
+
return f"ERROR: Gemini agent failed with primary key only."
|
| 62 |
+
|
| 63 |
+
# --- Evaluation & Submission Function ---
|
| 64 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 65 |
space_id = os.getenv("SPACE_ID")
|
| 66 |
|
|
|
|
| 97 |
print(f"Error fetching questions: {e}")
|
| 98 |
return f"Error fetching questions: {e}", None
|
| 99 |
except requests.exceptions.JSONDecodeError as e:
|
| 100 |
+
print(f"Error decoding JSON response: {e}")
|
|
|
|
| 101 |
return f"Error decoding server response for questions: {e}", None
|
| 102 |
except Exception as e:
|
| 103 |
+
print(f"Unexpected error: {e}")
|
| 104 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 105 |
|
| 106 |
results_log = []
|
|
|
|
| 121 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
| 122 |
|
| 123 |
if not answers_payload:
|
|
|
|
| 124 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 125 |
|
| 126 |
submission_data = {
|
|
|
|
| 128 |
"agent_code": agent_code,
|
| 129 |
"answers": answers_payload
|
| 130 |
}
|
|
|
|
|
|
|
|
|
|
| 131 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
| 132 |
try:
|
| 133 |
response = requests.post(submit_url, json=submission_data, timeout=60)
|
|
|
|
| 140 |
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
| 141 |
f"Message: {result_data.get('message', 'No message received.')}"
|
| 142 |
)
|
| 143 |
+
return final_status, pd.DataFrame(results_log)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
except requests.exceptions.RequestException as e:
|
| 145 |
+
status_message = f"Submission Failed: {e}"
|
|
|
|
| 146 |
return status_message, pd.DataFrame(results_log)
|
| 147 |
except Exception as e:
|
| 148 |
+
return f"Unexpected error during submission: {e}", pd.DataFrame(results_log)
|
|
|
|
|
|
|
| 149 |
|
| 150 |
+
# --- Gradio UI ---
|
| 151 |
with gr.Blocks() as demo:
|
| 152 |
gr.Markdown("# Gemini Agent Evaluation Runner")
|
| 153 |
+
gr.Markdown("""
|
| 154 |
+
**Instructions:**
|
| 155 |
+
1. Log in to Hugging Face using the button below.
|
| 156 |
+
2. Click the button to run your agent and submit your answers.
|
| 157 |
+
|
| 158 |
+
---
|
| 159 |
+
β οΈ Evaluation may take time. Keep this tab open until completion.
|
| 160 |
+
""")
|
| 161 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
gr.LoginButton()
|
| 163 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 164 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 165 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 166 |
|
| 167 |
+
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
if __name__ == "__main__":
|
| 170 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
|
|
|
| 173 |
|
| 174 |
if space_host_startup:
|
| 175 |
print(f"β
SPACE_HOST found: {space_host_startup}")
|
| 176 |
+
print(f" Runtime URL: https://{space_host_startup}.hf.space")
|
| 177 |
else:
|
| 178 |
+
print("βΉοΈ SPACE_HOST not found (likely running locally).")
|
| 179 |
|
| 180 |
if space_id_startup:
|
| 181 |
print(f"β
SPACE_ID found: {space_id_startup}")
|
| 182 |
+
print(f" Repo: https://huggingface.co/spaces/{space_id_startup}")
|
| 183 |
+
print(f" Code: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 184 |
else:
|
| 185 |
+
print("βΉοΈ SPACE_ID not found. Cannot link to code.")
|
| 186 |
|
| 187 |
+
print("-" * 70)
|
| 188 |
print("Launching Gradio Interface for Gemini Agent Evaluation...")
|
| 189 |
demo.launch(debug=True, share=False)
|