barghavani commited on
Commit
c00740d
·
verified ·
1 Parent(s): 3571a98

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -6
app.py CHANGED
@@ -83,16 +83,66 @@ def user_input(user_question):
83
  print(response)
84
  st.write("Reply: ", response["output_text"])
85
 
86
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  def main():
88
  st.set_page_config("Chat PDF")
89
  st.header("Chat with PDF using Gemini💁")
90
 
91
- # Audio input
92
- audio_file = st.file_uploader("Upload an audio file or record your question", type=['wav', 'mp3', 'ogg'])
93
- if audio_file and st.button("Transcribe and Process"):
94
- with st.spinner("Transcribing audio..."):
95
- user_question = transcribe_audio(audio_file)
96
  st.write(f"Transcribed Question: {user_question}")
97
  user_input(user_question)
98
 
 
83
  print(response)
84
  st.write("Reply: ", response["output_text"])
85
 
86
+ import streamlit.components.v1 as components
87
+
88
+ def voice_recorder(key=None):
89
+ component_html = """
90
+ <script src="https://cdn.jsdelivr.net/npm/@ffmpeg/ffmpeg"></script>
91
+ <button id="recordButton">Record</button>
92
+ <button id="stopButton" disabled>Stop</button>
93
+ <audio id="audioPlayback" controls></audio>
94
+ <script>
95
+ let recordButton = document.getElementById("recordButton");
96
+ let stopButton = document.getElementById("stopButton");
97
+ let audioPlayback = document.getElementById("audioPlayback");
98
+
99
+ let recorder;
100
+ let audioData;
101
+
102
+ recordButton.onclick = () => {
103
+ navigator.mediaDevices.getUserMedia({ audio: true })
104
+ .then(stream => {
105
+ const mediaRecorder = new MediaRecorder(stream);
106
+ mediaRecorder.start();
107
+ const audioChunks = [];
108
+ mediaRecorder.addEventListener("dataavailable", event => {
109
+ audioChunks.push(event.data);
110
+ });
111
+ recorder = mediaRecorder;
112
+ stopButton.disabled = false;
113
+ recordButton.disabled = true;
114
+ });
115
+ };
116
+
117
+ stopButton.onclick = () => {
118
+ recorder.stop();
119
+ recorder.addEventListener("stop", () => {
120
+ const audioBlob = new Blob(audioChunks);
121
+ const audioUrl = URL.createObjectURL(audioBlob);
122
+ audioPlayback.src = audioUrl;
123
+ const reader = new FileReader();
124
+ reader.readAsDataURL(audioBlob);
125
+ reader.onload = () => {
126
+ window.parent.postMessage({ audioData: reader.result }, '*');
127
+ };
128
+ });
129
+ stopButton.disabled = true;
130
+ recordButton.disabled = false;
131
+ };
132
+ </script>
133
+ """
134
+ audio_base64 = components.html(component_html, height=150, key=key)
135
+ return audio_base64
136
+
137
  def main():
138
  st.set_page_config("Chat PDF")
139
  st.header("Chat with PDF using Gemini💁")
140
 
141
+ # Replace the existing audio file uploader with the custom voice recorder component
142
+ audio_data = voice_recorder("audio_recorder")
143
+ if audio_data and st.button("Process Recorded Audio"):
144
+ with st.spinner("Processing audio..."):
145
+ user_question = transcribe_audio(audio_data)
146
  st.write(f"Transcribed Question: {user_question}")
147
  user_input(user_question)
148