Spaces:
Sleeping
Sleeping
improve submission
Browse files- app.py +13 -6
- submission_script.py +23 -7
app.py
CHANGED
@@ -9,19 +9,26 @@ app = FastAPI(
|
|
9 |
title="Frugal AI Challenge API",
|
10 |
description="API for the Frugal AI Challenge evaluation endpoints"
|
11 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
|
|
13 |
# Include all routers
|
14 |
-
app.include_router(text.router)
|
15 |
-
app.include_router(image.router)
|
16 |
-
app.include_router(audio.router)
|
17 |
|
18 |
@app.get("/")
|
19 |
async def root():
|
20 |
return {
|
21 |
"message": "Welcome to the Frugal AI Challenge API",
|
22 |
"endpoints": {
|
23 |
-
"text": "/text - Text classification task"
|
24 |
-
"image": "/image - Image classification task (coming soon)",
|
25 |
-
"audio": "/audio - Audio classification task (coming soon)"
|
26 |
}
|
27 |
}
|
|
|
9 |
title="Frugal AI Challenge API",
|
10 |
description="API for the Frugal AI Challenge evaluation endpoints"
|
11 |
)
|
12 |
+
from fastapi import FastAPI
|
13 |
+
from tasks.text import router as text_router
|
14 |
+
|
15 |
+
@app.get("/health")
|
16 |
+
async def health_check():
|
17 |
+
return {"status": "ok"}
|
18 |
|
19 |
+
app.include_router(text_router)
|
20 |
# Include all routers
|
21 |
+
# app.include_router(text.router)
|
22 |
+
# app.include_router(image.router)
|
23 |
+
# app.include_router(audio.router)
|
24 |
|
25 |
@app.get("/")
|
26 |
async def root():
|
27 |
return {
|
28 |
"message": "Welcome to the Frugal AI Challenge API",
|
29 |
"endpoints": {
|
30 |
+
"text": "/text - Text classification task"#,
|
31 |
+
# "image": "/image - Image classification task (coming soon)",
|
32 |
+
# "audio": "/audio - Audio classification task (coming soon)"
|
33 |
}
|
34 |
}
|
submission_script.py
CHANGED
@@ -14,32 +14,42 @@ def evaluate_text_model(space_url: str, max_retries=3, retry_delay=5):
|
|
14 |
"test_seed": 42,
|
15 |
}
|
16 |
|
17 |
-
# Construct API
|
18 |
if "localhost" in space_url:
|
19 |
-
|
20 |
else:
|
21 |
-
|
|
|
|
|
|
|
22 |
|
23 |
headers = {
|
24 |
'Content-Type': 'application/json',
|
25 |
'Accept': 'application/json'
|
26 |
}
|
27 |
|
|
|
|
|
|
|
|
|
28 |
for attempt in range(max_retries):
|
29 |
try:
|
30 |
print(f"\nAttempt {attempt + 1} of {max_retries}")
|
31 |
-
print(f"Making request to: {api_url}")
|
32 |
|
33 |
# Health check
|
34 |
-
|
35 |
health_response = requests.get(health_url, timeout=30)
|
|
|
36 |
|
37 |
if health_response.status_code != 200:
|
38 |
print(f"Space not ready (status: {health_response.status_code})")
|
39 |
-
|
|
|
|
|
40 |
continue
|
41 |
|
42 |
# Make API call
|
|
|
43 |
response = requests.post(
|
44 |
api_url,
|
45 |
json=params,
|
@@ -47,20 +57,26 @@ def evaluate_text_model(space_url: str, max_retries=3, retry_delay=5):
|
|
47 |
timeout=300
|
48 |
)
|
49 |
|
|
|
|
|
50 |
if response.status_code == 200:
|
51 |
return response.json()
|
52 |
else:
|
53 |
print(f"Error: Status {response.status_code}")
|
54 |
print(f"Response: {response.text}")
|
55 |
-
|
|
|
|
|
56 |
|
57 |
except requests.exceptions.RequestException as e:
|
58 |
print(f"Request error: {str(e)}")
|
59 |
if attempt < max_retries - 1:
|
|
|
60 |
time.sleep(retry_delay)
|
61 |
except Exception as e:
|
62 |
print(f"Unexpected error: {str(e)}")
|
63 |
if attempt < max_retries - 1:
|
|
|
64 |
time.sleep(retry_delay)
|
65 |
|
66 |
return None
|
|
|
14 |
"test_seed": 42,
|
15 |
}
|
16 |
|
17 |
+
# Construct base URL and API endpoints
|
18 |
if "localhost" in space_url:
|
19 |
+
base_url = space_url
|
20 |
else:
|
21 |
+
base_url = f"https://{space_url.replace('/', '-')}.hf.space"
|
22 |
+
|
23 |
+
api_url = f"{base_url}/text"
|
24 |
+
health_url = f"{base_url}/health"
|
25 |
|
26 |
headers = {
|
27 |
'Content-Type': 'application/json',
|
28 |
'Accept': 'application/json'
|
29 |
}
|
30 |
|
31 |
+
print(f"Base URL: {base_url}")
|
32 |
+
print(f"API URL: {api_url}")
|
33 |
+
print(f"Health check URL: {health_url}")
|
34 |
+
|
35 |
for attempt in range(max_retries):
|
36 |
try:
|
37 |
print(f"\nAttempt {attempt + 1} of {max_retries}")
|
|
|
38 |
|
39 |
# Health check
|
40 |
+
print("Checking space health...")
|
41 |
health_response = requests.get(health_url, timeout=30)
|
42 |
+
print(f"Health check status: {health_response.status_code}")
|
43 |
|
44 |
if health_response.status_code != 200:
|
45 |
print(f"Space not ready (status: {health_response.status_code})")
|
46 |
+
if attempt < max_retries - 1:
|
47 |
+
print(f"Waiting {retry_delay} seconds before retry...")
|
48 |
+
time.sleep(retry_delay)
|
49 |
continue
|
50 |
|
51 |
# Make API call
|
52 |
+
print("Making evaluation request...")
|
53 |
response = requests.post(
|
54 |
api_url,
|
55 |
json=params,
|
|
|
57 |
timeout=300
|
58 |
)
|
59 |
|
60 |
+
print(f"Evaluation response status: {response.status_code}")
|
61 |
+
|
62 |
if response.status_code == 200:
|
63 |
return response.json()
|
64 |
else:
|
65 |
print(f"Error: Status {response.status_code}")
|
66 |
print(f"Response: {response.text}")
|
67 |
+
if attempt < max_retries - 1:
|
68 |
+
print(f"Waiting {retry_delay} seconds before retry...")
|
69 |
+
time.sleep(retry_delay)
|
70 |
|
71 |
except requests.exceptions.RequestException as e:
|
72 |
print(f"Request error: {str(e)}")
|
73 |
if attempt < max_retries - 1:
|
74 |
+
print(f"Waiting {retry_delay} seconds before retry...")
|
75 |
time.sleep(retry_delay)
|
76 |
except Exception as e:
|
77 |
print(f"Unexpected error: {str(e)}")
|
78 |
if attempt < max_retries - 1:
|
79 |
+
print(f"Waiting {retry_delay} seconds before retry...")
|
80 |
time.sleep(retry_delay)
|
81 |
|
82 |
return None
|