Einstieg in Python und eine Fehlermeldung, mit der ich nicht wirklich was anfangen kann.
Verfasst: Montag 19. August 2024, 05:56
Nach dem ich unter Zuhilfenahme eines Buches über Python3 einige Beispiele und Standards durchgearbeitet hatte, habe ich mich auch dabei ein wenig an die Syntax gewöhnen müssen. Ich habe danach nach einem kleinen Projekt gesucht welches z.B. Chat GPT-4o mit Hilfe von akustischen Befehlen durchführen kann. Es gibt div. Beispiele im Internet dazu (div. bei GitHub), allerdings scheint bei mir auf dem System wohl das ein oder andere trotz Vollinstallation (Microsoft Visual Code v1.92.2 unter Windows + Erweiterung "Python3") zu fehlen... Microphone sowie Lautsprecher sind vorhanden und funktioneren auch. Ich kann aber nicht wirklich was mit der Fehlermeldung anfangen.
Der aktuell komplette Code (Hello_World.py):
Die komplette Ausgabe zur Fehlermeldungsanalyse:
Könnte mir da jemand behilflich sein, hat jemand von euch eine Idee, da ich nicht verstehe was er von mir noch haben will oder was er wo vermisst?
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'