Spaces:
Runtime error
Runtime error
| import os | |
| import time | |
| import openai | |
| import pyttsx3 | |
| import speech_recognition as sr | |
| import threading | |
| import itertools | |
| import sys | |
| # 🔑 Klucz API | |
| openai.api_key = "TU_WPISZ_SWÓJ_KLUCZ_API" | |
| # 🎤 Głos (TTS) | |
| engine = pyttsx3.init() | |
| engine.setProperty('rate', 165) # prędkość mowy | |
| engine.setProperty('volume', 1.0) | |
| # 🎧 Mikrofon + rozpoznawanie mowy | |
| recognizer = sr.Recognizer() | |
| microphone = sr.Microphone() | |
| # 🌀 Animacja "bot myśli..." | |
| def thinking_animation(stop_event): | |
| for c in itertools.cycle(["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]): | |
| if stop_event.is_set(): | |
| break | |
| sys.stdout.write(f"\r🤖 Bot myśli {c}") | |
| sys.stdout.flush() | |
| time.sleep(0.15) | |
| sys.stdout.write("\r" + " " * 20 + "\r") | |
| # 🧠 Generowanie odpowiedzi AI | |
| def get_response(prompt): | |
| stop_event = threading.Event() | |
| t = threading.Thread(target=thinking_animation, args=(stop_event,)) | |
| t.start() | |
| try: | |
| completion = openai.ChatCompletion.create( | |
| model="gpt-3.5-turbo", | |
| messages=[{"role": "user", "content": prompt}] | |
| ) | |
| stop_event.set() | |
| t.join() | |
| print("\r✅ Odpowiedź gotowa!\n") | |
| return completion.choices[0].message["content"].strip() | |
| except Exception as e: | |
| stop_event.set() | |
| t.join() | |
| print("\n❌ Błąd AI:", e) | |
| return "Coś poszło nie tak mordzia..." | |
| # 🔊 Mówienie | |
| def speak(text): | |
| print(f"🎙️ {text}") | |
| engine.say(text) | |
| engine.runAndWait() | |
| # 🎙️ Słuchanie | |
| def listen(): | |
| with microphone as source: | |
| recognizer.adjust_for_ambient_noise(source) | |
| print("\n🎧 Słucham cię mordzia...") | |
| audio = recognizer.listen(source) | |
| try: | |
| text = recognizer.recognize_google(audio, language="pl-PL") | |
| print(f"🗣️ Ty: {text}") | |
| return text | |
| except sr.UnknownValueError: | |
| print("❌ Nie zrozumiałem, powtórz mordzia.") | |
| return None | |
| except sr.RequestError as e: | |
| print(f"❌ Błąd rozpoznawania mowy: {e}") | |
| return None | |
| # 🔁 Pętla rozmowy | |
| def chat_loop(): | |
| print("🔥 Witam w trybie głosowym. Mów coś do mnie (powiedz 'stop' żeby zakończyć).") | |
| while True: | |
| user_input = listen() | |
| if user_input is None: | |
| continue | |
| if user_input.lower() in ["stop", "koniec", "pa", "nara"]: | |
| speak("Dobra mordzia, kończymy.") | |
| break | |
| response = get_response(user_input) | |
| speak(response) | |
| # 🚀 Start | |
| if __name__ == "__main__": | |
| chat_loop() |