-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask5.py
More file actions
48 lines (43 loc) · 1.51 KB
/
Copy pathTask5.py
File metadata and controls
48 lines (43 loc) · 1.51 KB
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
import ollama
import speech_recognition as sr
import pyttsx3
# Initialize Text-to-Speech
engine = pyttsx3.init()
engine.setProperty("rate", 150) # Adjust speech speed
# Initialize Speech Recognition
recognizer = sr.Recognizer()
mic = sr.Microphone()
def speak(text):
"""Converts text to speech."""
engine.say(text)
engine.runAndWait()
def listen():
"""Captures speech input and converts it to text."""
with mic as source:
print("Listening... (Speak now)")
recognizer.adjust_for_ambient_noise(source) # Adjust for background noise
try:
audio = recognizer.listen(source, timeout=10, phrase_time_limit=10) # Increased timeout
text = recognizer.recognize_google(audio)
print(f"You: {text}")
return text
except sr.WaitTimeoutError:
print("No speech detected. Try again.")
return None
except sr.UnknownValueError:
print("Sorry, I couldn't understand that.")
return None
except sr.RequestError:
print("Speech Recognition service is down.")
return None
# Chatbot Loop
while True:
user_input = listen()
if user_input:
if user_input.lower() == "exit":
speak("Goodbye!")
print("Chatbot: Goodbye!")
break
response = ollama.chat(model="llama2", messages=[{"role": "user", "content": user_input}])
print("Chatbot:", response['message']['content'])
speak(response['message']['content'])