Spaces:
Build error
Build error
Download app.py from Asapyams/Final_Agent_For_Submission: direct link, hf CLI and curl.
- Browser
- Download file 3.67 kB
-
https://huggingface.co/spaces/Asapyams/Final_Agent_For_Submission/resolve/main/app.py
- Command line
-
hf download hf://spaces/Asapyams/Final_Agent_For_Submission/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Asapyams/Final_Agent_For_Submission/resolve/main/app.py
3.67 kB
| import os | |
| import gradio as gr | |
| import requests | |
| import pandas as pd | |
| from dotenv import load_dotenv | |
| from agents.agent import GAIAAgent # or GeminiAgent, whichever you’re using | |
| load_dotenv() | |
| # Constants | |
| DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" | |
| def run_and_submit_all(username: str): | |
| """ | |
| Fetches all questions, runs the agent on them, submits all answers, | |
| and displays the results. | |
| """ | |
| username = username.strip() | |
| if not username: | |
| return "Please enter your Hugging Face username.", None | |
| api_url = DEFAULT_API_URL | |
| questions_url = f"{api_url}/questions" | |
| submit_url = f"{api_url}/submit" | |
| # 1. Instantiate Agent | |
| try: | |
| # if your agent class requires an API key, set it in env and pass it here | |
| agent = GAIAAgent() | |
| except Exception as e: | |
| return f"Error initializing agent: {e}", None | |
| # 2. Fetch Questions | |
| try: | |
| resp = requests.get(questions_url, timeout=15) | |
| resp.raise_for_status() | |
| questions_data = resp.json() | |
| except Exception as e: | |
| return f"Error fetching questions: {e}", None | |
| if not questions_data: | |
| return "No questions retrieved.", None | |
| # 3. Run Agent | |
| results_log = [] | |
| answers_payload = [] | |
| for item in questions_data: | |
| task_id = item.get("task_id") | |
| question_text = item.get("question") | |
| if not task_id or question_text is None: | |
| continue | |
| try: | |
| ans = agent.answer_question(question_text) | |
| except Exception as e: | |
| ans = f"AGENT ERROR: {e}" | |
| results_log.append({ | |
| "Task ID": task_id, | |
| "Question": question_text, | |
| "Submitted Answer": ans | |
| }) | |
| answers_payload.append({ | |
| "task_id": task_id, | |
| "submitted_answer": ans | |
| }) | |
| if not answers_payload: | |
| return "Agent produced no answers.", pd.DataFrame(results_log) | |
| # 4. Submit | |
| submission = { | |
| "username": username, | |
| "agent_code": f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main", | |
| "answers": answers_payload | |
| } | |
| try: | |
| resp = requests.post(submit_url, json=submission, timeout=60) | |
| resp.raise_for_status() | |
| result = resp.json() | |
| status = ( | |
| f"Submission Successful!\n" | |
| f"User: {result.get('username')}\n" | |
| f"Score: {result.get('score')}% " | |
| f"({result.get('correct_count')}/{result.get('total_attempted')})\n" | |
| f"{result.get('message')}" | |
| ) | |
| except Exception as e: | |
| # try to extract any detail from server | |
| detail = getattr(e, 'response', None) | |
| if detail is not None: | |
| try: detail = detail.json().get('detail', detail.text) | |
| except: detail = detail.text | |
| else: | |
| detail = str(e) | |
| status = f"Submission Failed: {detail}" | |
| return status, pd.DataFrame(results_log) | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Basic Agent Evaluation Runner") | |
| gr.Markdown("Enter your Hugging Face username and click **Run & Submit**.") | |
| username_input = gr.Textbox(label="Hugging Face Username", placeholder="Your HF username") | |
| run_btn = gr.Button("Run Evaluation & Submit All Answers") | |
| status_out = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False) | |
| results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True) | |
| run_btn.click( | |
| fn=run_and_submit_all, | |
| inputs=[username_input], | |
| outputs=[status_out, results_table], | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(debug=True, share=False) | |