media-gen-api / auto_test_endpoints.py
osnarayana's picture
Fix: use /tmp for audio generation to avoid permission errors
38822a8
import requests
import json
import os
import time
from datetime import datetime
BASE_URL = "https://osnarayana-media-gen-api.hf.space"
TOKEN = "my_secure_token_123" # must match your auth.py
headers = {
"Authorization": f"Bearer {TOKEN}",
"Content-Type": "application/json"
}
# Directory for saving media files
os.makedirs("outputs", exist_ok=True)
def unique_filename(prefix, ext):
ts = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
return os.path.join("outputs", f"{prefix}_{ts}.{ext}")
# Dummy payload generator
def get_dummy_payload(path):
if "audio/generate" in path:
return {"text": "Hello from auto test!"}
elif "video/generate" in path:
return {
"prompt": "A futuristic city with flying cars",
"tone": "inspiring",
"domain": "sci-fi",
"environment": "day"
}
elif "image/generate" in path:
return {"prompt": "A cute cat in watercolor style"}
elif "ppt/generate" in path:
return {
"slides": [
{"title": "Welcome", "content": "This is auto-generated PPT"},
{"title": "Conclusion", "content": "Demo slide"}
]
}
elif "metrics/evaluate/bleu" in path:
# metrics currently require query params
return {"reference": "hello world", "candidate": "hello"}
elif "metrics/evaluate/clipscore" in path:
return {"reference": "a photo of a cat", "candidate": "an image of a cute cat"}
return {}
# Detect if endpoint expects query params
def use_query_params(path):
return "metrics/evaluate" in path # currently only metrics use query
# List of endpoints to test
ENDPOINTS = [
"/api/v1/audio/generate",
"/api/v1/video/generate",
"/api/v1/image/generate",
"/api/v1/ppt/generate",
"/api/v1/metrics/evaluate/bleu",
"/api/v1/metrics/evaluate/clipscore"
]
for endpoint in ENDPOINTS:
url = f"{BASE_URL}{endpoint}"
payload = get_dummy_payload(endpoint)
print(f"\n=== Testing POST {url} ===")
print("Payload:", payload)
# Decide whether to use params or json
if use_query_params(endpoint):
response = requests.post(url, headers=headers, params=payload)
else:
response = requests.post(url, headers=headers, json=payload)
status = response.status_code
print("Status:", status)
if status == 200:
# Handle media files
content_type = response.headers.get("content-type", "")
if "application/json" in content_type:
print("JSON Response:", response.json())
else:
# Save binary file
ext = "bin"
if "image" in content_type:
ext = "png"
elif "audio" in content_type:
ext = "mp3"
elif "video" in content_type:
ext = "mp4"
elif "presentation" in content_type or "ppt" in endpoint:
ext = "pptx"
filename = unique_filename(endpoint.strip("/").replace("/", "_"), ext)
with open(filename, "wb") as f:
f.write(response.content)
print(f"βœ… Saved media file: {filename}")
else:
print("Error Response:", response.text)
time.sleep(1) # small delay to avoid spamming