code: import sounddevice as sd
  import wavio
  import whisper
  from llama_index.llms import LlamaCPP
  from llama_index.llms.base import ChatMessage
  import streamlit as st
  
  # Function to record audio
  def record_audio(output_filename, duration, sample_rate):
      st.write("Recording...")
      audio_data = sd.rec(int(duration * sample_rate),
                          samplerate=sample_rate, channels=1)
      sd.wait()  # Wait until recording is finished
      st.write("Recording finished.")
  
      # Save the recorded audio to a WAV file
      wavio.write(output_filename, audio_data, sample_rate, sampwidth=2)
  
  # Function to transcribe audio
  def transcribe_audio(audio_file):
      model = whisper.load_model('small')
      text = model.transcribe(audio_file)
      return text['text']
  
  # Function to check grammar and format
  def check_grammar_and_format(text):
      path = r'llama-2-13b.ggmlv3.q4_0.bin'
      llm_gpt = LlamaCPP(model_path=path)
      message = ChatMessage(role='user', content=f'check grammar for the following: {text}')
      response = llm_gpt.chat([message])
      return response.message.content
  
  def main():
      st.title("Speech-to-Text and Grammar Checking")
      st.subheader('How was your day?')
  
      recording_duration = st.sidebar.slider("Recording Duration (seconds)", 1, 60, 10)
      sample_rate = 44100
  
      # Button to start recording
      if st.button("Start Recording"):
          output_file = "recorded_audio.wav"
          record_audio(output_file, recording_duration, sample_rate)
          st.write("Audio saved as:", output_file)
  
          if not sd.query_devices(None, 'input')['default_samplerate'] == sample_rate:
              st.warning("Warning: The sample rate of the input device is not set to", sample_rate)
  
          # Use a spinner for transcription
          with st.spinner("Transcribing..."):
              transcribed_text = transcribe_audio(output_file)
              st.subheader("Transcribed Text:")
              st.write(transcribed_text)
  
          # Use a spinner for grammar check
          with st.spinner("Checking Grammar..."):
              grammar_check_result = check_grammar_and_format(transcribed_text)
              st.subheader("Grammar Check Result:")
              st.write(grammar_check_result)
  
      # Button to upload audio file
      uploaded_file = st.file_uploader("Upload an audio file", type=['wav', 'mp3', 'ogg'])
  
      if uploaded_file is not None:
          with open("uploaded_audio.wav", "wb") as f:
              f.write(uploaded_file.getbuffer())
  
      # Use a spinner for transcription
          with st.spinner("Transcribing uploaded audio..."):
              transcribed_text = transcribe_audio("uploaded_audio.wav")
              st.subheader("Transcribed Text:")
              st.write(transcribed_text)
  
      # Use a spinner for grammar check
          with st.spinner("Checking Grammar..."):
              grammar_check_result = check_grammar_and_format(transcribed_text)
              st.subheader("Grammar Check Result:")
              st.write(grammar_check_result)
  
  
  if __name__ == "__main__":
      main()
  
