Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| import pandas as pd | |
| import json | |
| import inspect | |
| # --- Basic Agent Definition --- | |
| class BasicAgent: | |
| def __init__(self): | |
| print("BasicAgent initialized.") | |
| def __call__(self, question: str) -> str: | |
| print(f"Agent received question (first 50 chars): {question[:50]}...") | |
| fixed_answer = "This is a default answer." | |
| print(f"Agent returning fixed answer: {fixed_answer}") | |
| return fixed_answer | |
| # --- Main Function: run_and_save --- | |
| def run_and_save(profile: gr.OAuthProfile | None, task_id: int): | |
| """ | |
| Loads questions.json, finds the question by task_id, | |
| runs the agent, and appends the answer to result_log.json. | |
| """ | |
| # --- Authentication Check --- | |
| if not profile: | |
| print("User not logged in.") | |
| return "Please Login to Hugging Face with the button.", None | |
| username = profile.username | |
| print(f"β User logged in: {username}") | |
| # --- Instantiate Agent --- | |
| try: | |
| agent = BasicAgent() | |
| except Exception as e: | |
| return f"Error initializing agent: {e}", None | |
| # --- Load Questions --- | |
| questions_path = "questions.json" | |
| if not os.path.exists(questions_path): | |
| return f"β {questions_path} not found.", None | |
| try: | |
| with open(questions_path, "r", encoding="utf-8") as f: | |
| questions_data = json.load(f) | |
| except json.JSONDecodeError as e: | |
| return f"Error decoding {questions_path}: {e}", None | |
| if not isinstance(questions_data, list): | |
| return "Invalid format: questions.json must contain a list.", None | |
| # --- Find Question by Task ID --- | |
| question_item = next((q for q in questions_data if q.get("task_id") == task_id), None) | |
| if not question_item: | |
| return f"No question found for task_id {task_id}.", None | |
| question_text = question_item.get("question", "") | |
| print(f"π¦ Running agent for task_id {task_id}...") | |
| # --- Run Agent --- | |
| try: | |
| submitted_answer = agent(question_text) | |
| result = { | |
| "Task ID": task_id, | |
| "Question": question_text, | |
| "Submitted Answer": submitted_answer | |
| } | |
| except Exception as e: | |
| result = { | |
| "Task ID": task_id, | |
| "Question": question_text, | |
| "Submitted Answer": f"AGENT ERROR: {e}" | |
| } | |
| # --- Save to result_log.json --- | |
| result_log_path = "result_log.json" | |
| if os.path.exists(result_log_path): | |
| try: | |
| with open(result_log_path, "r", encoding="utf-8") as f: | |
| result_log = json.load(f) | |
| if not isinstance(result_log, list): | |
| result_log = [] | |
| except Exception: | |
| result_log = [] | |
| else: | |
| result_log = [] | |
| result_log.append(result) | |
| with open(result_log_path, "w", encoding="utf-8") as f: | |
| json.dump(result_log, f, indent=4, ensure_ascii=False) | |
| print(f"β Result saved to {result_log_path}") | |
| results_df = pd.DataFrame([result]) | |
| return f"β Answer saved locally for task_id {task_id}", results_df | |
| # --- Gradio Interface --- | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# JUST ANOTHER AGENT") | |
| gr.Markdown( | |
| """ | |
| **Instructions:** | |
| 1. Login to Hugging Face using the button below. | |
| 2. Enter the Task ID from your `questions.json`. | |
| 3. Click **Run Agent & Save Answer**. | |
| 4. The result will be appended to `result_log.json` for manual upload. | |
| """ | |
| ) | |
| profile = gr.LoginButton() | |
| task_id_input = gr.Number(label="Enter Task ID", precision=0) | |
| run_button = gr.Button("Run Agent & Save Answer") | |
| status_output = gr.Textbox(label="Status", lines=3, interactive=False) | |
| results_table = gr.DataFrame(label="Result Log (Latest Entry)") | |
| run_button.click( | |
| fn=run_and_save, | |
| inputs=[profile, task_id_input], | |
| outputs=[status_output, results_table] | |
| ) | |
| if __name__ == "__main__": | |
| print("Launching Gradio interface...") | |
| demo.launch(debug=True, share=False) | |