File size: 3,534 Bytes
8b5414d
66fdee1
42e99a5
20c5975
 
 
 
42e99a5
 
fabc1d7
42e99a5
 
20c5975
 
42e99a5
20c5975
 
42e99a5
 
20c5975
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42e99a5
20c5975
 
42e99a5
 
 
20c5975
42e99a5
 
 
 
20c5975
42e99a5
 
 
 
20c5975
42e99a5
 
 
 
 
 
 
 
 
 
 
 
 
 
20c5975
42e99a5
 
 
 
 
 
 
 
 
20c5975
 
66fdee1
20c5975
 
 
8b5414d
20c5975
8b5414d
bb72330
 
20c5975
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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)