|
|
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=""):
|
|
|
|
|
|
self.groq_api_key = os.getenv("GROQ_API_KEY")
|
|
|
if not self.groq_api_key:
|
|
|
raise ValueError("GROQ_API_KEY environment variable is required")
|
|
|
|
|
|
|
|
|
self.client = Groq(api_key=self.groq_api_key)
|
|
|
|
|
|
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
try:
|
|
|
|
|
|
if "inputs" in data:
|
|
|
audio_data = data["inputs"]
|
|
|
else:
|
|
|
audio_data = data
|
|
|
|
|
|
|
|
|
if isinstance(audio_data, str):
|
|
|
audio_bytes = base64.b64decode(audio_data)
|
|
|
else:
|
|
|
audio_bytes = audio_data
|
|
|
|
|
|
|
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as temp_file:
|
|
|
temp_file.write(audio_bytes)
|
|
|
temp_file.flush()
|
|
|
|
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
|
os.unlink(temp_file.name)
|
|
|
|
|
|
return {"text": transcription}
|
|
|
|
|
|
except Exception as e:
|
|
|
return {"error": str(e)} |