itsbava commited on
Commit
53a770a
·
verified ·
1 Parent(s): e1b5fca

Upload 2 files

Browse files
Files changed (2) hide show
  1. handler.py +53 -0
  2. requirements.txt +1 -0
handler.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import base64
3
+ import tempfile
4
+ from pathlib import Path
5
+ from typing import Dict, Any
6
+ from groq import Groq
7
+
8
+ class EndpointHandler:
9
+ def __init__(self, path=""):
10
+ # Get Groq API key from HF endpoint environment variables
11
+ self.groq_api_key = os.getenv("GROQ_API_KEY")
12
+ if not self.groq_api_key:
13
+ raise ValueError("GROQ_API_KEY environment variable is required")
14
+
15
+ # Initialize Groq client
16
+ self.client = Groq(api_key=self.groq_api_key)
17
+
18
+ def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
19
+ try:
20
+ # Get audio data from request
21
+ if "inputs" in data:
22
+ audio_data = data["inputs"]
23
+ else:
24
+ audio_data = data
25
+
26
+ # Handle base64 encoded audio
27
+ if isinstance(audio_data, str):
28
+ audio_bytes = base64.b64decode(audio_data)
29
+ else:
30
+ audio_bytes = audio_data
31
+
32
+ # Save to temp file
33
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as temp_file:
34
+ temp_file.write(audio_bytes)
35
+ temp_file.flush()
36
+
37
+ # Forward to Groq API
38
+ with open(temp_file.name, "rb") as file:
39
+ transcription = self.client.audio.transcriptions.create(
40
+ file=(Path(temp_file.name).name, file.read()),
41
+ model="whisper-large-v3-turbo",
42
+ language="ta",
43
+ response_format="text",
44
+ temperature=0.0
45
+ )
46
+
47
+ # Clean up
48
+ os.unlink(temp_file.name)
49
+
50
+ return {"text": transcription}
51
+
52
+ except Exception as e:
53
+ return {"error": str(e)}
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ groq>=0.4.0