Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -90,28 +90,98 @@ class BasicAgent:
|
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
-
# ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
with gr.Blocks() as demo:
|
| 95 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 96 |
gr.Markdown(
|
| 97 |
"""
|
| 98 |
**Instructions:**
|
| 99 |
-
1.
|
| 100 |
-
2.
|
| 101 |
-
3.
|
| 102 |
---
|
| 103 |
-
**
|
| 104 |
-
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).
|
| 105 |
-
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.
|
| 106 |
"""
|
| 107 |
)
|
| 108 |
|
| 109 |
gr.LoginButton()
|
| 110 |
-
|
| 111 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 112 |
-
|
| 113 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 114 |
-
# Removed max_rows=10 from DataFrame constructor
|
| 115 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 116 |
|
| 117 |
run_button.click(
|
|
@@ -119,26 +189,22 @@ with gr.Blocks() as demo:
|
|
| 119 |
outputs=[status_output, results_table]
|
| 120 |
)
|
| 121 |
|
|
|
|
| 122 |
if __name__ == "__main__":
|
| 123 |
-
print("
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
| 127 |
-
|
| 128 |
-
if space_host_startup:
|
| 129 |
-
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 130 |
-
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 131 |
-
else:
|
| 132 |
-
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 133 |
|
| 134 |
-
if
|
| 135 |
-
print(f"✅
|
| 136 |
-
print(f"
|
| 137 |
-
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 138 |
else:
|
| 139 |
-
print("ℹ️
|
| 140 |
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
-
|
| 144 |
-
demo.launch(debug=True, share=False)
|
|
|
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
+
# --- Submission Function ---
|
| 94 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 95 |
+
|
| 96 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 97 |
+
space_id = os.getenv("SPACE_ID")
|
| 98 |
+
|
| 99 |
+
if profile:
|
| 100 |
+
username = profile.username
|
| 101 |
+
print(f"User logged in: {username}")
|
| 102 |
+
else:
|
| 103 |
+
return "Please Login to Hugging Face with the button.", None
|
| 104 |
+
|
| 105 |
+
api_url = DEFAULT_API_URL
|
| 106 |
+
questions_url = f"{api_url}/questions"
|
| 107 |
+
submit_url = f"{api_url}/submit"
|
| 108 |
+
|
| 109 |
+
try:
|
| 110 |
+
agent = BasicAgent()
|
| 111 |
+
except Exception as e:
|
| 112 |
+
return f"Error initializing agent: {e}", None
|
| 113 |
+
|
| 114 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 115 |
+
print(f"Agent repo: {agent_code}")
|
| 116 |
+
|
| 117 |
+
try:
|
| 118 |
+
response = requests.get(questions_url, timeout=15)
|
| 119 |
+
response.raise_for_status()
|
| 120 |
+
questions_data = response.json()
|
| 121 |
+
print(f"Fetched {len(questions_data)} questions.")
|
| 122 |
+
except Exception as e:
|
| 123 |
+
return f"Error fetching questions: {e}", None
|
| 124 |
+
|
| 125 |
+
results_log = []
|
| 126 |
+
answers_payload = []
|
| 127 |
+
|
| 128 |
+
for item in questions_data:
|
| 129 |
+
task_id = item.get("task_id")
|
| 130 |
+
question_text = item.get("question")
|
| 131 |
+
video_link = item.get("video_link")
|
| 132 |
+
|
| 133 |
+
if not task_id or question_text is None:
|
| 134 |
+
continue
|
| 135 |
+
|
| 136 |
+
try:
|
| 137 |
+
submitted_answer = agent(question_text, video_path=video_link)
|
| 138 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 139 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 140 |
+
except Exception as e:
|
| 141 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"ERROR: {e}"})
|
| 142 |
+
|
| 143 |
+
if not answers_payload:
|
| 144 |
+
return "No answers were submitted.", pd.DataFrame(results_log)
|
| 145 |
+
|
| 146 |
+
submission_data = {
|
| 147 |
+
"username": username.strip(),
|
| 148 |
+
"agent_code": agent_code,
|
| 149 |
+
"answers": answers_payload
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
try:
|
| 153 |
+
response = requests.post(submit_url, json=submission_data, timeout=60)
|
| 154 |
+
response.raise_for_status()
|
| 155 |
+
result_data = response.json()
|
| 156 |
+
final_status = (
|
| 157 |
+
f"✅ Submission Successful!\n"
|
| 158 |
+
f"User: {result_data.get('username')}\n"
|
| 159 |
+
f"Score: {result_data.get('score', 'N/A')}% "
|
| 160 |
+
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')})\n"
|
| 161 |
+
f"Message: {result_data.get('message', '')}"
|
| 162 |
+
)
|
| 163 |
+
return final_status, pd.DataFrame(results_log)
|
| 164 |
+
except Exception as e:
|
| 165 |
+
return f"Submission Failed: {e}", pd.DataFrame(results_log)
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
# --- Gradio Interface ---
|
| 169 |
with gr.Blocks() as demo:
|
| 170 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
| 171 |
gr.Markdown(
|
| 172 |
"""
|
| 173 |
**Instructions:**
|
| 174 |
+
1. Clone this space and modify the agent logic if desired.
|
| 175 |
+
2. Log in to Hugging Face with the button below.
|
| 176 |
+
3. Click 'Run Evaluation & Submit All Answers' to evaluate and submit your agent.
|
| 177 |
---
|
| 178 |
+
**Note:** This process may take several minutes depending on the number of questions.
|
|
|
|
|
|
|
| 179 |
"""
|
| 180 |
)
|
| 181 |
|
| 182 |
gr.LoginButton()
|
|
|
|
| 183 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
|
|
|
| 184 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
|
|
|
| 185 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 186 |
|
| 187 |
run_button.click(
|
|
|
|
| 189 |
outputs=[status_output, results_table]
|
| 190 |
)
|
| 191 |
|
| 192 |
+
|
| 193 |
if __name__ == "__main__":
|
| 194 |
+
print("-" * 30 + " App Starting " + "-" * 30)
|
| 195 |
+
space_host = os.getenv("SPACE_HOST")
|
| 196 |
+
space_id = os.getenv("SPACE_ID")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
+
if space_host:
|
| 199 |
+
print(f"✅ SPACE_HOST: {space_host}")
|
| 200 |
+
print(f" → https://{space_host}.hf.space")
|
|
|
|
| 201 |
else:
|
| 202 |
+
print("ℹ️ No SPACE_HOST set.")
|
| 203 |
|
| 204 |
+
if space_id:
|
| 205 |
+
print(f"✅ SPACE_ID: {space_id}")
|
| 206 |
+
print(f" → https://huggingface.co/spaces/{space_id}/tree/main")
|
| 207 |
+
else:
|
| 208 |
+
print("ℹ️ No SPACE_ID set.")
|
| 209 |
|
| 210 |
+
demo.launch(debug=True, share=False)
|
|
|