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, } # Construct API URL if "localhost" in space_url: api_url = f"{space_url}/text" else: api_url = f"https://{space_url.replace('/', '-')}.hf.space/text" headers = { 'Content-Type': 'application/json', 'Accept': 'application/json' } for attempt in range(max_retries): try: print(f"\nAttempt {attempt + 1} of {max_retries}") print(f"Making request to: {api_url}") # Health check health_url = f"https://{space_url.replace('/', '-')}.hf.space/health" health_response = requests.get(health_url, timeout=30) if health_response.status_code != 200: print(f"Space not ready (status: {health_response.status_code})") time.sleep(retry_delay) continue # Make API call response = requests.post( api_url, json=params, headers=headers, timeout=300 ) if response.status_code == 200: return response.json() else: print(f"Error: Status {response.status_code}") print(f"Response: {response.text}") time.sleep(retry_delay) except requests.exceptions.RequestException as e: print(f"Request error: {str(e)}") if attempt < max_retries - 1: time.sleep(retry_delay) except Exception as e: print(f"Unexpected error: {str(e)}") if attempt < max_retries - 1: 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()