File size: 1,922 Bytes
53a770a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)}