File size: 3,294 Bytes
ca042ba
 
 
38822a8
 
ca042ba
 
38822a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca042ba
38822a8
 
 
 
 
 
 
 
 
ca042ba
38822a8
ca042ba
 
 
38822a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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