code: import sounddevice as sd
  import wavio
  import whisper
  import openai
  from llama_index.llms import LlamaCPP
  from llama_index.llms.base import ChatMessage
  
  
  def record_audio(output_filename, duration, sample_rate):
      print("Recording...")
      audio_data = sd.rec(int(duration * sample_rate),
                          samplerate=sample_rate, channels=1)
      sd.wait()  # Wait until recording is finished
      print("Recording finished.")
  
      # Save the recorded audio to a WAV file
      wavio.write(output_filename, audio_data, sample_rate, sampwidth=2)
  
  
  def transcribe_audio(audio_file):
      model = whisper.load_model('base')
      text = model.transcribe(audio_file)
      return text['text']
  
  
  def check_grammar_and_format(text):
      path = r'C:\Users\vikra\llama.cpp\llama-2-13b-chat.ggmlv3.q4_0.bin'
      llm_gpt = LlamaCPP(model_path=path)
      message = ChatMessage(role='user', content=f'check grammar and the correct format for the following: {text}')
      return llm_gpt.chat([message])
  
  
  def main():
      print("Speech-to-Text and Grammar Checking")
  
      recording_duration = 5
      output_file = "recorded_audio.wav"
      sample_rate = 44100
  
      record_audio(output_file, recording_duration, sample_rate)
      print("Audio saved as:", output_file)
  
      if not sd.query_devices(None, 'input')['default_samplerate'] == sample_rate:
          print("Warning: The sample rate of the input device is not set to", sample_rate)
  
      transcribed_text = transcribe_audio(output_file)
      print("Transcribed Text:", transcribed_text)
  
      grammar_check_result = check_grammar_and_format(transcribed_text)
      print("Grammar Check Result:", grammar_check_result)
  
  
  if __name__ == "__main__":
      main()
  
apis: llama_index.llms.base.ChatMessage llama_index.llms.LlamaCPP
extract_api: [((366, 375), 'sounddevice.wait', 'sd.wait', ([], {}), '()\n', (373, 375), True, 'import sounddevice as sd\n'), ((498, 564), 'wavio.write', 'wavio.write', (['output_filename', 'audio_data', 'sample_rate'], {'sampwidth': '(2)'}), '(output_filename, audio_data, sample_rate, sampwidth=2)\n', (509, 564), False, 'import wavio\n'), ((617, 643), 'whisper.load_model', 'whisper.load_model', (['"""base"""'], {}), "('base')\n", (635, 643), False, 'import whisper\n'), ((839, 864), 'llama_index.llms.LlamaCPP', 'LlamaCPP', ([], {'model_path': 'path'}), '(model_path=path)\n', (847, 864), False, 'from llama_index.llms import LlamaCPP\n'), ((880, 984), 'llama_index.llms.base.ChatMessage', 'ChatMessage', ([], {'role': '"""user"""', 'content': 'f"""check grammar and the correct format for the following: {text}"""'}), "(role='user', content=\n    f'check grammar and the correct format for the following: {text}')\n", (891, 984), False, 'from llama_index.llms.base import ChatMessage\n'), ((1301, 1332), 'sounddevice.query_devices', 'sd.query_devices', (['None', '"""input"""'], {}), "(None, 'input')\n", (1317, 1332), True, 'import sounddevice as sd\n')]