import os
from assemblyai import (
Transcriber,
Client,
Settings,
TranscriptStatus,
)
from base64 import b64encode
import json
API_KEY = os.getenv("RESPAN_API_KEY")
BASE_URL = (
os.getenv("RESPAN_BASE_URL", "https://api.respan.ai/") + "api/assemblyai/"
)
class RespanAssemblyAIClient(Client):
"""
Define Respan's AssemblyAI client wrapper for passing extra headers containing respan params
"""
def __init__(self, respan_api_key: str, headers: dict):
settings = Settings(api_key=respan_api_key, base_url=BASE_URL)
print(settings)
super().__init__(settings=settings)
self._http_client.headers.update(headers)
respan_params = {
"metadata": {
"paid_user": "true",
}
# other respan parameters...
}
respan_headers = {
"X-Assemblyai-Api-Key": os.getenv("ASSEMBLYAI_API_KEY"), # <-- This is the AssemblyAI API key
"X-Data-Respan-Params": b64encode(
json.dumps(respan_params).encode()
).decode(),
}
client = RespanAssemblyAIClient(
respan_api_key=API_KEY, # <-- This is the API key for authenticating against Respan, not the AssemblyAI API key (defined above)
headers=respan_headers
)
transcriber = Transcriber(client=client)
transcript = transcriber.transcribe("https://assembly.ai/wildfires.mp3")
if transcript.status == TranscriptStatus.error:
print(transcript.error)
else:
print(transcript.text)