import pyaudio import wave from groq import Groq # Initialize the Groq client groq_client = Groq(api_key="your_groq_api_key") def record_audio(output_file, duration=5): """Record audio from the microphone and save it to a file.""" chunk = 1024 # Record in chunks of 1024 samples sample_format = pyaudio.paInt16 # 16 bits per sample channels = 1 fs = 44100 # Record at 44100 samples per second p = pyaudio.PyAudio() # Create an interface to PortAudio print('Recording') stream = p.open(format=sample_format, channels=channels, rate=fs, frames_per_buffer=chunk, input=True) frames = [] # Initialize array to store frames # Store data in chunks for the specified duration for _ in range(0, int(fs / chunk * duration)): data = stream.read(chunk) frames.append(data) # Stop and close the stream stream.stop_stream() stream.close() # Terminate the PortAudio interface p.terminate() print('Finished recording') # Save the recorded data as a WAV file wf = wave.open(output_file, 'wb') wf.setnchannels(channels) wf.setsampwidth(p.get_sample_size(sample_format)) wf.setframerate(fs) wf.writeframes(b''.join(frames)) wf.close() def transcribe_audio(file_path): """Transcribe audio file using Groq's Whisper Large V3 Turbo model.""" with open(file_path, "rb") as audio_file: transcription = groq_client.audio.transcriptions.create( file=(file_path, audio_file.read()), model="whisper-large-v3-turbo", response_format="json", language="en" # Specify the language if needed ) return transcription.text def generate_response(transcription_text): """Generate a response based on the transcribed text.""" # Here you can use any language model or logic to generate a response # For simplicity, let's just echo the transcription return f"Echo: {transcription_text}" def main(): audio_file = "output.wav" record_audio(audio_file) transcription = transcribe_audio(audio_file) response = generate_response(transcription) print(response) if __name__ == "__main__": main()