Der aktuell komplette Code (Hello_World.py):
Code: Alles auswählen
#!/usr/bin/env python3
#
import os
import sys
import openai
import pyaudio
import pygame
import speech_recognition as sr
import pyttsx3
import datetime
#Initializing pyttsx3
listening = True
engine = pyttsx3.init()
#Set your openai api key and customizing the chatgpt role
openai.api_key = "sk-..."
messages = [{"role": "system", "content": "Your name is Jarvis and give answers in 2 lines"}]
#Customizing The output voice
voices = engine.getProperty('voices')
rate = engine.getProperty('rate')
volume = engine.getProperty('volume')
def speak(audio):
engine.say(audio)
engine.runAndWait()
def wishMe():
hour = int(datetime.datetime.now().hour)
if hour >= 0 and hour < 12:
speak("Guten Morgen!")
elif hour >= 12 and hour < 18:
speak("Guten Tag!")
else:
speak("Guten Abend!")
speak("Ich bin Jarvis... Wie kann ich Dir helfen?")
def get_response(user_input):
messages.append({"role": "user", "content": user_input})
response = openai.ChatCompletion.create(
model = "gpt-4o",
messages = messages
)
ChatGPT_reply = response["choices"][0]["message"]["content"]
messages.append({"role": "assistant", "content": ChatGPT_reply})
return ChatGPT_reply
while listening:
wishMe()
with sr.Microphone() as source:
recognizer = sr.Recognizer()
recognizer.adjust_for_ambient_noise(source)
recognizer.dynamic_energy_threshold = 3000
try:
print("Listening...")
audio = recognizer.listen(source, timeout=5.0)
response = recognizer.recognize_google(audio)
print(response)
if "jarvis" in response.lower():
response_from_openai = get_response(response)
engine.setProperty('rate', 120)
engine.setProperty('volume', volume)
engine.setProperty('voice', 'greek')
engine.say(response_from_openai)
engine.runAndWait()
else:
print("Didn't recognize 'jarvis'.")
except sr.UnknownValueError:
print("Didn't recognize anything.")
Code: Alles auswählen
PS D:\Python\Hello-World> & C:/Users/rvogt/AppData/Local/Microsoft/WindowsApps/python3.11.exe d:/Python/Hello-World/Hello_World.py
pygame 2.6.0 (SDL 2.28.4, Python 3.11.9)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "d:\Python\Hello-World\Hello_World.py", line 53, in <module>
recognizer.adjust_for_ambient_noise(source)
File "C:\Users\[user]\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\speech_recognition\__init__.py", line 383, in adjust_for_ambient_noise
assert source.stream is not None, "Audio source must be entered before adjusting, see documentation for ``AudioSource``; are you using ``source``
outside of a ``with`` statement?"
^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: Audio source must be entered before adjusting, see documentation for ``AudioSource``; are you using ``source`` outside of a ``with`` statement?
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "d:\Python\Hello-World\Hello_World.py", line 51, in <module>
with sr.Microphone() as source:
File "C:\Users\[user]\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\speech_recognition\__init__.py", line 189, in __exit__
self.stream.close()
^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'close'