Spaces:
Runtime error
Runtime error
Mike Jay
commited on
Commit
·
cc6f511
1
Parent(s):
9095c2c
wip payload
Browse files- app.py +29 -29
- payload.py +26 -0
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import pandas as pd
|
|
| 6 |
|
| 7 |
from questions import get_questions_data
|
| 8 |
from submit import submit_answers
|
|
|
|
| 9 |
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
|
@@ -13,13 +14,24 @@ API_URL = DEFAULT_API_URL
|
|
| 13 |
QUESTIONS_URL = f"{API_URL}/questions"
|
| 14 |
SUBMIT_URL = f"{API_URL}/submit"
|
| 15 |
|
|
|
|
|
|
|
| 16 |
SPACE_ID = os.getenv("SPACE_ID")
|
| 17 |
if SPACE_ID:
|
| 18 |
AGENT_CODE = f"https://huggingface.co/spaces/{SPACE_ID}/tree/main"
|
| 19 |
print(f"Agent Code URL: {AGENT_CODE}")
|
| 20 |
|
| 21 |
|
| 22 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
"""Run and Submit All"""
|
| 24 |
if profile:
|
| 25 |
username = f"{profile.username}"
|
|
@@ -27,39 +39,25 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 27 |
else:
|
| 28 |
print("User not logged in.")
|
| 29 |
return "Please Login to Hugging Face with the button.", None
|
| 30 |
-
|
| 31 |
if not SPACE_ID:
|
| 32 |
print("SPACE_ID must be set")
|
| 33 |
return "SPACE_ID environment variable must be set", None
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
questions_data = get_questions_data(questions_url=QUESTIONS_URL)
|
| 36 |
if not questions_data:
|
| 37 |
print("Questions list is empty.")
|
| 38 |
return "Questions list is empty or invalid format.", None
|
| 39 |
print(f"Retrieved {len(questions_data)} questions.")
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
for item in questions_data:
|
| 48 |
-
task_id = item.get("task_id")
|
| 49 |
-
question_text = item.get("question")
|
| 50 |
-
if not task_id or question_text is None:
|
| 51 |
-
print(f"Skipping item with missing task_id or question: {item}")
|
| 52 |
-
continue
|
| 53 |
-
answers_payload.append(
|
| 54 |
-
{"task_id": task_id, "submitted_answer": submitted_answer}
|
| 55 |
-
)
|
| 56 |
-
results_log.append(
|
| 57 |
-
{
|
| 58 |
-
"Task ID": task_id,
|
| 59 |
-
"Question": question_text,
|
| 60 |
-
"Submitted Answer": submitted_answer,
|
| 61 |
-
}
|
| 62 |
-
)
|
| 63 |
|
| 64 |
submission_data = {
|
| 65 |
"username": username.strip(),
|
|
@@ -71,11 +69,13 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 71 |
results_df = pd.DataFrame(results_log)
|
| 72 |
print(f"Submitting {len(answers_payload)} answers to: {SUBMIT_URL}")
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
|
| 80 |
if final_status:
|
| 81 |
return final_status, results_df
|
|
|
|
| 6 |
|
| 7 |
from questions import get_questions_data
|
| 8 |
from submit import submit_answers
|
| 9 |
+
from payload import get_answer_payload_results_log
|
| 10 |
|
| 11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 12 |
|
|
|
|
| 14 |
QUESTIONS_URL = f"{API_URL}/questions"
|
| 15 |
SUBMIT_URL = f"{API_URL}/submit"
|
| 16 |
|
| 17 |
+
SAFETY_OFF = os.getenv("SAFETY_OFF")
|
| 18 |
+
|
| 19 |
SPACE_ID = os.getenv("SPACE_ID")
|
| 20 |
if SPACE_ID:
|
| 21 |
AGENT_CODE = f"https://huggingface.co/spaces/{SPACE_ID}/tree/main"
|
| 22 |
print(f"Agent Code URL: {AGENT_CODE}")
|
| 23 |
|
| 24 |
|
| 25 |
+
def _check_agent_dependent_environment_variables() -> str:
|
| 26 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 27 |
+
if not openai_api_key:
|
| 28 |
+
return "OPENAI_API_KEY environment variable must be set"
|
| 29 |
+
return None
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def run_and_submit_all(
|
| 33 |
+
profile: gr.OAuthProfile | None,
|
| 34 |
+
): # pylint: disable=too-many-return-statements
|
| 35 |
"""Run and Submit All"""
|
| 36 |
if profile:
|
| 37 |
username = f"{profile.username}"
|
|
|
|
| 39 |
else:
|
| 40 |
print("User not logged in.")
|
| 41 |
return "Please Login to Hugging Face with the button.", None
|
|
|
|
| 42 |
if not SPACE_ID:
|
| 43 |
print("SPACE_ID must be set")
|
| 44 |
return "SPACE_ID environment variable must be set", None
|
| 45 |
|
| 46 |
+
error_message = _check_agent_dependent_environment_variables()
|
| 47 |
+
if error_message:
|
| 48 |
+
return error_message, None
|
| 49 |
+
|
| 50 |
questions_data = get_questions_data(questions_url=QUESTIONS_URL)
|
| 51 |
if not questions_data:
|
| 52 |
print("Questions list is empty.")
|
| 53 |
return "Questions list is empty or invalid format.", None
|
| 54 |
print(f"Retrieved {len(questions_data)} questions.")
|
| 55 |
|
| 56 |
+
answers_payload, results_log, error_message = get_answer_payload_results_log(
|
| 57 |
+
questions_data=questions_data
|
| 58 |
+
)
|
| 59 |
+
if error_message:
|
| 60 |
+
return error_message, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
submission_data = {
|
| 63 |
"username": username.strip(),
|
|
|
|
| 69 |
results_df = pd.DataFrame(results_log)
|
| 70 |
print(f"Submitting {len(answers_payload)} answers to: {SUBMIT_URL}")
|
| 71 |
|
| 72 |
+
if SAFETY_OFF:
|
| 73 |
+
final_status, error_message = submit_answers(
|
| 74 |
+
submit_url=SUBMIT_URL, submission_data=submission_data
|
| 75 |
+
)
|
| 76 |
+
else:
|
| 77 |
+
final_status = "\n\tSAFETY_OFF bypass active\n"
|
| 78 |
+
error_message = None
|
| 79 |
|
| 80 |
if final_status:
|
| 81 |
return final_status, results_df
|
payload.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Runner for Agents to output answers"""
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def get_answer_payload_results_log(questions_data: dict) -> tuple:
|
| 5 |
+
"""Get Answer Payload, Results Log or Error Message"""
|
| 6 |
+
results_log = []
|
| 7 |
+
answers_payload = []
|
| 8 |
+
submitted_answer = "Mock Final Answer Test Data"
|
| 9 |
+
|
| 10 |
+
for item in questions_data:
|
| 11 |
+
task_id = item.get("task_id")
|
| 12 |
+
question_text = item.get("question")
|
| 13 |
+
if not task_id or question_text is None:
|
| 14 |
+
print(f"Skipping item with missing task_id or question: {item}")
|
| 15 |
+
continue
|
| 16 |
+
answers_payload.append(
|
| 17 |
+
{"task_id": task_id, "submitted_answer": submitted_answer}
|
| 18 |
+
)
|
| 19 |
+
results_log.append(
|
| 20 |
+
{
|
| 21 |
+
"Task ID": task_id,
|
| 22 |
+
"Question": question_text,
|
| 23 |
+
"Submitted Answer": submitted_answer,
|
| 24 |
+
}
|
| 25 |
+
)
|
| 26 |
+
return answers_payload, results_log, None
|