77 lines
2.3 KiB
Python
77 lines
2.3 KiB
Python
import os
|
|
from groq import Groq
|
|
import pyaudio
|
|
import wave
|
|
import pyautogui
|
|
import keyboard
|
|
import mouse
|
|
|
|
# Constants
|
|
API_KEY = "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" # Make sure to use your actual API key
|
|
BUTTON = 'ctrl' # The keyboard button to listen for
|
|
|
|
# Initialize the Groq client
|
|
client = Groq(api_key=API_KEY)
|
|
|
|
def record_audio(filename):
|
|
# Setup audio recording
|
|
audio = pyaudio.PyAudio()
|
|
stream = audio.open(format=pyaudio.paInt16, channels=1, rate=44100, input=True, frames_per_buffer=1024)
|
|
|
|
frames = []
|
|
print("Recording...")
|
|
|
|
# Record while button or mouse is pressed
|
|
while keyboard.is_pressed(BUTTON) or mouse.is_pressed(button='left'):
|
|
data = stream.read(1024)
|
|
frames.append(data)
|
|
|
|
print("Recording stopped.")
|
|
stream.stop_stream()
|
|
stream.close()
|
|
audio.terminate()
|
|
|
|
# Save the recorded audio
|
|
wave_file = wave.open(filename, 'wb')
|
|
wave_file.setnchannels(1)
|
|
wave_file.setsampwidth(audio.get_sample_size(pyaudio.paInt16))
|
|
wave_file.setframerate(44100)
|
|
wave_file.writeframes(b''.join(frames))
|
|
wave_file.close()
|
|
|
|
def transcribe_audio(filename):
|
|
# Open the audio file
|
|
with open(filename, "rb") as file:
|
|
# Create a transcription of the audio file
|
|
transcription = client.audio.transcriptions.create(
|
|
file=(filename, file.read()), # Required audio file
|
|
model="distil-whisper-large-v3-en", # Required model to use for transcription
|
|
prompt="Specify context or spelling", # Optional
|
|
response_format="json", # Optional
|
|
temperature=0.0 # Optional
|
|
)
|
|
return transcription.text
|
|
|
|
def simulate_keypress(text):
|
|
# Simulate keypress for each character in text
|
|
for char in text:
|
|
pyautogui.typewrite(char)
|
|
pyautogui.press('enter')
|
|
|
|
def main():
|
|
filename = "output.wav"
|
|
|
|
print("Press and hold the button or left mouse button to record...")
|
|
# Wait for button or mouse press
|
|
while not (keyboard.is_pressed(BUTTON) or mouse.is_pressed(button='left')):
|
|
pass
|
|
record_audio(filename)
|
|
|
|
print("Transcribing audio...")
|
|
transcribed_text = transcribe_audio(filename)
|
|
|
|
print("Entering text...")
|
|
simulate_keypress(transcribed_text)
|
|
|
|
if __name__ == "__main__":
|
|
main() |