Spaces:
Sleeping
Sleeping
Update app.py (#21)
Browse files- Update app.py (885ebe5af1c2a14c10dcc580ac97089ba2fba055)
710 unmodified lines71171271371471571671771871972072072172272372422 unmodified lines7477487497497507517527534 unmodified lines75875976076076176276311 unmodified lines77577677777877877978078115 unmodified lines79779879980080080180280380419 unmodified lines82482582682682782882782882983083183283383483583583683783883983683783883984084184284384414 unmodified lines859860861860862863864865866867868869870710 unmodified lines # --- Determine HF Space Runtime URL and Repo URL --- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "" answers_payload = []
if profile: username= f"{profile.username}" print(f"User logged in: {username}") else: print("User not logged in.") return "Please Login to Hugging Face with the button.", None return "Please Login to Hugging Face with the button.", pd.DataFrame([]), answers_payload
api_url = DEFAULT_API_URL questions_url = f"{api_url}/questions"22 unmodified lines print(f"Fetched {len(questions_data)} questions.") except requests.exceptions.RequestException as e: print(f"Error fetching questions: {e}") return f"Error fetching questions: {e}", None return f"Error fetching questions: {e}", pd.DataFrame([]), answers_payload except requests.exceptions.JSONDecodeError as e: print(f"Error decoding JSON response from questions endpoint: {e}") print(f"Response text: {response.text[:500]}")4 unmodified lines
# 3. Run your Agent results_log = [] answers_payload = [] print(f"Running agent on {len(questions_data)} questions...") for item in questions_data: task_id = item.get("task_id")11 unmodified lines
if not answers_payload: print("Agent did not produce any answers to submit.") return "Agent did not produce any answers to submit.", pd.DataFrame(results_log) return "Agent did not produce any answers to submit.", pd.DataFrame(results_log), answers_payload
# 4. Prepare Submission submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}15 unmodified lines ) print("Submission successful.") results_df = pd.DataFrame(results_log)
return final_status, results_df return final_status, results_df, answers_payload except requests.exceptions.HTTPError as e: error_detail = f"Server responded with status {e.response.status_code}." try:19 unmodified lines status_message = f"An unexpected error occurred during submission: {e}" print(status_message) results_df = pd.DataFrame(results_log) return status_message, results_df
answers_state = gr.State([]) return status_message, results_df, answers_payload
def build_jsonl(answers): import json, os path = "/tmp/submission.jsonl" if not isinstance(answers, list): answers = [] with open(path, "w", encoding="utf-8") as f: for a in answers: f.write(json.dumps( {"task_id": a["task_id"], "model_answer": a["submitted_answer"]}, ensure_ascii=False ) + "\n") return path # gr.File/DownloadButton will serve this file tid = a.get("task_id") ans = a.get("submitted_answer") if tid is None or ans is None: continue f.write(json.dumps({"task_id": tid, "model_answer": ans}, ensure_ascii=False) + "\n") return path # --- Build Gradio Interface using Blocks ---with gr.Blocks() as demo:14 unmodified lines )
gr.LoginButton()
run_button = gr.Button("Run Evaluation & Submit All Answers")
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False) results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
answers_state = gr.State([]) jsonl_file = gr.File(label="submission.jsonl", interactive=False) save_btn = gr.Button("Build JSONL for Leaderboard") app.py+17-14