-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstt.py
37 lines (34 loc) · 1.21 KB
/
stt.py
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
from deepgram import Deepgram
import asyncio, json
import os
from dotenv import load_dotenv
load_dotenv()
class Deepgram_STT():
def __init__(self):
DEEPGRAM_API_KEY = os.getenv("DEEPGRAM_API")
self.dg = Deepgram(DEEPGRAM_API_KEY)
async def transcribe_from_file(self, temp_file):
try:
response = None
# Set the source
with open(temp_file, 'rb') as audio:
audio_buffer = audio.read()
source = {
'buffer': audio_buffer,
'mimetype': 'audio/webm'
}
print(">>>> transcribing")
response = await asyncio.create_task(
self.dg.transcription.prerecorded(
source,
{
'punctuate': True,
'tier': 'base',
'model': 'voicemail'
}
)
)
# print(json.dumps(response, indent=4))
return response['results']['channels'][0]['alternatives'][0]['transcript']
except Exception as e:
raise Exception(f'Could not process audio: {e}')