Spaces:
Sleeping
Sleeping
Commit
·
ca042ba
1
Parent(s):
2854ab0
Added auto_test_endpoints.py to verify all endpoints in swagger
Browse files- auto_test_endpoints.py +83 -0
- test_combined_api.py +19 -0
auto_test_endpoints.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
BASE_URL = "https://osnarayana-media-gen-api.hf.space"
|
| 6 |
+
TOKEN = "my_secure_token_123"
|
| 7 |
+
|
| 8 |
+
headers = {"Authorization": f"Bearer {TOKEN}"}
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def fetch_openapi_schema():
|
| 12 |
+
"""Fetch Swagger/OpenAPI schema from FastAPI"""
|
| 13 |
+
url = f"{BASE_URL}/openapi.json"
|
| 14 |
+
resp = requests.get(url)
|
| 15 |
+
resp.raise_for_status()
|
| 16 |
+
return resp.json()
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def guess_payload(path):
|
| 20 |
+
"""Generate dummy payloads based on endpoint path"""
|
| 21 |
+
path_lower = path.lower()
|
| 22 |
+
if "image" in path:
|
| 23 |
+
return {"prompt": "A cute cat in watercolor style"}
|
| 24 |
+
if "audio" in path:
|
| 25 |
+
return {"text": "Hello from Hugging Face Space!"}
|
| 26 |
+
if "video" in path:
|
| 27 |
+
return {"prompt": "A futuristic city with flying cars"}
|
| 28 |
+
if "metrics" in path and "bleu" in path:
|
| 29 |
+
return {"reference": "hello world", "candidate": "hello"}
|
| 30 |
+
if "metrics" in path and "clipscore" in path:
|
| 31 |
+
return {"reference": "a photo of a cat", "candidate": "an image of a cute cat"}
|
| 32 |
+
# fallback
|
| 33 |
+
return {}
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def save_response_content(resp, path):
|
| 37 |
+
"""Save binary content for media endpoints"""
|
| 38 |
+
content_type = resp.headers.get("content-type", "")
|
| 39 |
+
filename = None
|
| 40 |
+
if "image" in content_type:
|
| 41 |
+
filename = "output_image.png"
|
| 42 |
+
elif "audio" in content_type:
|
| 43 |
+
filename = "output_audio.mp3"
|
| 44 |
+
elif "video" in content_type:
|
| 45 |
+
filename = "output_video.mp4"
|
| 46 |
+
|
| 47 |
+
if filename:
|
| 48 |
+
with open(filename, "wb") as f:
|
| 49 |
+
f.write(resp.content)
|
| 50 |
+
print(f"✅ Saved file: {filename}")
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def test_all_endpoints():
|
| 54 |
+
schema = fetch_openapi_schema()
|
| 55 |
+
paths = schema.get("paths", {})
|
| 56 |
+
|
| 57 |
+
for path, methods in paths.items():
|
| 58 |
+
for method, details in methods.items():
|
| 59 |
+
# Only test POST endpoints for now
|
| 60 |
+
if method.lower() != "post":
|
| 61 |
+
continue
|
| 62 |
+
|
| 63 |
+
url = BASE_URL + path
|
| 64 |
+
payload = guess_payload(path)
|
| 65 |
+
|
| 66 |
+
print(f"\nTesting {method.upper()} {url} with payload: {payload}")
|
| 67 |
+
# Decide if we need JSON or query params (FastAPI default body)
|
| 68 |
+
resp = requests.post(url, headers=headers, json=payload)
|
| 69 |
+
|
| 70 |
+
print(f"Status: {resp.status_code}")
|
| 71 |
+
if resp.status_code == 200:
|
| 72 |
+
# Try to save file if it's media
|
| 73 |
+
save_response_content(resp, path)
|
| 74 |
+
try:
|
| 75 |
+
print("Response JSON:", resp.json())
|
| 76 |
+
except Exception:
|
| 77 |
+
print("Non-JSON response saved as file")
|
| 78 |
+
else:
|
| 79 |
+
print("Error Response:", resp.text)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
test_all_endpoints()
|
test_combined_api.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
|
| 3 |
+
BASE_URL = "https://osnarayana-media-gen-api.hf.space/api/v1/metrics"
|
| 4 |
+
TOKEN = "my_secure_token_123" # must match app/auth/auth.py
|
| 5 |
+
|
| 6 |
+
headers = {
|
| 7 |
+
"Authorization": f"Bearer {TOKEN}",
|
| 8 |
+
"Content-Type": "application/json"
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
# 1️⃣ BLEU
|
| 12 |
+
bleu_payload = {"reference": "hello world", "candidate": "hello"}
|
| 13 |
+
r1 = requests.post(f"{BASE_URL}/evaluate/bleu", headers=headers, params={"reference": "hello world", "candidate": "hello"})
|
| 14 |
+
print("BLEU Response:", r1.status_code, r1.text)
|
| 15 |
+
|
| 16 |
+
# 2️⃣ CLIPScore
|
| 17 |
+
clip_payload = {"reference": "a photo of a cat", "candidate": "an image of a cute cat"}
|
| 18 |
+
r2 = requests.post(f"{BASE_URL}/evaluate/clipscore", headers=headers, params={"reference": "a photo of a cat", "candidate": "an image of a cute cat"})
|
| 19 |
+
print("CLIPScore Response:", r2.status_code, r2.text)
|