rumaisa1054 commited on
Commit
dc86cbf
·
verified ·
1 Parent(s): e96bab3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -33
app.py CHANGED
@@ -3,6 +3,21 @@ from responser import responsr
3
  from gtts import gTTS
4
  from io import BytesIO
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  # Function to convert text to speech and return audio file
7
  def text_to_speech(text):
8
  tts = gTTS(text)
@@ -11,55 +26,87 @@ def text_to_speech(text):
11
  audio_file.seek(0)
12
  return audio_file
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def main():
15
  # Layout with three columns
16
  col1, col2, col3 = st.columns([3, 1, 1])
17
 
18
  with col1:
19
  # Title with custom CSS styling for top margin
20
- st.markdown('<div style="margin-top: -5px;margin-left: -200px;" class="title-wrapper"><h1 style="text-align: center;">ChatBot</h1></div>', unsafe_allow_html=True)
21
 
22
  # Initialize chat history if not already initialized
23
  if "chat_messages" not in st.session_state:
24
  st.session_state.chat_messages = []
25
 
26
- # Display chat history with audio only
27
  for message in st.session_state.chat_messages:
28
- if message["role"] == "assistant":
29
- st.audio(message["audio"], format="audio/mp3")
30
- else:
31
- st.audio(message["audio"], format="audio/mp3")
 
 
 
32
 
33
- # User input
34
- if prompt := st.chat_input("Welcome - How can I help you?"):
35
- # Convert user input to speech (optional: if you want to hear user input)
36
- user_audio = text_to_speech(prompt)
37
-
38
- # Add user message (as audio) to chat history
39
- st.session_state.chat_messages.append({
40
- "role": "user",
41
- "content": prompt,
42
- "audio": user_audio.getvalue()
43
- })
44
-
45
- # Get AI response using responsr function
46
- response = responsr(prompt)
47
 
48
- # Convert AI response to speech
49
- response_audio = text_to_speech(response)
50
-
51
- # Add assistant's response (as audio) to chat history
52
- st.session_state.chat_messages.append({
53
- "role": "assistant",
54
- "content": response,
55
- "audio": response_audio.getvalue()
56
- })
57
-
58
- # Display the audio files for both user input and AI response
59
- st.audio(user_audio, format="audio/mp3")
60
- st.audio(response_audio, format="audio/mp3")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  if __name__ == "__main__":
63
  main()
64
 
65
 
 
 
3
  from gtts import gTTS
4
  from io import BytesIO
5
 
6
+ import streamlit as st
7
+ from responser import responsr
8
+ from gtts import gTTS
9
+ from io import BytesIO
10
+ import whisper
11
+ import pyaudio
12
+ import numpy as np
13
+ import time
14
+
15
+ # Load Whisper model
16
+ whisper_model = whisper.load_model("base")
17
+
18
+ # Initialize PyAudio
19
+ p = pyaudio.PyAudio()
20
+
21
  # Function to convert text to speech and return audio file
22
  def text_to_speech(text):
23
  tts = gTTS(text)
 
26
  audio_file.seek(0)
27
  return audio_file
28
 
29
+ # Function to record audio from the microphone
30
+ def record_audio(duration=5, fs=16000):
31
+ stream = p.open(format=pyaudio.paInt16, channels=1, rate=fs, input=True, frames_per_buffer=1024)
32
+ frames = []
33
+ for _ in range(int(fs / 1024 * duration)):
34
+ data = stream.read(1024)
35
+ frames.append(data)
36
+ stream.stop_stream()
37
+ stream.close()
38
+ audio_data = b''.join(frames)
39
+ return np.frombuffer(audio_data, dtype=np.int16)
40
+
41
+ # Function to recognize speech using Whisper
42
+ def recognize_speech(audio_data):
43
+ # Assuming audio_data is a numpy array of int16
44
+ audio_data = audio_data.astype(np.float32) / 32768.0
45
+ result = whisper_model.transcribe(audio_data)
46
+ return result['text']
47
+
48
  def main():
49
  # Layout with three columns
50
  col1, col2, col3 = st.columns([3, 1, 1])
51
 
52
  with col1:
53
  # Title with custom CSS styling for top margin
54
+ st.markdown('<div style="margin-top: -5px;" class="title-wrapper"><h1 style="text-align: center;">ChatBot</h1></div>', unsafe_allow_html=True)
55
 
56
  # Initialize chat history if not already initialized
57
  if "chat_messages" not in st.session_state:
58
  st.session_state.chat_messages = []
59
 
60
+ # Display chat history with audio and text
61
  for message in st.session_state.chat_messages:
62
+ with col1:
63
+ if message["role"] == "assistant":
64
+ st.markdown(f"**Assistant:** {message['content']}")
65
+ st.audio(message["audio"], format="audio/mp3")
66
+ else:
67
+ st.markdown(f"**User:** {message['content']}")
68
+ st.audio(message["audio"], format="audio/mp3")
69
 
70
+ # Button to record audio input
71
+ if st.button('Record Audio'):
72
+ st.write("Recording...")
73
+ audio_data = record_audio(duration=5) # Adjust duration as needed
74
+ st.write("Processing...")
75
+ user_input = recognize_speech(audio_data)
 
 
 
 
 
 
 
 
76
 
77
+ if user_input:
78
+ # Convert user input to speech
79
+ user_audio = text_to_speech(user_input)
80
+
81
+ # Add user message (as audio) to chat history
82
+ st.session_state.chat_messages.append({
83
+ "role": "user",
84
+ "content": user_input,
85
+ "audio": user_audio.getvalue()
86
+ })
87
+
88
+ # Get AI response using responsr function
89
+ response = responsr(user_input)
90
+
91
+ # Convert AI response to speech
92
+ response_audio = text_to_speech(response)
93
+
94
+ # Add assistant's response (as audio) to chat history
95
+ st.session_state.chat_messages.append({
96
+ "role": "assistant",
97
+ "content": response,
98
+ "audio": response_audio.getvalue()
99
+ })
100
+
101
+ # Display the audio files for both user input and AI response
102
+ with col1:
103
+ st.markdown(f"**User:** {user_input}")
104
+ st.audio(user_audio, format="audio/mp3")
105
+ st.markdown(f"**Assistant:** {response}")
106
+ st.audio(response_audio, format="audio/mp3")
107
 
108
  if __name__ == "__main__":
109
  main()
110
 
111
 
112
+