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import spck_groq as sg
|
| 9 |
|
| 10 |
# Title of the app
|
| 11 |
st.title("Simple Streamlit UI")
|
|
@@ -15,5 +157,5 @@ user_input = st.text_input("Enter something:")
|
|
| 15 |
|
| 16 |
# Button
|
| 17 |
if st.button("Submit"):
|
| 18 |
-
st.write("You entered:", user_input)
|
| 19 |
-
|
|
|
|
| 2 |
import sys
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
#-------------------------------------
|
| 6 |
+
import pyttsx3
|
| 7 |
+
import time
|
| 8 |
+
from llama_index.llms.groq import Groq
|
| 9 |
+
|
| 10 |
+
class TextToSpeechChat:
|
| 11 |
+
def __init__(self, api_key="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"):
|
| 12 |
+
# Initialize the LLM
|
| 13 |
+
self.llm = Groq(model="llama3-70b-8192", api_key=api_key)
|
| 14 |
+
|
| 15 |
+
# Initialize text-to-speech engine
|
| 16 |
+
self.tts_engine = pyttsx3.init()
|
| 17 |
+
self.setup_tts()
|
| 18 |
+
|
| 19 |
+
# Initialize speech enabled flag
|
| 20 |
+
self.speech_enabled = True
|
| 21 |
+
|
| 22 |
+
def setup_tts(self):
|
| 23 |
+
"""Configure text-to-speech settings"""
|
| 24 |
+
# Get available voices
|
| 25 |
+
voices = self.tts_engine.getProperty('voices')
|
| 26 |
+
|
| 27 |
+
# Set voice (use first available voice)
|
| 28 |
+
if voices:
|
| 29 |
+
self.tts_engine.setProperty('voice', voices[0].id)
|
| 30 |
+
else:
|
| 31 |
+
print("β οΈ No voices available for text-to-speech")
|
| 32 |
+
|
| 33 |
+
# Set speech rate (words per minute)
|
| 34 |
+
self.tts_engine.setProperty('rate', 150)
|
| 35 |
+
|
| 36 |
+
# Set volume (0.0 to 1.0)
|
| 37 |
+
self.tts_engine.setProperty('volume', 0.9)
|
| 38 |
+
|
| 39 |
+
print("π Text-to-speech engine initialized")
|
| 40 |
+
|
| 41 |
+
def speak_text(self, text):
|
| 42 |
+
"""Convert text to speech with error handling"""
|
| 43 |
+
if not text.strip():
|
| 44 |
+
return
|
| 45 |
+
|
| 46 |
+
try:
|
| 47 |
+
print(f"π£οΈ Speaking paragraph: {text}")
|
| 48 |
+
self.tts_engine.say(text)
|
| 49 |
+
self.tts_engine.runAndWait()
|
| 50 |
+
except Exception as e:
|
| 51 |
+
print(f"β Speech error: {e}")
|
| 52 |
+
|
| 53 |
+
def get_llm_response(self, prompt):
|
| 54 |
+
"""Get response from LLM and speak paragraphs in real-time if enabled"""
|
| 55 |
+
try:
|
| 56 |
+
print("π€ Generating response...")
|
| 57 |
+
response = self.llm.stream_complete(prompt)
|
| 58 |
+
|
| 59 |
+
# Collect the full response and buffer for paragraphs
|
| 60 |
+
full_response = ""
|
| 61 |
+
buffer = ""
|
| 62 |
+
|
| 63 |
+
for r in response:
|
| 64 |
+
delta = r.delta
|
| 65 |
+
full_response += delta
|
| 66 |
+
buffer += delta
|
| 67 |
+
print(delta, end="", flush=True)
|
| 68 |
+
|
| 69 |
+
# Check for paragraph boundary (\n\n)
|
| 70 |
+
if self.speech_enabled and "\n\n" in buffer:
|
| 71 |
+
# Split buffer into paragraphs
|
| 72 |
+
paragraphs = buffer.split("\n\n")
|
| 73 |
+
# Speak all complete paragraphs (all but the last part)
|
| 74 |
+
for paragraph in paragraphs[:-1]:
|
| 75 |
+
if paragraph.strip():
|
| 76 |
+
self.speak_text(paragraph.strip())
|
| 77 |
+
# Keep the last part (incomplete paragraph) in buffer
|
| 78 |
+
buffer = paragraphs[-1]
|
| 79 |
+
|
| 80 |
+
# Speak any remaining buffered text
|
| 81 |
+
if self.speech_enabled and buffer.strip():
|
| 82 |
+
self.speak_text(buffer.strip())
|
| 83 |
+
|
| 84 |
+
print() # New line after response
|
| 85 |
+
return full_response
|
| 86 |
+
|
| 87 |
+
except Exception as e:
|
| 88 |
+
error_msg = f"β Error getting LLM response: {e}"
|
| 89 |
+
print(error_msg)
|
| 90 |
+
if self.speech_enabled:
|
| 91 |
+
self.speak_text(error_msg)
|
| 92 |
+
return error_msg
|
| 93 |
+
|
| 94 |
+
def chat_with_speech(self):
|
| 95 |
+
"""Interactive chat with text-to-speech output"""
|
| 96 |
+
print("π― TEXT-TO-SPEECH CHAT")
|
| 97 |
+
print("=" * 50)
|
| 98 |
+
print("π‘ Type your message and press Enter")
|
| 99 |
+
print("π£οΈ The AI response will be spoken aloud paragraph by paragraph as it generates")
|
| 100 |
+
print("π¬ Type 'quit' or 'exit' to end the chat")
|
| 101 |
+
print("π Type 'mute' to disable speech")
|
| 102 |
+
print("π Type 'unmute' to enable speech")
|
| 103 |
+
print("=" * 50)
|
| 104 |
+
|
| 105 |
+
# Test speech engine at startup
|
| 106 |
+
self.speak_text("Text-to-speech chat started.")
|
| 107 |
+
|
| 108 |
+
while True:
|
| 109 |
+
try:
|
| 110 |
+
# Get user input
|
| 111 |
+
user_input = input("\nπ€ You: ").strip()
|
| 112 |
+
|
| 113 |
+
if not user_input:
|
| 114 |
+
continue
|
| 115 |
+
|
| 116 |
+
# Check for commands
|
| 117 |
+
if user_input.lower() in ['quit', 'exit', 'q']:
|
| 118 |
+
self.speak_text("Goodbye!")
|
| 119 |
+
print("π Goodbye!")
|
| 120 |
+
break
|
| 121 |
+
elif user_input.lower() == 'mute':
|
| 122 |
+
self.speech_enabled = False
|
| 123 |
+
print("π Speech disabled")
|
| 124 |
+
continue
|
| 125 |
+
elif user_input.lower() == 'unmute':
|
| 126 |
+
self.speech_enabled = True
|
| 127 |
+
print("π Speech enabled")
|
| 128 |
+
self.speak_text("Speech enabled.")
|
| 129 |
+
continue
|
| 130 |
+
|
| 131 |
+
# Get AI response, which now speaks paragraphs in real-time
|
| 132 |
+
print("\nπ€ AI: ", end="")
|
| 133 |
+
response = self.get_llm_response(user_input)
|
| 134 |
+
|
| 135 |
+
except KeyboardInterrupt:
|
| 136 |
+
self.speak_text("Goodbye!")
|
| 137 |
+
print("\nπ Goodbye!")
|
| 138 |
+
break
|
| 139 |
+
except Exception as e:
|
| 140 |
+
print(f"β Error: {e}")
|
| 141 |
+
if self.speech_enabled:
|
| 142 |
+
self.speak_text(f"Error: {str(e)}")
|
| 143 |
+
|
| 144 |
+
def main(user_input):
|
| 145 |
+
chat = TextToSpeechChat()
|
| 146 |
+
response = chat.get_llm_response(user_input)
|
| 147 |
+
return response
|
| 148 |
+
|
| 149 |
+
#-----------------------------------
|
| 150 |
|
|
|
|
| 151 |
|
| 152 |
# Title of the app
|
| 153 |
st.title("Simple Streamlit UI")
|
|
|
|
| 157 |
|
| 158 |
# Button
|
| 159 |
if st.button("Submit"):
|
| 160 |
+
st.write("You entered:", main(user_input))
|
| 161 |
+
|