Gopikanth123 commited on
Commit
2aeecdb
·
verified ·
1 Parent(s): ae75c09

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -80
app.py CHANGED
@@ -1,16 +1,17 @@
1
  import streamlit as st
2
- import pyttsx3
 
3
  import threading
4
- import wave
5
- import io
6
- import speech_recognition as sr
7
  from gradio_client import Client
8
- import streamlit.components.v1 as components
9
 
10
- # Initialize session state
11
  if "messages" not in st.session_state:
12
  st.session_state["messages"] = [] # Store chat history
13
 
 
 
 
14
  # Function to generate a response using Gradio client
15
  def generate_response(query):
16
  try:
@@ -35,6 +36,7 @@ def handle_user_input(user_input):
35
 
36
  # Function to speak text (Voice Output)
37
  def speak_text(text):
 
38
  engine = pyttsx3.init()
39
  engine.stop() # Ensure no previous loop is running
40
  engine.say(text)
@@ -49,69 +51,15 @@ def update_chat_history():
49
  if "bot" in msg:
50
  st.markdown(f"<div class='chat-bubble bot-message'><strong>Bot:</strong> {msg['bot']}</div>", unsafe_allow_html=True)
51
 
52
- # Function to recognize speech from audio received as bytes
53
- def recognize_speech_from_audio(audio_bytes):
54
- st.info("Processing audio...")
55
-
56
- # Convert byte stream to audio file
57
- audio_data = io.BytesIO(audio_bytes)
58
- recognizer = sr.Recognizer()
59
 
60
- # Recognize speech from the audio data
61
- with sr.AudioFile(audio_data) as source:
62
- audio = recognizer.record(source)
63
-
64
- try:
65
- recognized_text = recognizer.recognize_google(audio)
66
- st.session_state["user_input"] = recognized_text
67
- st.success(f"Recognized Text: {recognized_text}")
68
- handle_user_input(recognized_text)
69
- except sr.UnknownValueError:
70
- st.error("Sorry, I couldn't understand the audio.")
71
- except sr.RequestError:
72
- st.error("Could not request results; please check your internet connection.")
73
-
74
- # JavaScript for audio recording and sending data to Streamlit
75
- audio_recorder_html = """
76
- <script>
77
- let audioChunks = [];
78
- let mediaRecorder;
79
-
80
- function startRecording() {
81
- navigator.mediaDevices.getUserMedia({ audio: true })
82
- .then(function(stream) {
83
- mediaRecorder = new MediaRecorder(stream);
84
- mediaRecorder.ondataavailable = function(event) {
85
- audioChunks.push(event.data);
86
- };
87
- mediaRecorder.onstop = function() {
88
- const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
89
- const reader = new FileReader();
90
- reader.onloadend = function() {
91
- const audioBase64 = reader.result.split(',')[1];
92
- window.parent.postMessage({ 'type': 'audio_data', 'audio': audioBase64 }, '*');
93
- };
94
- reader.readAsDataURL(audioBlob);
95
- };
96
- mediaRecorder.start();
97
- });
98
- }
99
-
100
- function stopRecording() {
101
- mediaRecorder.stop();
102
- }
103
-
104
- function handleStartStop() {
105
- if (mediaRecorder && mediaRecorder.state === "recording") {
106
- stopRecording();
107
- } else {
108
- startRecording();
109
- }
110
- }
111
- </script>
112
- <button onclick="handleStartStop()">Start/Stop Recording</button>
113
- <p>Click the button to start/stop audio recording.</p>
114
- """
115
 
116
  # Main Streamlit app
117
  st.set_page_config(page_title="Llama2 Chatbot", page_icon="🤖", layout="wide")
@@ -170,7 +118,7 @@ st.markdown(
170
  """
171
  Welcome to the *Llama2 Chatbot*!
172
  - *Type* your message below, or
173
- - *Speak* to the bot using your microphone.
174
  """
175
  )
176
 
@@ -186,17 +134,25 @@ with chat_history_container:
186
  if submit_button:
187
  handle_user_input(user_input)
188
 
189
- # Display JavaScript for audio recording
190
- components.html(audio_recorder_html, height=300)
 
 
 
 
 
191
 
192
- # Update chat history on every interaction
193
- update_chat_history()
194
 
195
- # Listening to the audio data sent by JavaScript
196
- def process_audio_data():
197
- audio_data = st.experimental_get_query_params().get('audio', [None])[0]
198
- if audio_data:
199
- recognize_speech_from_audio(audio_data)
200
 
201
- # Call the function to process audio if available
202
- process_audio_data()
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ from transformers import pipeline
3
+ import numpy as np
4
  import threading
 
 
 
5
  from gradio_client import Client
6
+ from streamlit_audio_recorder import st_audiorec
7
 
8
+ # Initialize session state for chat history
9
  if "messages" not in st.session_state:
10
  st.session_state["messages"] = [] # Store chat history
11
 
12
+ # Load the ASR model using the Hugging Face transformers pipeline
13
+ transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base.en")
14
+
15
  # Function to generate a response using Gradio client
16
  def generate_response(query):
17
  try:
 
36
 
37
  # Function to speak text (Voice Output)
38
  def speak_text(text):
39
+ import pyttsx3
40
  engine = pyttsx3.init()
41
  engine.stop() # Ensure no previous loop is running
42
  engine.say(text)
 
51
  if "bot" in msg:
52
  st.markdown(f"<div class='chat-bubble bot-message'><strong>Bot:</strong> {msg['bot']}</div>", unsafe_allow_html=True)
53
 
54
+ # Function to process and transcribe audio
55
+ def transcribe_audio(audio_data, sr):
56
+ # Normalize audio to float32
57
+ audio_data = audio_data.astype(np.float32)
58
+ audio_data /= np.max(np.abs(audio_data))
 
 
59
 
60
+ # Use the ASR model to transcribe the audio
61
+ transcription = transcriber({"sampling_rate": sr, "raw": audio_data})["text"]
62
+ return transcription
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
  # Main Streamlit app
65
  st.set_page_config(page_title="Llama2 Chatbot", page_icon="🤖", layout="wide")
 
118
  """
119
  Welcome to the *Llama2 Chatbot*!
120
  - *Type* your message below, or
121
+ - *Use the microphone* to speak to the bot.
122
  """
123
  )
124
 
 
134
  if submit_button:
135
  handle_user_input(user_input)
136
 
137
+ # Separate button for speech recognition outside of the form
138
+ if st.button("Speak"):
139
+ # Record and process the speech using Streamlit Audio Recorder
140
+ audio_data, sr = st_audiorec()
141
+
142
+ if audio_data is not None:
143
+ st.audio(audio_data, format="audio/wav")
144
 
145
+ # Convert to numpy array
146
+ audio_np = np.array(audio_data)
147
 
148
+ # Transcribe the audio
149
+ transcription = transcribe_audio(audio_np, sr)
 
 
 
150
 
151
+ # Display the recognized text
152
+ st.session_state["user_input"] = transcription
153
+ st.success(f"Recognized Text: {transcription}")
154
+ handle_user_input(transcription)
155
+
156
+ st.markdown("### Chat History")
157
+ # Update chat history on every interaction
158
+ update_chat_history()