Update app.py
Browse files
app.py
CHANGED
@@ -12,6 +12,8 @@ from PyPDF2 import PdfReader
|
|
12 |
from groq import Groq
|
13 |
from streamlit_webrtc import webrtc_streamer, AudioProcessorBase, WebRtcMode
|
14 |
import av
|
|
|
|
|
15 |
|
16 |
# Clear ChromaDB cache to fix tenant issue
|
17 |
chromadb.api.client.SharedSystemClient.clear_system_cache()
|
@@ -67,9 +69,23 @@ def transcribe_audio(file_path):
|
|
67 |
|
68 |
# Audio Processor Class for Recording
|
69 |
class AudioProcessor(AudioProcessorBase):
|
|
|
|
|
|
|
70 |
def recv(self, frame: av.AudioFrame) -> av.AudioFrame:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
return frame
|
72 |
|
|
|
|
|
|
|
73 |
# Streamlit UI
|
74 |
st.title("Chat with PDFs via Speech/Text π£οΈππ")
|
75 |
|
@@ -93,38 +109,36 @@ if uploaded_files:
|
|
93 |
# Record Audio
|
94 |
elif input_method == "Record Audio":
|
95 |
st.write("Record your audio query:")
|
|
|
96 |
webrtc_ctx = webrtc_streamer(
|
97 |
key="record",
|
98 |
mode=WebRtcMode.SENDONLY,
|
99 |
-
|
100 |
-
audio_processor_factory=AudioProcessor,
|
101 |
media_stream_constraints={"audio": True, "video": False},
|
102 |
)
|
103 |
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
response = chain({"question": transcription})["answer"]
|
127 |
-
st.write(f"**Response:** {response}")
|
128 |
|
129 |
# Upload Audio File Mode
|
130 |
elif input_method == "Upload Audio File":
|
|
|
12 |
from groq import Groq
|
13 |
from streamlit_webrtc import webrtc_streamer, AudioProcessorBase, WebRtcMode
|
14 |
import av
|
15 |
+
from pydub import AudioSegment
|
16 |
+
from io import BytesIO
|
17 |
|
18 |
# Clear ChromaDB cache to fix tenant issue
|
19 |
chromadb.api.client.SharedSystemClient.clear_system_cache()
|
|
|
69 |
|
70 |
# Audio Processor Class for Recording
|
71 |
class AudioProcessor(AudioProcessorBase):
|
72 |
+
def __init__(self):
|
73 |
+
self.audio_buffer = BytesIO()
|
74 |
+
|
75 |
def recv(self, frame: av.AudioFrame) -> av.AudioFrame:
|
76 |
+
# Append audio data to buffer
|
77 |
+
audio_segment = AudioSegment(
|
78 |
+
data=frame.to_ndarray().tobytes(),
|
79 |
+
sample_width=2,
|
80 |
+
frame_rate=frame.sample_rate,
|
81 |
+
channels=1
|
82 |
+
)
|
83 |
+
self.audio_buffer.write(audio_segment.raw_data)
|
84 |
return frame
|
85 |
|
86 |
+
def get_audio_data(self):
|
87 |
+
return self.audio_buffer
|
88 |
+
|
89 |
# Streamlit UI
|
90 |
st.title("Chat with PDFs via Speech/Text π£οΈππ")
|
91 |
|
|
|
109 |
# Record Audio
|
110 |
elif input_method == "Record Audio":
|
111 |
st.write("Record your audio query:")
|
112 |
+
audio_processor = AudioProcessor()
|
113 |
webrtc_ctx = webrtc_streamer(
|
114 |
key="record",
|
115 |
mode=WebRtcMode.SENDONLY,
|
116 |
+
audio_processor_factory=lambda: audio_processor,
|
|
|
117 |
media_stream_constraints={"audio": True, "video": False},
|
118 |
)
|
119 |
|
120 |
+
# Stop recording when session ends
|
121 |
+
if webrtc_ctx.state.playing:
|
122 |
+
st.write("Recording... Speak now.")
|
123 |
+
elif webrtc_ctx.state.stopped:
|
124 |
+
st.write("Recording stopped. Processing...")
|
125 |
+
audio_data = audio_processor.get_audio_data()
|
126 |
+
|
127 |
+
if audio_data:
|
128 |
+
# Save audio to a file
|
129 |
+
audio_file_path = "recorded_audio.wav"
|
130 |
+
audio_segment = AudioSegment.from_file(BytesIO(audio_data.getvalue()), format="raw", frame_rate=48000, channels=1, sample_width=2)
|
131 |
+
audio_segment.export(audio_file_path, format="wav")
|
132 |
+
st.success("Recording saved successfully!")
|
133 |
+
|
134 |
+
# Transcribe and Generate Response
|
135 |
+
st.write("Transcribing audio...")
|
136 |
+
transcription = transcribe_audio(audio_file_path)
|
137 |
+
st.write(f"**You said:** {transcription}")
|
138 |
+
|
139 |
+
with st.spinner("Generating response..."):
|
140 |
+
response = chain({"question": transcription})["answer"]
|
141 |
+
st.write(f"**Response:** {response}")
|
|
|
|
|
142 |
|
143 |
# Upload Audio File Mode
|
144 |
elif input_method == "Upload Audio File":
|