Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| API Testing Script for HF Agents Course Scoring API | |
| Tests connectivity, endpoints, and response formats | |
| """ | |
| import requests | |
| import json | |
| import sys | |
| from datetime import datetime | |
| # API Configuration | |
| API_BASE_URL = "https://agents-course-unit4-scoring.hf.space" | |
| QUESTIONS_URL = f"{API_BASE_URL}/questions" | |
| SUBMIT_URL = f"{API_BASE_URL}/submit" | |
| def test_connectivity(): | |
| """Test basic API connectivity""" | |
| print("π Testing API Connectivity...") | |
| print(f"π Base URL: {API_BASE_URL}") | |
| print("-" * 50) | |
| try: | |
| response = requests.get(API_BASE_URL, timeout=10) | |
| print(f"β Base URL Status: {response.status_code}") | |
| print(f"π Headers: {dict(response.headers)}") | |
| if response.text: | |
| print(f"π Response (first 200 chars): {response.text[:200]}") | |
| return True | |
| except requests.exceptions.RequestException as e: | |
| print(f"β Base URL Error: {e}") | |
| return False | |
| def test_questions_endpoint(): | |
| """Test questions endpoint""" | |
| print("\nπ Testing Questions Endpoint...") | |
| print(f"π Questions URL: {QUESTIONS_URL}") | |
| print("-" * 50) | |
| try: | |
| response = requests.get(QUESTIONS_URL, timeout=15) | |
| print(f"β Status Code: {response.status_code}") | |
| print(f"π Headers: {dict(response.headers)}") | |
| if response.status_code == 200: | |
| try: | |
| data = response.json() | |
| print(f"π Response Type: {type(data)}") | |
| if isinstance(data, list): | |
| print(f"π Questions Count: {len(data)}") | |
| if data: | |
| print(f"π Sample Question Keys: {list(data[0].keys()) if data else 'Empty'}") | |
| # Show first question safely | |
| if data: | |
| first_q = data[0] | |
| print(f"π First Question ID: {first_q.get('task_id', 'N/A')}") | |
| question_text = first_q.get('question', '') | |
| print(f"π First Question (50 chars): {question_text[:50]}...") | |
| else: | |
| print(f"π Response Content: {data}") | |
| return True, data | |
| except json.JSONDecodeError as e: | |
| print(f"β JSON Decode Error: {e}") | |
| print(f"π Raw Response (first 500 chars): {response.text[:500]}") | |
| return False, None | |
| else: | |
| print(f"β HTTP Error: {response.status_code}") | |
| print(f"π Error Response: {response.text[:500]}") | |
| return False, None | |
| except requests.exceptions.RequestException as e: | |
| print(f"β Request Error: {e}") | |
| return False, None | |
| def test_submit_endpoint(): | |
| """Test submit endpoint with minimal payload""" | |
| print("\nπ Testing Submit Endpoint...") | |
| print(f"π Submit URL: {SUBMIT_URL}") | |
| print("-" * 50) | |
| # Create minimal test payload | |
| test_payload = { | |
| "username": "test_user", | |
| "agent_code": "https://github.com/test/repo", | |
| "answers": [ | |
| {"task_id": "test_task_001", "submitted_answer": "test_answer"} | |
| ] | |
| } | |
| try: | |
| # Note: This might fail with authentication or validation errors, but it tests connectivity | |
| response = requests.post(SUBMIT_URL, json=test_payload, timeout=30) | |
| print(f"π Status Code: {response.status_code}") | |
| print(f"π Headers: {dict(response.headers)}") | |
| try: | |
| data = response.json() | |
| print(f"π Response: {json.dumps(data, indent=2)}") | |
| except: | |
| print(f"π Raw Response: {response.text[:500]}") | |
| return response.status_code, response.text | |
| except requests.exceptions.RequestException as e: | |
| print(f"β Submit Error: {e}") | |
| return None, str(e) | |
| def check_huggingface_space(): | |
| """Check if the HuggingFace Space exists and is accessible""" | |
| print("\nπ Checking HuggingFace Space...") | |
| hf_space_url = "https://huggingface.co/spaces/agents-course/unit4-scoring" | |
| print(f"π HF Space URL: {hf_space_url}") | |
| print("-" * 50) | |
| try: | |
| response = requests.get(hf_space_url, timeout=10) | |
| print(f"π HF Space Status: {response.status_code}") | |
| if response.status_code == 200: | |
| if "Space" in response.text and "Gradio" in response.text: | |
| print("β HuggingFace Space appears to be active") | |
| else: | |
| print("β οΈ HuggingFace Space exists but might not be a Gradio space") | |
| else: | |
| print(f"β HuggingFace Space not accessible: {response.status_code}") | |
| except requests.exceptions.RequestException as e: | |
| print(f"β HF Space Check Error: {e}") | |
| def main(): | |
| """Run all API tests""" | |
| print("π API Testing Suite") | |
| print(f"β° Started at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") | |
| print("=" * 60) | |
| # Test 1: Basic connectivity | |
| connectivity_ok = test_connectivity() | |
| # Test 2: Questions endpoint | |
| questions_ok, questions_data = test_questions_endpoint() | |
| # Test 3: Submit endpoint (will likely fail, but shows connectivity) | |
| submit_status, submit_response = test_submit_endpoint() | |
| # Test 4: Check HuggingFace Space | |
| check_huggingface_space() | |
| # Summary | |
| print("\n" + "=" * 60) | |
| print("π SUMMARY") | |
| print("=" * 60) | |
| print(f"π Base URL: {'β OK' if connectivity_ok else 'β FAILED'}") | |
| print(f"β Questions: {'β OK' if questions_ok else 'β FAILED'}") | |
| if questions_ok and questions_data: | |
| print(f"π Available Questions: {len(questions_data) if isinstance(questions_data, list) else 'Unknown'}") | |
| print(f"π€ Submit Endpoint: {'π Reachable' if submit_status else 'β FAILED'}") | |
| if not any([connectivity_ok, questions_ok]): | |
| print("\nβ API appears to be completely down or unreachable") | |
| print("π‘ Suggestions:") | |
| print(" - Check your internet connection") | |
| print(" - Try again later (server might be temporarily down)") | |
| print(" - Contact course administrators if issue persists") | |
| elif questions_ok: | |
| print("\nβ Questions endpoint is working - you can fetch questions") | |
| print("β Submit endpoint issue might be temporary server problem") | |
| print(f"\nβ° Completed at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") | |
| if __name__ == "__main__": | |
| main() |