Spaces:
Sleeping
Sleeping
| import requests | |
| import json | |
| from pprint import pprint | |
| import time | |
| import sys | |
| def evaluate_text_model(space_url: str, max_retries=3, retry_delay=5): | |
| """ | |
| Evaluate a text classification model through its API endpoint | |
| """ | |
| params = { | |
| "dataset_name": "QuotaClimat/frugalaichallenge-text-train", | |
| "test_size": 0.2, | |
| "test_seed": 42, | |
| } | |
| if "localhost" in space_url: | |
| base_url = space_url | |
| else: | |
| base_url = f"https://{space_url.replace('/', '-')}.hf.space" | |
| api_url = f"{base_url}/text" | |
| health_url = f"{base_url}/health" | |
| headers = { | |
| 'Content-Type': 'application/json', | |
| 'Accept': 'application/json' | |
| } | |
| print(f"Base URL: {base_url}") | |
| print(f"API URL: {api_url}") | |
| print(f"Health check URL: {health_url}") | |
| for attempt in range(max_retries): | |
| try: | |
| print(f"\nAttempt {attempt + 1} of {max_retries}") | |
| # Health check | |
| print("Checking space health...") | |
| health_response = requests.get(health_url, timeout=30) | |
| print(f"Health check status: {health_response.status_code}") | |
| if health_response.status_code != 200: | |
| print(f"Space not ready (status: {health_response.status_code})") | |
| if attempt < max_retries - 1: | |
| print(f"Waiting {retry_delay} seconds before retry...") | |
| time.sleep(retry_delay) | |
| continue | |
| # Make API call | |
| print("Making evaluation request...") | |
| response = requests.post( | |
| api_url, | |
| json=params, | |
| headers=headers, | |
| timeout=300 | |
| ) | |
| print(f"Evaluation response status: {response.status_code}") | |
| if response.status_code == 200: | |
| return response.json() | |
| else: | |
| print(f"Error: Status {response.status_code}") | |
| try: | |
| error_detail = response.json() | |
| print(f"Error detail: {error_detail}") | |
| except: | |
| print(f"Response: {response.text}") | |
| if attempt < max_retries - 1: | |
| print(f"Waiting {retry_delay} seconds before retry...") | |
| time.sleep(retry_delay) | |
| except requests.exceptions.RequestException as e: | |
| print(f"Request error: {str(e)}") | |
| if attempt < max_retries - 1: | |
| print(f"Waiting {retry_delay} seconds before retry...") | |
| time.sleep(retry_delay) | |
| except Exception as e: | |
| print(f"Unexpected error: {str(e)}") | |
| if attempt < max_retries - 1: | |
| print(f"Waiting {retry_delay} seconds before retry...") | |
| time.sleep(retry_delay) | |
| return None | |
| def main(): | |
| # Space URL | |
| space_url = "Tonic/frugal-ai-submission-template" | |
| print("\nStarting model evaluation...") | |
| results = evaluate_text_model(space_url) | |
| if results: | |
| print("\nEvaluation Results:") | |
| print("-" * 50) | |
| print(f"Accuracy: {results.get('accuracy', 'N/A'):.4f}") | |
| print(f"Energy (Wh): {results.get('energy_consumed_wh', 'N/A'):.6f}") | |
| print(f"Emissions (gCO2eq): {results.get('emissions_gco2eq', 'N/A'):.6f}") | |
| print("\nFull Results:") | |
| pprint(results) | |
| else: | |
| print("\nEvaluation failed!") | |
| print("Troubleshooting:") | |
| print(f"1. Check space status: https://{space_url.replace('/', '-')}.hf.space") | |
| print("2. Verify API implementation") | |
| print("3. Try again later") | |
| sys.exit(1) | |
| if __name__ == "__main__": | |
| main() |