import os import base64 import tempfile from pathlib import Path from typing import Dict, Any from groq import Groq class EndpointHandler: def __init__(self, path=""): # Get Groq API key from HF endpoint environment variables self.groq_api_key = os.getenv("GROQ_API_KEY") if not self.groq_api_key: raise ValueError("GROQ_API_KEY environment variable is required") # Initialize Groq client self.client = Groq(api_key=self.groq_api_key) def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]: try: # Get audio data from request if "inputs" in data: audio_data = data["inputs"] else: audio_data = data # Handle base64 encoded audio if isinstance(audio_data, str): audio_bytes = base64.b64decode(audio_data) else: audio_bytes = audio_data # Save to temp file with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as temp_file: temp_file.write(audio_bytes) temp_file.flush() # Forward to Groq API with open(temp_file.name, "rb") as file: transcription = self.client.audio.transcriptions.create( file=(Path(temp_file.name).name, file.read()), model="whisper-large-v3-turbo", language="ta", response_format="text", temperature=0.0 ) # Clean up os.unlink(temp_file.name) return {"text": transcription} except Exception as e: return {"error": str(e)}