stt wip
evrardt committed
Aug 7, 2024 at 22:12 UTC
f5e790a35b65477237080ef73932a4f10f0bf444
2 files changed
+66
-1
main.py
+1
-1
@@ -1,4 +1,4 @@
1
-import threading, time, models, os, sys, argparse, asyncio
1
+import threading, time, models, stt, os, sys, argparse, asyncio
2
from ansio import application_keypad, mouse_input, raw_input
3
from ansio.input import InputEvent, get_input_event
4
from agent import Agent, AgentConfig
stt.py
new
+65
@@ -0,0 +1,65 @@
1
+import os
2
+from dotenv import load_dotenv
3
+from groq import Groq
4
+import pyaudio
5
+import wave
6
+
7
+load_dotenv() # take environment variables from .env.
8
+
9
+# Utility function to get API keys from environment variables
10
+def get_api_key(service):
11
+ return os.getenv(f"API_KEY_{service.upper()}") or os.getenv(f"{service.upper()}_API_KEY")
12
+
13
+def record():
14
+ client = Groq(
15
+ api_key=get_api_key("groq")
16
+ )
17
+
18
+ CHUNK = 1024
19
+ FORMAT = pyaudio.paInt16
20
+ CHANNELS = 2
21
+ RATE = 44100
22
+ RECORD_SECONDS = 10
23
+ WAVE_OUTPUT_FILENAME = "output.wav"
24
+
25
+ p = pyaudio.PyAudio()
26
+
27
+ stream = p.open(format=FORMAT,
28
+ channels=CHANNELS,
29
+ rate=RATE,
30
+ input=True,
31
+ frames_per_buffer=CHUNK)
32
+
33
+ print("* recording")
34
+
35
+ frames = []
36
+
37
+ for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
38
+ data = stream.read(CHUNK)
39
+ frames.append(data)
40
+
41
+ print("* done recording")
42
+
43
+ stream.stop_stream()
44
+ stream.close()
45
+ p.terminate()
46
+
47
+ wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
48
+ wf.setnchannels(CHANNELS)
49
+ wf.setsampwidth(p.get_sample_size(FORMAT))
50
+ wf.setframerate(RATE)
51
+ wf.writeframes(b''.join(frames))
52
+ wf.close()
53
+
54
+ filename = os.path.dirname(__file__) + "" + WAVE_OUTPUT_FILENAME
55
+
56
+ with open(filename, "rb") as file:
57
+ transcription = client.audio.transcriptions.create(
58
+ file=(filename, file.read()),
59
+ model="whisper-large-v3",
60
+ prompt="Human talking to personnal ia agent", # Optional
61
+ response_format="json", # Optional
62
+ language="en", # Optional
63
+ temperature=0.0 # Optional
64
+ )
65
+ print(transcription.text)
\ No newline at end of file