Prajithr04 commited on
Commit
0af1295
·
1 Parent(s): e0a450a
Files changed (1) hide show
  1. assistant-streamlit.py +51 -0
assistant-streamlit.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import sounddevice as sd
3
+ import soundfile as sf
4
+ from faster_whisper import WhisperModel
5
+ import io
6
+ import os
7
+ from langchain_community.llms import Ollama
8
+ import pyttsx3
9
+ # Set environment variable to handle duplicate libraries
10
+ os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
11
+
12
+ # Initialize WhisperModel and Ollama
13
+ model_size = "base.en"
14
+ model = WhisperModel(model_size, device="cpu", compute_type="int8", num_workers=5)
15
+ llm = Ollama(model="tinyllama")
16
+
17
+ # Initialize text-to-speech engine
18
+ engine = pyttsx3.init('sapi5')
19
+ voices = engine.getProperty('voices')
20
+ engine.setProperty('voice',voices[0].id)
21
+ engine.setProperty('rate',180)
22
+
23
+ def speak(audio):
24
+ engine.say(audio)
25
+ engine.runAndWait()
26
+
27
+ # Record and transcribe audio
28
+ audio_data = st.audio("recorded_audio.wav", format="audio/wav", start_time=0)
29
+ if st.button("Record"):
30
+ with st.spinner("Recording..."):
31
+ recorded_audio = sd.rec(int(5 * 44100), samplerate=44100, channels=2, dtype="int16")
32
+ sd.wait()
33
+ sf.write("recorded_audio.wav", recorded_audio, samplerate=44100)
34
+
35
+ st.audio("recorded_audio.wav", format="audio/wav", start_time=0)
36
+
37
+ # Transcribe audio and speak response
38
+ with open("recorded_audio.wav", "rb") as audio_file:
39
+ segments,info= model.transcribe(io.BytesIO(audio_file.read()), beam_size=10)
40
+ for segment in segments:
41
+ prompt=segment.text
42
+ print(prompt)
43
+ st.text(prompt)
44
+ if prompt:
45
+ response = llm.invoke(prompt)
46
+ st.success("Response: " + response)
47
+ speak(response)
48
+ st.stop()
49
+ else:
50
+ st.error("Failed to transcribe audio.")
51
+