Update app.py
Browse files
app.py
CHANGED
@@ -117,7 +117,7 @@ def handle_objection(text):
|
|
117 |
return "\n".join(responses) if responses else "No objection response found."
|
118 |
return "No objection response found."
|
119 |
|
120 |
-
def transcribe_audio(audio_bytes):
|
121 |
"""Transcribe audio using the transcribe_with_chunks function from sentiment_analysis.py."""
|
122 |
try:
|
123 |
# Save audio bytes to a temporary WAV file
|
@@ -125,7 +125,7 @@ def transcribe_audio(audio_bytes):
|
|
125 |
with wave.open(wav_buffer, 'wb') as wf:
|
126 |
wf.setnchannels(1) # Mono audio
|
127 |
wf.setsampwidth(2) # 2 bytes for int16
|
128 |
-
wf.setframerate(
|
129 |
wf.writeframes(audio_bytes)
|
130 |
|
131 |
# Use the transcribe_with_chunks function from sentiment_analysis.py
|
@@ -140,37 +140,38 @@ def real_time_analysis():
|
|
140 |
st.info("Listening... Say 'stop' to end the process.")
|
141 |
|
142 |
def audio_frame_callback(audio_frame):
|
143 |
-
|
144 |
-
|
|
|
145 |
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
|
165 |
-
|
166 |
-
|
167 |
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
|
173 |
-
|
174 |
|
175 |
# Start WebRTC audio stream
|
176 |
webrtc_ctx = webrtc_streamer(
|
@@ -271,4 +272,5 @@ def run_app():
|
|
271 |
st.error(f"Error loading dashboard: {e}")
|
272 |
|
273 |
if __name__ == "__main__":
|
274 |
-
run_app()
|
|
|
|
117 |
return "\n".join(responses) if responses else "No objection response found."
|
118 |
return "No objection response found."
|
119 |
|
120 |
+
def transcribe_audio(audio_bytes, sample_rate=16000):
|
121 |
"""Transcribe audio using the transcribe_with_chunks function from sentiment_analysis.py."""
|
122 |
try:
|
123 |
# Save audio bytes to a temporary WAV file
|
|
|
125 |
with wave.open(wav_buffer, 'wb') as wf:
|
126 |
wf.setnchannels(1) # Mono audio
|
127 |
wf.setsampwidth(2) # 2 bytes for int16
|
128 |
+
wf.setframerate(sample_rate) # Sample rate
|
129 |
wf.writeframes(audio_bytes)
|
130 |
|
131 |
# Use the transcribe_with_chunks function from sentiment_analysis.py
|
|
|
140 |
st.info("Listening... Say 'stop' to end the process.")
|
141 |
|
142 |
def audio_frame_callback(audio_frame):
|
143 |
+
# Convert audio frame to bytes
|
144 |
+
audio_data = audio_frame.to_ndarray()
|
145 |
+
audio_bytes = (audio_data * 32767).astype(np.int16).tobytes() # Convert to int16 format
|
146 |
|
147 |
+
# Transcribe the audio
|
148 |
+
text = transcribe_audio(audio_bytes)
|
149 |
+
if text:
|
150 |
+
st.write(f"*Recognized Text:* {text}")
|
151 |
|
152 |
+
# Analyze sentiment
|
153 |
+
sentiment, score = analyze_sentiment(text)
|
154 |
+
st.write(f"*Sentiment:* {sentiment} (Score: {score})")
|
155 |
|
156 |
+
# Handle objection
|
157 |
+
objection_response = handle_objection(text)
|
158 |
+
st.write(f"*Objection Response:* {objection_response}")
|
159 |
|
160 |
+
# Get product recommendation
|
161 |
+
recommendations = []
|
162 |
+
if is_valid_input(text) and is_relevant_sentiment(score):
|
163 |
+
query_embedding = model.encode([text])
|
164 |
+
distances, indices = product_recommender.index.search(query_embedding, 1)
|
165 |
|
166 |
+
if distances[0][0] < 1.5: # Similarity threshold
|
167 |
+
recommendations = product_recommender.get_recommendations(text)
|
168 |
|
169 |
+
if recommendations:
|
170 |
+
st.write("*Product Recommendations:*")
|
171 |
+
for rec in recommendations:
|
172 |
+
st.write(rec)
|
173 |
|
174 |
+
return audio_frame
|
175 |
|
176 |
# Start WebRTC audio stream
|
177 |
webrtc_ctx = webrtc_streamer(
|
|
|
272 |
st.error(f"Error loading dashboard: {e}")
|
273 |
|
274 |
if __name__ == "__main__":
|
275 |
+
run_app()
|
276 |
+
|