Spaces:
Sleeping
Sleeping
Vlad Bastina
commited on
Commit
·
60dbb29
1
Parent(s):
243586b
ui made
Browse files- __pycache__/sentiment_analysis.cpython-312.pyc +0 -0
- sentiment_analysis.py +1 -1
- streamlit_app.py +54 -63
__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 |
-
|
| 11 |
-
|
| 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 |
-
|
| 23 |
-
|
| 24 |
-
with mic as source:
|
| 25 |
-
audio = recognizer.listen(source,timeout=5)
|
| 26 |
-
f.write(audio.get_wav_data())
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 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()
|