vishalkatheriya commited on
Commit
0ff4842
Β·
verified Β·
1 Parent(s): 25243eb

Upload spck_groq.py

Browse files
Files changed (1) hide show
  1. spck_groq.py +293 -0
spck_groq.py ADDED
@@ -0,0 +1,293 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import pyttsx3
2
+ # import time
3
+ # from llama_index.llms.groq import Groq
4
+
5
+ # class TextToSpeechChat:
6
+ # def __init__(self, api_key="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"):
7
+ # # Initialize the LLM
8
+ # self.llm = Groq(model="llama3-70b-8192", api_key=api_key)
9
+
10
+ # # Initialize text-to-speech engine
11
+ # self.tts_engine = pyttsx3.init()
12
+ # self.setup_tts()
13
+
14
+ # # Initialize speech enabled flag
15
+ # self.speech_enabled = True
16
+
17
+ # def setup_tts(self):
18
+ # """Configure text-to-speech settings"""
19
+ # # Get available voices
20
+ # voices = self.tts_engine.getProperty('voices')
21
+
22
+ # # Set voice (use first available voice)
23
+ # if voices:
24
+ # self.tts_engine.setProperty('voice', voices[0].id)
25
+ # else:
26
+ # print("⚠️ No voices available for text-to-speech")
27
+
28
+ # # Set speech rate (words per minute)
29
+ # self.tts_engine.setProperty('rate', 150)
30
+
31
+ # # Set volume (0.0 to 1.0)
32
+ # self.tts_engine.setProperty('volume', 0.9)
33
+
34
+ # print("πŸ”Š Text-to-speech engine initialized")
35
+
36
+ # def speak_text(self, text):
37
+ # """Convert text to speech with error handling"""
38
+ # if not text.strip():
39
+ # return
40
+
41
+ # try:
42
+ # print(f"πŸ—£οΈ Speaking: {text}")
43
+ # self.tts_engine.say(text)
44
+ # self.tts_engine.runAndWait()
45
+ # except Exception as e:
46
+ # print(f"❌ Speech error: {e}")
47
+
48
+ # def get_llm_response(self, prompt):
49
+ # """Get response from LLM and speak streaming chunks if enabled"""
50
+ # try:
51
+ # print("πŸ€– Generating response...")
52
+ # response = self.llm.stream_complete(prompt)
53
+
54
+ # # Collect the full response and buffer chunks for speaking
55
+ # full_response = ""
56
+ # buffer = ""
57
+ # min_buffer_length = 20 # Minimum characters to speak (adjust for smoothness)
58
+
59
+ # for r in response:
60
+ # delta = r.delta
61
+ # full_response += delta
62
+ # buffer += delta
63
+ # print(delta, end="", flush=True)
64
+
65
+ # # Speak when buffer is large enough or ends with punctuation
66
+ # if self.speech_enabled and (len(buffer) >= min_buffer_length or buffer.endswith(('.', '!', '?', ','))):
67
+ # self.speak_text(buffer)
68
+ # buffer = "" # Reset buffer after speaking
69
+
70
+ # # Speak any remaining buffered text
71
+ # if self.speech_enabled and buffer.strip():
72
+ # self.speak_text(buffer)
73
+
74
+ # print() # New line after response
75
+ # return full_response
76
+
77
+ # except Exception as e:
78
+ # error_msg = f"❌ Error getting LLM response: {e}"
79
+ # print(error_msg)
80
+ # if self.speech_enabled:
81
+ # self.speak_text(error_msg)
82
+ # return error_msg
83
+
84
+ # def chat_with_speech(self):
85
+ # """Interactive chat with text-to-speech output"""
86
+ # print("🎯 TEXT-TO-SPEECH CHAT")
87
+ # print("=" * 50)
88
+ # print("πŸ’‘ Type your message and press Enter")
89
+ # print("πŸ—£οΈ The AI response will be spoken aloud as it generates")
90
+ # print("πŸ’¬ Type 'quit' or 'exit' to end the chat")
91
+ # print("πŸ”‡ Type 'mute' to disable speech")
92
+ # print("πŸ”Š Type 'unmute' to enable speech")
93
+ # print("=" * 50)
94
+
95
+ # # Test speech engine at startup
96
+ # self.speak_text("Text-to-speech chat started.")
97
+
98
+ # while True:
99
+ # try:
100
+ # # Get user input
101
+ # user_input = input("\nπŸ‘€ You: ").strip()
102
+
103
+ # if not user_input:
104
+ # continue
105
+
106
+ # # Check for commands
107
+ # if user_input.lower() in ['quit', 'exit', 'q']:
108
+ # self.speak_text("Goodbye!")
109
+ # print("πŸ‘‹ Goodbye!")
110
+ # break
111
+ # elif user_input.lower() == 'mute':
112
+ # self.speech_enabled = False
113
+ # print("πŸ”‡ Speech disabled")
114
+ # continue
115
+ # elif user_input.lower() == 'unmute':
116
+ # self.speech_enabled = True
117
+ # print("πŸ”Š Speech enabled")
118
+ # self.speak_text("Speech enabled.")
119
+ # continue
120
+
121
+ # # Get AI response, which now speaks streaming chunks
122
+ # print("\nπŸ€– AI: ", end="")
123
+ # response = self.get_llm_response(user_input)
124
+
125
+ # except KeyboardInterrupt:
126
+ # self.speak_text("Goodbye!")
127
+ # print("\nπŸ‘‹ Goodbye!")
128
+ # break
129
+ # except Exception as e:
130
+ # print(f"❌ Error: {e}")
131
+ # if self.speech_enabled:
132
+ # self.speak_text(f"Error: {str(e)}")
133
+
134
+ # def main():
135
+ # # Create the chat interface
136
+ # chat = TextToSpeechChat()
137
+
138
+ # # Start the interactive chat
139
+ # chat.chat_with_speech()
140
+
141
+ # if __name__ == "__main__":
142
+ # main()
143
+ import pyttsx3
144
+ import time
145
+ from llama_index.llms.groq import Groq
146
+
147
+ class TextToSpeechChat:
148
+ def __init__(self, api_key="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"):
149
+ # Initialize the LLM
150
+ self.llm = Groq(model="llama3-70b-8192", api_key=api_key)
151
+
152
+ # Initialize text-to-speech engine
153
+ self.tts_engine = pyttsx3.init()
154
+ self.setup_tts()
155
+
156
+ # Initialize speech enabled flag
157
+ self.speech_enabled = True
158
+
159
+ def setup_tts(self):
160
+ """Configure text-to-speech settings"""
161
+ # Get available voices
162
+ voices = self.tts_engine.getProperty('voices')
163
+
164
+ # Set voice (use first available voice)
165
+ if voices:
166
+ self.tts_engine.setProperty('voice', voices[0].id)
167
+ else:
168
+ print("⚠️ No voices available for text-to-speech")
169
+
170
+ # Set speech rate (words per minute)
171
+ self.tts_engine.setProperty('rate', 150)
172
+
173
+ # Set volume (0.0 to 1.0)
174
+ self.tts_engine.setProperty('volume', 0.9)
175
+
176
+ print("πŸ”Š Text-to-speech engine initialized")
177
+
178
+ def speak_text(self, text):
179
+ """Convert text to speech with error handling"""
180
+ if not text.strip():
181
+ return
182
+
183
+ try:
184
+ print(f"πŸ—£οΈ Speaking paragraph: {text}")
185
+ self.tts_engine.say(text)
186
+ self.tts_engine.runAndWait()
187
+ except Exception as e:
188
+ print(f"❌ Speech error: {e}")
189
+
190
+ def get_llm_response(self, prompt):
191
+ """Get response from LLM and speak paragraphs in real-time if enabled"""
192
+ try:
193
+ print("πŸ€– Generating response...")
194
+ response = self.llm.stream_complete(prompt)
195
+
196
+ # Collect the full response and buffer for paragraphs
197
+ full_response = ""
198
+ buffer = ""
199
+
200
+ for r in response:
201
+ delta = r.delta
202
+ full_response += delta
203
+ buffer += delta
204
+ print(delta, end="", flush=True)
205
+
206
+ # Check for paragraph boundary (\n\n)
207
+ if self.speech_enabled and "\n\n" in buffer:
208
+ # Split buffer into paragraphs
209
+ paragraphs = buffer.split("\n\n")
210
+ # Speak all complete paragraphs (all but the last part)
211
+ for paragraph in paragraphs[:-1]:
212
+ if paragraph.strip():
213
+ self.speak_text(paragraph.strip())
214
+ # Keep the last part (incomplete paragraph) in buffer
215
+ buffer = paragraphs[-1]
216
+
217
+ # Speak any remaining buffered text
218
+ if self.speech_enabled and buffer.strip():
219
+ self.speak_text(buffer.strip())
220
+
221
+ print() # New line after response
222
+ return full_response
223
+
224
+ except Exception as e:
225
+ error_msg = f"❌ Error getting LLM response: {e}"
226
+ print(error_msg)
227
+ if self.speech_enabled:
228
+ self.speak_text(error_msg)
229
+ return error_msg
230
+
231
+ def chat_with_speech(self):
232
+ """Interactive chat with text-to-speech output"""
233
+ print("🎯 TEXT-TO-SPEECH CHAT")
234
+ print("=" * 50)
235
+ print("πŸ’‘ Type your message and press Enter")
236
+ print("πŸ—£οΈ The AI response will be spoken aloud paragraph by paragraph as it generates")
237
+ print("πŸ’¬ Type 'quit' or 'exit' to end the chat")
238
+ print("πŸ”‡ Type 'mute' to disable speech")
239
+ print("πŸ”Š Type 'unmute' to enable speech")
240
+ print("=" * 50)
241
+
242
+ # Test speech engine at startup
243
+ self.speak_text("Text-to-speech chat started.")
244
+
245
+ while True:
246
+ try:
247
+ # Get user input
248
+ user_input = input("\nπŸ‘€ You: ").strip()
249
+
250
+ if not user_input:
251
+ continue
252
+
253
+ # Check for commands
254
+ if user_input.lower() in ['quit', 'exit', 'q']:
255
+ self.speak_text("Goodbye!")
256
+ print("πŸ‘‹ Goodbye!")
257
+ break
258
+ elif user_input.lower() == 'mute':
259
+ self.speech_enabled = False
260
+ print("πŸ”‡ Speech disabled")
261
+ continue
262
+ elif user_input.lower() == 'unmute':
263
+ self.speech_enabled = True
264
+ print("πŸ”Š Speech enabled")
265
+ self.speak_text("Speech enabled.")
266
+ continue
267
+
268
+ # Get AI response, which now speaks paragraphs in real-time
269
+ print("\nπŸ€– AI: ", end="")
270
+ response = self.get_llm_response(user_input)
271
+
272
+ except KeyboardInterrupt:
273
+ self.speak_text("Goodbye!")
274
+ print("\nπŸ‘‹ Goodbye!")
275
+ break
276
+ except Exception as e:
277
+ print(f"❌ Error: {e}")
278
+ if self.speech_enabled:
279
+ self.speak_text(f"Error: {str(e)}")
280
+
281
+ # def main():
282
+ # # Create the chat interface
283
+ # chat = TextToSpeechChat()
284
+
285
+ # # Start the interactive chat
286
+ # chat.chat_with_speech()
287
+ def main(user_input):
288
+ chat = TextToSpeechChat()
289
+ response = chat.get_llm_response(user_input)
290
+ return response
291
+
292
+ # if __name__ == "__main__":
293
+ # main()