Spaces:
Sleeping
Sleeping
Update app.py
#21
by
olcapone
- opened
app.py
CHANGED
|
@@ -711,13 +711,14 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 711 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 712 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 713 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else ""
|
|
|
|
| 714 |
|
| 715 |
if profile:
|
| 716 |
username= f"{profile.username}"
|
| 717 |
print(f"User logged in: {username}")
|
| 718 |
else:
|
| 719 |
print("User not logged in.")
|
| 720 |
-
return "Please Login to Hugging Face with the button.",
|
| 721 |
|
| 722 |
api_url = DEFAULT_API_URL
|
| 723 |
questions_url = f"{api_url}/questions"
|
|
@@ -746,7 +747,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 746 |
print(f"Fetched {len(questions_data)} questions.")
|
| 747 |
except requests.exceptions.RequestException as e:
|
| 748 |
print(f"Error fetching questions: {e}")
|
| 749 |
-
return f"Error fetching questions: {e}",
|
| 750 |
except requests.exceptions.JSONDecodeError as e:
|
| 751 |
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 752 |
print(f"Response text: {response.text[:500]}")
|
|
@@ -757,7 +758,6 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 757 |
|
| 758 |
# 3. Run your Agent
|
| 759 |
results_log = []
|
| 760 |
-
answers_payload = []
|
| 761 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 762 |
for item in questions_data:
|
| 763 |
task_id = item.get("task_id")
|
|
@@ -775,7 +775,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 775 |
|
| 776 |
if not answers_payload:
|
| 777 |
print("Agent did not produce any answers to submit.")
|
| 778 |
-
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 779 |
|
| 780 |
# 4. Prepare Submission
|
| 781 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
|
@@ -797,7 +797,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 797 |
)
|
| 798 |
print("Submission successful.")
|
| 799 |
results_df = pd.DataFrame(results_log)
|
| 800 |
-
|
|
|
|
| 801 |
except requests.exceptions.HTTPError as e:
|
| 802 |
error_detail = f"Server responded with status {e.response.status_code}."
|
| 803 |
try:
|
|
@@ -823,20 +824,21 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 823 |
status_message = f"An unexpected error occurred during submission: {e}"
|
| 824 |
print(status_message)
|
| 825 |
results_df = pd.DataFrame(results_log)
|
| 826 |
-
return status_message, results_df
|
| 827 |
-
|
| 828 |
-
answers_state = gr.State([])
|
| 829 |
|
| 830 |
def build_jsonl(answers):
|
| 831 |
import json, os
|
| 832 |
path = "/tmp/submission.jsonl"
|
|
|
|
|
|
|
| 833 |
with open(path, "w", encoding="utf-8") as f:
|
| 834 |
for a in answers:
|
| 835 |
-
|
| 836 |
-
|
| 837 |
-
|
| 838 |
-
|
| 839 |
-
|
|
|
|
| 840 |
|
| 841 |
# --- Build Gradio Interface using Blocks ---
|
| 842 |
with gr.Blocks() as demo:
|
|
@@ -857,11 +859,12 @@ with gr.Blocks() as demo:
|
|
| 857 |
)
|
| 858 |
|
| 859 |
gr.LoginButton()
|
| 860 |
-
|
| 861 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 862 |
|
| 863 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 864 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
|
|
|
|
|
|
| 865 |
jsonl_file = gr.File(label="submission.jsonl", interactive=False)
|
| 866 |
save_btn = gr.Button("Build JSONL for Leaderboard")
|
| 867 |
|
|
|
|
| 711 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 712 |
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 713 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else ""
|
| 714 |
+
answers_payload = []
|
| 715 |
|
| 716 |
if profile:
|
| 717 |
username= f"{profile.username}"
|
| 718 |
print(f"User logged in: {username}")
|
| 719 |
else:
|
| 720 |
print("User not logged in.")
|
| 721 |
+
return "Please Login to Hugging Face with the button.", pd.DataFrame([]), answers_payload
|
| 722 |
|
| 723 |
api_url = DEFAULT_API_URL
|
| 724 |
questions_url = f"{api_url}/questions"
|
|
|
|
| 747 |
print(f"Fetched {len(questions_data)} questions.")
|
| 748 |
except requests.exceptions.RequestException as e:
|
| 749 |
print(f"Error fetching questions: {e}")
|
| 750 |
+
return f"Error fetching questions: {e}", pd.DataFrame([]), answers_payload
|
| 751 |
except requests.exceptions.JSONDecodeError as e:
|
| 752 |
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 753 |
print(f"Response text: {response.text[:500]}")
|
|
|
|
| 758 |
|
| 759 |
# 3. Run your Agent
|
| 760 |
results_log = []
|
|
|
|
| 761 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 762 |
for item in questions_data:
|
| 763 |
task_id = item.get("task_id")
|
|
|
|
| 775 |
|
| 776 |
if not answers_payload:
|
| 777 |
print("Agent did not produce any answers to submit.")
|
| 778 |
+
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log), answers_payload
|
| 779 |
|
| 780 |
# 4. Prepare Submission
|
| 781 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
|
|
|
| 797 |
)
|
| 798 |
print("Submission successful.")
|
| 799 |
results_df = pd.DataFrame(results_log)
|
| 800 |
+
|
| 801 |
+
return final_status, results_df, answers_payload
|
| 802 |
except requests.exceptions.HTTPError as e:
|
| 803 |
error_detail = f"Server responded with status {e.response.status_code}."
|
| 804 |
try:
|
|
|
|
| 824 |
status_message = f"An unexpected error occurred during submission: {e}"
|
| 825 |
print(status_message)
|
| 826 |
results_df = pd.DataFrame(results_log)
|
| 827 |
+
return status_message, results_df, answers_payload
|
|
|
|
|
|
|
| 828 |
|
| 829 |
def build_jsonl(answers):
|
| 830 |
import json, os
|
| 831 |
path = "/tmp/submission.jsonl"
|
| 832 |
+
if not isinstance(answers, list):
|
| 833 |
+
answers = []
|
| 834 |
with open(path, "w", encoding="utf-8") as f:
|
| 835 |
for a in answers:
|
| 836 |
+
tid = a.get("task_id")
|
| 837 |
+
ans = a.get("submitted_answer")
|
| 838 |
+
if tid is None or ans is None:
|
| 839 |
+
continue
|
| 840 |
+
f.write(json.dumps({"task_id": tid, "model_answer": ans}, ensure_ascii=False) + "\n")
|
| 841 |
+
return path
|
| 842 |
|
| 843 |
# --- Build Gradio Interface using Blocks ---
|
| 844 |
with gr.Blocks() as demo:
|
|
|
|
| 859 |
)
|
| 860 |
|
| 861 |
gr.LoginButton()
|
|
|
|
| 862 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 863 |
|
| 864 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 865 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 866 |
+
|
| 867 |
+
answers_state = gr.State([])
|
| 868 |
jsonl_file = gr.File(label="submission.jsonl", interactive=False)
|
| 869 |
save_btn = gr.Button("Build JSONL for Leaderboard")
|
| 870 |
|