speech / src /streamlit_app.py
vishalkatheriya's picture
Update src/streamlit_app.py
fabc1d7 verified
import streamlit as st
import os
from llama_index.llms.groq import Groq
import streamlit.components.v1 as components
# Ensure Streamlit config directory is set
os.environ["STREAMLIT_CONFIG_DIR"] = "/app/.streamlit"
class TextToSpeechChat:
def __init__(self, api_key="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"):
# Initialize the LLM
self.llm = Groq(model="llama3-70b-8192", api_key=api_key)
self.speech_enabled = st.session_state.get("speech_enabled", True)
def speak_text(self, text):
"""Convert text to speech using browser's SpeechSynthesis API"""
if not text.strip() or not self.speech_enabled:
return
try:
safe_text = text.replace('"', '\\"').replace('\n', ' ')
js_code = f"""
<script>
function speak(text) {{
const utterance = new SpeechSynthesisUtterance(text);
utterance.rate = 1.0;
utterance.volume = 0.9;
const voices = window.speechSynthesis.getVoices();
if (voices.length > 0) {{
utterance.voice = voices[0];
}}
window.speechSynthesis.speak(utterance);
}}
window.speechSynthesis.onvoiceschanged = function() {{
speak("{safe_text}");
}};
window.speechSynthesis.getVoices();
</script>
"""
components.html(js_code, height=0)
st.write(f"πŸ—£οΈ Speaking: {text}")
except Exception as e:
st.error(f"❌ Speech error: {e}")
def get_llm_response(self, prompt):
"""Get response from LLM and speak paragraphs in real-time if enabled"""
try:
st.write("πŸ€– Generating response...")
response = self.llm.stream_complete(prompt)
full_response = ""
buffer = ""
response_container = st.empty()
for r in response:
delta = r.delta
full_response += delta
buffer += delta
response_container.write(full_response, unsafe_allow_html=True)
if self.speech_enabled and "\n\n" in buffer:
paragraphs = buffer.split("\n\n")
for paragraph in paragraphs[:-1]:
if paragraph.strip():
self.speak_text(paragraph.strip())
buffer = paragraphs[-1]
if self.speech_enabled and buffer.strip():
self.speak_text(buffer.strip())
return full_response
except Exception as e:
error_msg = f"❌ Error getting LLM response: {e}"
st.error(error_msg)
if self.speech_enabled:
self.speak_text(error_msg)
return error_msg
def main(user_input):
chat = TextToSpeechChat()
response = chat.get_llm_response(user_input)
return response
# Streamlit UI
st.title("Simple Streamlit UI")
# Speech toggle
if "speech_enabled" not in st.session_state:
st.session_state.speech_enabled = True
st.checkbox("Enable Speech", value=st.session_state.speech_enabled, key="speech_enabled")
user_input = st.text_input("Enter something:")
if st.button("Submit"):
st.write("You entered:", user_input)
response = main(user_input)
st.write("Response:", response)