Vlad Bastina commited on
Commit
60dbb29
·
1 Parent(s): 243586b
__pycache__/sentiment_analysis.cpython-312.pyc CHANGED
Binary files a/__pycache__/sentiment_analysis.cpython-312.pyc and b/__pycache__/sentiment_analysis.cpython-312.pyc differ
 
sentiment_analysis.py CHANGED
@@ -10,7 +10,7 @@ def get_analysis(file_path)->str:
10
 
11
  analysis = ask_gemini(transcript)
12
 
13
- return analysis
14
 
15
  if __name__ == "__main__":
16
  file_path = "harvard.wav"
 
10
 
11
  analysis = ask_gemini(transcript)
12
 
13
+ return transcript , analysis
14
 
15
  if __name__ == "__main__":
16
  file_path = "harvard.wav"
streamlit_app.py CHANGED
@@ -1,70 +1,61 @@
1
- from sentiment_analysis import get_analysis
2
-
3
  import streamlit as st
4
- import speech_recognition as sr
5
- import pyaudio
6
- import wave
7
  import time
 
 
 
 
8
 
 
 
 
 
 
 
 
 
9
 
10
- # Function to record the user's voice and save it as a .wav file
11
- def record_voice():
12
- recognizer = sr.Recognizer()
13
- mic = sr.Microphone()
14
-
15
- # Set up the microphone and record the audio
16
- with mic as source:
17
- st.write("Listening...")
18
- recognizer.adjust_for_ambient_noise(source)
19
- audio = recognizer.listen(source)
20
- st.write("Recording complete!")
21
 
22
- # Save the audio to a .wav file
23
- with open("recorded_audio.wav", "wb") as f:
24
- with mic as source:
25
- audio = recognizer.listen(source,timeout=5)
26
- f.write(audio.get_wav_data())
27
 
28
- # Return the file name of the recorded audio
29
- return "recorded_audio.wav", audio
30
-
31
- # Function to transcribe the audio file
32
- def transcribe_audio(audio):
33
- recognizer = sr.Recognizer()
34
-
35
- try:
36
- # Recognize the speech using Google's speech recognition service
37
- text = recognizer.recognize_google(audio)
38
- st.write(f"Recognized text: {text}")
39
- return text
40
- except sr.UnknownValueError:
41
- st.error("Sorry, I could not understand the audio.")
42
- except sr.RequestError as e:
43
- st.error(f"Error with the speech recognition service: {e}")
44
- return None
45
-
46
- # Streamlit app setup
47
- st.title("Voice Chat App")
48
-
49
- st.sidebar.header("Controls")
50
- start_button = st.sidebar.button("Start Recording")
51
-
52
- if start_button:
53
- st.write("Clicking this will start recording your voice...")
54
- time.sleep(2) # Pause for a moment before starting to record
55
- audio_file, audio_data = record_voice()
56
-
57
- # Saving and displaying the recorded audio
58
- st.write(f"Audio saved as: {audio_file}")
59
-
60
- # Transcribe the recorded audio
61
- if audio_data:
62
- transcription = transcribe_audio(audio_data)
63
- if transcription:
64
- st.text_area("Chat", value=transcription, height=200)
65
- else:
66
- st.write("Sorry, no transcription available.")
67
- else:
68
- st.write("No audio recorded.")
69
-
70
 
 
 
 
 
 
1
  import streamlit as st
 
 
 
2
  import time
3
+ from streamlit_mic_recorder import mic_recorder
4
+ import wave
5
+ import pyaudio
6
+ from sentiment_analysis import get_analysis
7
 
8
+ def save_audio(audio_data, filename):
9
+ """Save the recorded audio in .wav format"""
10
+ # Open a .wav file for writing
11
+ with wave.open(filename, 'wb') as wf:
12
+ wf.setnchannels(1) # Mono channel
13
+ wf.setsampwidth(pyaudio.PyAudio().get_sample_size(pyaudio.paInt16)) # 16-bit encoding
14
+ wf.setframerate(44100) # Sample rate (44100 Hz)
15
+ wf.writeframes(audio_data) # Write audio data to file
16
 
17
+ def main():
18
+ st.title("Streamlit Voice Recorder")
 
 
 
 
 
 
 
 
 
19
 
20
+ if "messages" not in st.session_state:
21
+ st.session_state.messages = []
 
 
 
22
 
23
+ # Prompt for voice recording
24
+ st.write("Click the button below to start recording your voice.")
25
+
26
+ # Mic Recorder input
27
+ audio = mic_recorder(
28
+ start_prompt="Start recording",
29
+ stop_prompt="Stop recording",
30
+ just_once=False,
31
+ use_container_width=False,
32
+ format="wav", # Specify format
33
+ callback=None,
34
+ args=(),
35
+ kwargs={},
36
+ key=None
37
+ )
38
+
39
+ if audio:
40
+ # Saving the audio to a .wav file
41
+ save_path = "recorded_audio.wav"
42
+ save_audio(audio["bytes"], save_path)
43
+
44
+ time.sleep(0.1)
45
+ with st.spinner('Fetching response from Gemini...'):
46
+ user_message, gemini_response = get_analysis("recorded_audio.wav")
47
+
48
+ st.session_state.messages.append({"role": "user", "content": user_message})
49
+ st.session_state.messages.append({"role": "gemini", "content": gemini_response})
50
+
51
+ # Display the chat history
52
+ for msg in st.session_state.messages:
53
+ if msg["role"] == "user":
54
+ st.chat_message(msg["role"]).markdown(f"**User:** {msg['content']}")
55
+ elif msg["role"] == "gemini":
56
+ st.chat_message(msg["role"]).markdown(f"**Gemini:** {msg['content']}")
57
+
58
+ time.sleep(0.1)
 
 
 
 
 
 
59
 
60
+ if __name__ == "__main__":
61
+ main()