apis: llama_index.llms.base.ChatMessage llama_index.llms.LlamaCPP
extract_api: [((259, 283), 'streamlit.write', 'st.write', (['"""Recording..."""'], {}), "('Recording...')\n", (267, 283), True, 'import streamlit as st\n'), ((404, 413), 'sounddevice.wait', 'sd.wait', ([], {}), '()\n', (411, 413), True, 'import sounddevice as sd\n'), ((455, 486), 'streamlit.write', 'st.write', (['"""Recording finished."""'], {}), "('Recording finished.')\n", (463, 486), True, 'import streamlit as st\n'), ((539, 605), 'wavio.write', 'wavio.write', (['output_filename', 'audio_data', 'sample_rate'], {'sampwidth': '(2)'}), '(output_filename, audio_data, sample_rate, sampwidth=2)\n', (550, 605), False, 'import wavio\n'), ((688, 715), 'whisper.load_model', 'whisper.load_model', (['"""small"""'], {}), "('small')\n", (706, 715), False, 'import whisper\n'), ((919, 944), 'llama_index.llms.LlamaCPP', 'LlamaCPP', ([], {'model_path': 'path'}), '(model_path=path)\n', (927, 944), False, 'from llama_index.llms import LlamaCPP\n'), ((960, 1036), 'llama_index.llms.base.ChatMessage', 'ChatMessage', ([], {'role': '"""user"""', 'content': 'f"""check grammar for the following: {text}"""'}), "(role='user', content=f'check grammar for the following: {text}')\n", (971, 1036), False, 'from llama_index.llms.base import ChatMessage\n'), ((1134, 1181), 'streamlit.title', 'st.title', (['"""Speech-to-Text and Grammar Checking"""'], {}), "('Speech-to-Text and Grammar Checking')\n", (1142, 1181), True, 'import streamlit as st\n'), ((1187, 1220), 'streamlit.subheader', 'st.subheader', (['"""How was your day?"""'], {}), "('How was your day?')\n", (1199, 1220), True, 'import streamlit as st\n'), ((1249, 1309), 'streamlit.sidebar.slider', 'st.sidebar.slider', (['"""Recording Duration (seconds)"""', '(1)', '(60)', '(10)'], {}), "('Recording Duration (seconds)', 1, 60, 10)\n", (1266, 1309), True, 'import streamlit as st\n'), ((1378, 1406), 'streamlit.button', 'st.button', (['"""Start Recording"""'], {}), "('Start Recording')\n", (1387, 1406), True, 'import streamlit as st\n'), ((2322, 2390), 'streamlit.file_uploader', 'st.file_uploader', (['"""Upload an audio file"""'], {'type': "['wav', 'mp3', 'ogg']"}), "('Upload an audio file', type=['wav', 'mp3', 'ogg'])\n", (2338, 2390), True, 'import streamlit as st\n'), ((1529, 1569), 'streamlit.write', 'st.write', (['"""Audio saved as:"""', 'output_file'], {}), "('Audio saved as:', output_file)\n", (1537, 1569), True, 'import streamlit as st\n'), ((1671, 1760), 'streamlit.warning', 'st.warning', (['"""Warning: The sample rate of the input device is not set to"""', 'sample_rate'], {}), "('Warning: The sample rate of the input device is not set to',\n    sample_rate)\n", (1681, 1760), True, 'import streamlit as st\n'), ((1816, 1845), 'streamlit.spinner', 'st.spinner', (['"""Transcribing..."""'], {}), "('Transcribing...')\n", (1826, 1845), True, 'import streamlit as st\n'), ((1922, 1955), 'streamlit.subheader', 'st.subheader', (['"""Transcribed Text:"""'], {}), "('Transcribed Text:')\n", (1934, 1955), True, 'import streamlit as st\n'), ((1969, 1995), 'streamlit.write', 'st.write', (['transcribed_text'], {}), '(transcribed_text)\n', (1977, 1995), True, 'import streamlit as st\n'), ((2055, 2088), 'streamlit.spinner', 'st.spinner', (['"""Checking Grammar..."""'], {}), "('Checking Grammar...')\n", (2065, 2088), True, 'import streamlit as st\n'), ((2182, 2219), 'streamlit.subheader', 'st.subheader', (['"""Grammar Check Result:"""'], {}), "('Grammar Check Result:')\n", (2194, 2219), True, 'import streamlit as st\n'), ((2233, 2263), 'streamlit.write', 'st.write', (['grammar_check_result'], {}), '(grammar_check_result)\n', (2241, 2263), True, 'import streamlit as st\n'), ((2584, 2628), 'streamlit.spinner', 'st.spinner', (['"""Transcribing uploaded audio..."""'], {}), "('Transcribing uploaded audio...')\n", (2594, 2628), True, 'import streamlit as st\n'), ((2714, 2747), 'streamlit.subheader', 'st.subheader', (['"""Transcribed Text:"""'], {}), "('Transcribed Text:')\n", (2726, 2747), True, 'import streamlit as st\n'), ((2761, 2787), 'streamlit.write', 'st.write', (['transcribed_text'], {}), '(transcribed_text)\n', (2769, 2787), True, 'import streamlit as st\n'), ((2843, 2876), 'streamlit.spinner', 'st.spinner', (['"""Checking Grammar..."""'], {}), "('Checking Grammar...')\n", (2853, 2876), True, 'import streamlit as st\n'), ((2970, 3007), 'streamlit.subheader', 'st.subheader', (['"""Grammar Check Result:"""'], {}), "('Grammar Check Result:')\n", (2982, 3007), True, 'import streamlit as st\n'), ((3021, 3051), 'streamlit.write', 'st.write', (['grammar_check_result'], {}), '(grammar_check_result)\n', (3029, 3051), True, 'import streamlit as st\n'), ((1588, 1619), 'sounddevice.query_devices', 'sd.query_devices', (['None', '"""input"""'], {}), "(None, 'input')\n", (1604, 1619), True, 'import sounddevice as sd\n')]