barghavani commited on
Commit
5353c1d
·
verified ·
1 Parent(s): ca5c18b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -16
app.py CHANGED
@@ -10,6 +10,8 @@ from langchain.chains.question_answering import load_qa_chain
10
  from langchain.prompts import PromptTemplate
11
  from dotenv import load_dotenv
12
  import speech_recognition as sr
 
 
13
 
14
  load_dotenv()
15
  os.getenv("GOOGLE_API_KEY")
@@ -79,21 +81,18 @@ def user_input(user_question):
79
  st.write("Reply: ", response["output_text"])
80
 
81
 
 
 
 
 
82
  def record_audio():
83
- r = sr.Recognizer()
84
- with sr.Microphone() as source:
85
- st.write("Please speak your question...")
86
- audio = r.listen(source)
87
- try:
88
- text = r.recognize_google(audio)
89
- st.write("You said: " + text)
90
- return text
91
- except sr.UnknownValueError:
92
- st.error("Could not understand audio")
93
- return None
94
- except sr.RequestError as e:
95
- st.error(f"Could not request results; {e}")
96
- return None
97
 
98
  def main():
99
  st.set_page_config("Chat PDF")
@@ -109,10 +108,11 @@ def main():
109
  get_vector_store(text_chunks)
110
  st.success("Done")
111
 
112
- # User can choose to input question via text or voice
113
  user_question = st.text_input("Ask a Question from the PDF Files")
114
  if st.button("Record Question via Microphone"):
115
- user_question = record_audio()
 
 
116
 
117
  if user_question:
118
  user_input(user_question)
 
10
  from langchain.prompts import PromptTemplate
11
  from dotenv import load_dotenv
12
  import speech_recognition as sr
13
+ import sounddevice as sd
14
+ import scipy.io.wavfile as wav
15
 
16
  load_dotenv()
17
  os.getenv("GOOGLE_API_KEY")
 
81
  st.write("Reply: ", response["output_text"])
82
 
83
 
84
+ # Constants
85
+ DURATION = 5 # seconds
86
+ SAMPLERATE = 44100 # Hz
87
+
88
  def record_audio():
89
+ st.write("Recording for {} seconds...".format(DURATION))
90
+ audio = sd.rec(int(DURATION * SAMPLERATE), samplerate=SAMPLERATE, channels=2, dtype='float64')
91
+ sd.wait() # Wait until recording is finished
92
+ wav.write('temp_audio.wav', SAMPLERATE, audio) # Save as WAV file (optional)
93
+ st.write("Recording finished. Processing the audio...")
94
+ return 'temp_audio.wav' # Return path to the audio file
95
+
 
 
 
 
 
 
 
96
 
97
  def main():
98
  st.set_page_config("Chat PDF")
 
108
  get_vector_store(text_chunks)
109
  st.success("Done")
110
 
 
111
  user_question = st.text_input("Ask a Question from the PDF Files")
112
  if st.button("Record Question via Microphone"):
113
+ audio_path = record_audio()
114
+ # Implement audio processing to text or use a service like Google Speech-to-Text here
115
+ # user_question = transcribe_audio(audio_path) # You'd need to implement this function
116
 
117
  if user_question:
118
  user_input(user_question)