Spaces:
Sleeping
Sleeping
File size: 3,760 Bytes
acf9798 ab3fccf acf9798 ab3fccf acf9798 ab3fccf acf9798 ab3fccf acf9798 ab3fccf acf9798 ab3fccf acf9798 ab3fccf acf9798 ab3fccf acf9798 e33fed0 ab3fccf acf9798 ab3fccf acf9798 ab3fccf acf9798 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
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() |