cap2 / test_client.py
ADXabhi's picture
Upload 7 files
c516bed verified
"""
Test Client for Caption Renderer API
Use this to test the HF Space endpoint programmatically
"""
import requests
import json
# Your HF Space URL (update after deployment)
HF_SPACE_URL = "https://YOUR-USERNAME-caption-renderer-v4.hf.space"
# Sample Hindi transcript (as provided by user)
SAMPLE_TRANSCRIPT = [
{"end": 0.44, "text": "राधे", "start": 0.3},
{"end": 0.62, "text": "राधे", "start": 0.48},
{"end": 0.9, "text": "महाराज", "start": 0.68},
{"end": 1, "text": "जी,", "start": 0.94},
{"end": 1.7, "text": "महाराज", "start": 1.52},
{"end": 1.9, "text": "इस", "start": 1.82},
{"end": 2.36, "text": "कलयुग", "start": 2.04},
{"end": 2.56, "text": "में", "start": 2.46},
{"end": 3.18, "text": "विद्यार्थी", "start": 2.72},
{"end": 3.5, "text": "जीवन", "start": 3.3},
{"end": 3.66, "text": "में", "start": 3.6},
{"end": 4.22, "text": "ब्रह्मचर्य", "start": 3.78},
{"end": 4.36, "text": "का", "start": 4.3}
]
def test_local():
"""Test the video generation locally without API"""
from canvas_renderer import render_frame
from video_encoder import encode_frames_pipe
# Test with sample transcript
words = [item['text'] for item in SAMPLE_TRANSCRIPT[:4]]
print(f"Testing with words: {words}")
# Generate a few test frames
frames = []
for i in range(48): # 2 seconds at 24fps
active_idx = (i // 12) % len(words)
frame = render_frame(words, active_idx, "hormozi")
frames.append(frame)
# Save first frame for visual inspection
frames[0].save("test_frame_0.png")
frames[12].save("test_frame_12.png")
print("Saved test frames: test_frame_0.png, test_frame_12.png")
# Try encoding
try:
encode_frames_pipe(frames, "test_local.webm", fps=24)
print("Successfully created test_local.webm")
except Exception as e:
print(f"Encoding failed (expected if FFmpeg not installed): {e}")
def test_gradio_api(space_url: str = HF_SPACE_URL):
"""Test the deployed Gradio API"""
from gradio_client import Client
client = Client(space_url)
result = client.predict(
transcript_json=json.dumps(SAMPLE_TRANSCRIPT),
style="hormozi",
api_name="/generate_video"
)
print(f"Video path: {result[0]}")
print(f"Cloudinary URL: {result[1]}")
if __name__ == "__main__":
print("=== Testing Local Rendering ===")
test_local()