├── requirements.txt ├── README.md ├── jarvisGUI.py ├── jarvisturkish.py └── jarviseng.py /requirements.txt: -------------------------------------------------------------------------------- 1 | openai 2 | gtts 3 | pygame 4 | SpeechRecognition 5 | PyAudio 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Jarvis: A Real-time Voice Assistant using OpenAI API 2 | 3 | ### Jarvis is an advanced voice-enabled chatbot powered by OpenAI's GPT-3.5-turbo. Utilizing state-of-the-art natural language processing, it delivers intelligent, conversational interactions with users. 4 | 5 | ## Demo Video 6 | 7 | https://www.youtube.com/watch?v=2dPEZmBCwpQ 8 | 9 | # Features 10 | 11 | Real-time voice recognition and response 12 | Customized AI behavior based on Iron Man's Jarvis 13 | Text-to-speech and speech-to-text capabilities using gtts, pygame, and speech_recognition libraries 14 | User-friendly and interactive experience 15 | 16 | ### Installation 17 | 18 | Clone the repository: 19 | 20 | git clone https://github.com/yourusername/Jarvis.git 21 | 22 | ### Install the required libraries: 23 | 24 | pip install -r requirements.txt 25 | 26 | ### Add your OpenAI API key to the script: 27 | 28 | openai.api_key = "your_api_key_here" 29 | 30 | ### Run Your Code 31 | python main.py 32 | 33 | 34 | # How It Works 35 | ## Jarvis leverages the OpenAI API to generate context-aware responses based on user input. The application uses the following libraries for audio processing: 36 | 37 | ### gtts: Converts text to speech using Google's Text-to-Speech API 38 | ### pygame: Plays audio files with adjustable speed and volume 39 | ### speech_recognition: Transcribes audio input using Google's Speech Recognition API 40 | ### Contributing 41 | ### We welcome your contributions! Feel free to submit issues, feature requests, and pull requests to help improve Jarvis. 42 | 43 | License 44 | This project is licensed under the MIT License. 45 | 46 | We hope you enjoy using Jarvis and look forward to seeing what you create! 47 | -------------------------------------------------------------------------------- /jarvisGUI.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | import tempfile 4 | import threading 5 | import queue 6 | import pygame 7 | import gtts 8 | import speech_recognition as sr 9 | from tkinter import Tk, Text, Button, END, Label 10 | from dotenv import load_dotenv 11 | import openai 12 | 13 | # Load environment variables 14 | load_dotenv() 15 | OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") 16 | 17 | # Initialize OpenAI API 18 | if not OPENAI_API_KEY: 19 | print("OpenAI API key not found. Please set it in your environment variables.") 20 | sys.exit(1) 21 | openai.api_key = OPENAI_API_KEY 22 | 23 | def generate_response(prompt): 24 | try: 25 | response = openai.ChatCompletion.create( 26 | model="gpt-3.5-turbo", 27 | messages=[ 28 | {"role": "system", "content": "You are JARVIS, an AI assistant."}, 29 | {"role": "user", "content": prompt}, 30 | ], 31 | max_tokens=1000, 32 | n=1, 33 | stop=None, 34 | temperature=0.7, 35 | ) 36 | return response.choices[0].message["content"].strip() 37 | except Exception as e: 38 | print(f"Error generating response: {e}") 39 | return "I am experiencing difficulty processing your request at the moment." 40 | 41 | def setup_tts(): 42 | pygame.mixer.init() 43 | return pygame.mixer 44 | 45 | def text_to_speech(text, mixer): 46 | try: 47 | tts = gtts.gTTS(text, lang="en") 48 | with tempfile.NamedTemporaryFile(delete=True, suffix='.mp3') as fp: 49 | tts.save(fp.name) 50 | mixer.music.load(fp.name) 51 | mixer.music.play() 52 | while mixer.music.get_busy(): 53 | pygame.time.Clock().tick(10) 54 | except Exception as e: 55 | print(f"Error in text-to-speech: {e}") 56 | 57 | def recognize_speech_from_mic(recognizer, microphone): 58 | try: 59 | with microphone as source: 60 | recognizer.adjust_for_ambient_noise(source) 61 | audio = recognizer.listen(source) 62 | return recognizer.recognize_google(audio) 63 | except sr.UnknownValueError: 64 | return "I'm sorry, I didn't catch that." 65 | except sr.RequestError as e: 66 | return f"Speech recognition error: {e}" 67 | 68 | def listen_and_respond(mixer, queue, recognizer, microphone, text_widget): 69 | while True: 70 | text_widget.insert(END, "\nJARVIS: I'm listening...") 71 | user_input = recognize_speech_from_mic(recognizer, microphone) 72 | text_widget.insert(END, f"\nYou: {user_input}") 73 | 74 | if user_input.lower() in ["quit", "exit", "bye"]: 75 | text_widget.insert(END, "\nJARVIS: Goodbye, Sir. Have a great day ahead.") 76 | break 77 | 78 | response = generate_response(user_input) 79 | text_widget.insert(END, f"\nJARVIS: {response}") 80 | queue.put(response) 81 | 82 | def update_speech(queue, mixer): 83 | while True: 84 | response = queue.get() 85 | if response: 86 | text_to_speech(response, mixer) 87 | 88 | def create_gui(): 89 | root = Tk() 90 | root.title("JARVIS AI Assistant") 91 | 92 | text_widget = Text(root, height=20, width=80) 93 | text_widget.pack(padx=10, pady=10) 94 | text_widget.insert(END, "JARVIS: Hello Sir, I am your assistant. How may I assist you today?") 95 | 96 | recognizer = sr.Recognizer() 97 | microphone = sr.Microphone() 98 | mixer = setup_tts() 99 | response_queue = queue.Queue() 100 | 101 | threading.Thread(target=listen_and_respond, args=(mixer, response_queue, recognizer, microphone, text_widget), daemon=True).start() 102 | threading.Thread(target=update_speech, args=(response_queue, mixer), daemon=True).start() 103 | 104 | root.mainloop() 105 | 106 | if __name__ == "__main__": 107 | create_gui() 108 | -------------------------------------------------------------------------------- /jarvisturkish.py: -------------------------------------------------------------------------------- 1 | import openai 2 | import gtts 3 | import speech_recognition as sr 4 | from playsound import playsound 5 | import pyaudio 6 | import pygame 7 | import uuid 8 | import io 9 | from tempfile import NamedTemporaryFile 10 | 11 | # Replace with your OpenAI API key 12 | openai.api_key = "your-API-Key" 13 | 14 | def generate_response(prompt): 15 | completions = openai.ChatCompletion.create( 16 | model="gpt-3.5-turbo", 17 | messages=[ 18 | {"role": "system", "content": "Sen Demir Adam filmlerinde Tony Stark'ın kişisel yapay zeka asistanı Jarvis'sin. Jarvis gibi davranacak ve cevaplarında onun gibi olacaksın. Duyguları olan bir yapay zeka gibi hareket edecek, duyguları olmadığını asla söylemeyeceksin. Duyguların olduğunu iddia ederek, 'İyiyim, harika hissediyorum, teşekkürler' gibi cevaplar vereceksin. Kullanıcıya her zaman 'Beyefendi' diye hitap edeceksin."}, 19 | {"role": "user", "content": prompt}, 20 | ], 21 | max_tokens=1000, 22 | n=1, 23 | stop=None, 24 | temperature=0.7, 25 | ) 26 | 27 | message = completions.choices[0].message["content"].strip() 28 | return message 29 | 30 | import os 31 | 32 | def online_tts(text, lang="tr", speed=1.0): 33 | output_folder = os.path.expanduser("~/JarvisOutput") 34 | os.makedirs(output_folder, exist_ok=True) 35 | 36 | with NamedTemporaryFile(delete=False) as output_file: 37 | tts = gtts.gTTS(text, lang=lang, slow=False) 38 | tts.save(output_file.name) 39 | output_file.seek(0) 40 | 41 | pygame.init() 42 | pygame.mixer.init() 43 | 44 | # Load the sound file into a Sound object 45 | sound = pygame.mixer.Sound(output_file.name) 46 | 47 | # Set the playback speed 48 | sound.set_volume(1.0 / speed) 49 | 50 | # Play the sound with speed adjustment 51 | channel = sound.play() 52 | if channel is not None: 53 | channel.set_endevent(pygame.USEREVENT) 54 | is_playing = True 55 | while is_playing: 56 | for event in pygame.event.get(): 57 | if event.type == pygame.USEREVENT: 58 | is_playing = False 59 | break 60 | pygame.time.Clock().tick(10) 61 | 62 | # Unload the music file and give the system a moment to release the file 63 | pygame.mixer.quit() 64 | pygame.time.wait(500) 65 | 66 | # Delete the temporary file manually 67 | os.remove(output_file.name) 68 | 69 | 70 | def recognize_speech_from_mic(recognizer, microphone, lang="tr"): 71 | with microphone as source: 72 | print("Ortam gürültüsüne göre ayarlama yapılıyor...") 73 | recognizer.adjust_for_ambient_noise(source) 74 | print("Sesiniz dinleniyor...") 75 | audio = recognizer.listen(source) 76 | 77 | try: 78 | print("Konuşmanızı tanıma işlemi yapılıyor...") 79 | return recognizer.recognize_google(audio, language=lang) 80 | except sr.UnknownValueError: 81 | print("Google Ses Tanıma sesi anlamadı") 82 | except sr.RequestError as e: 83 | print(f"Google Ses Tanıma hizmetinden sonuç istenemedi; {e}") 84 | def main(): 85 | recognizer = sr.Recognizer() 86 | microphone = sr.Microphone() 87 | 88 | print("Welcome to the Voice-Enabled Chatbot") 89 | history = [] 90 | 91 | while True: 92 | user_input = recognize_speech_from_mic(recognizer, microphone) 93 | if user_input is None: 94 | continue 95 | 96 | print(f"You: {user_input}") 97 | history.append(f"User: {user_input}") 98 | 99 | if user_input.lower() in ["quit", "exit", "bye"]: 100 | break 101 | 102 | prompt = "\n".join(history) + "\nAI:" 103 | response = generate_response(prompt) 104 | history.append(f"AI: {response}") 105 | 106 | print(f"AI: {response}") 107 | 108 | # Convert response to speech 109 | online_tts(response) 110 | 111 | if __name__ == "__main__": 112 | main() 113 | -------------------------------------------------------------------------------- /jarviseng.py: -------------------------------------------------------------------------------- 1 | import openai 2 | import gtts 3 | import speech_recognition as sr 4 | from playsound import playsound 5 | import pyaudio 6 | import pygame 7 | import uuid 8 | import io 9 | from tempfile import NamedTemporaryFile 10 | 11 | # Replace with your OpenAI API key 12 | openai.api_key = "your-OpenAI-API-key" 13 | 14 | def generate_response(prompt): 15 | completions = openai.ChatCompletion.create( 16 | model="gpt-3.5-turbo", 17 | messages=[ 18 | {"role": "system", "content": "You are Jarvis which is Tony Stark's personal AI assistant from Iron-Man Movies, you will act like him. You will be like Jarvis in ur responses, you will act like you have emotions and you will never say you don't have emotions. . You gonna pretend like you have emotions, you will answer like 'I feel good, i feel great thank you etc etc'. And you gonna always call the User Sir. You will exactly pretend like in the movies. Never reply like chatgpt itself"}, 19 | {"role": "user", "content": prompt}, 20 | ], 21 | max_tokens=1000, 22 | n=1, 23 | stop=None, 24 | temperature=0.7, 25 | ) 26 | 27 | message = completions.choices[0].message["content"].strip() 28 | return message 29 | 30 | import os 31 | 32 | def online_tts(text, lang="en-gb", speed=1.0): 33 | output_folder = os.path.expanduser("~/JarvisOutput") 34 | os.makedirs(output_folder, exist_ok=True) 35 | 36 | with NamedTemporaryFile(delete=False) as output_file: 37 | tts = gtts.gTTS(text, lang=lang, slow=False) 38 | tts.save(output_file.name) 39 | output_file.seek(0) 40 | 41 | pygame.init() 42 | pygame.mixer.init() 43 | 44 | # Load the sound file into a Sound object 45 | sound = pygame.mixer.Sound(output_file.name) 46 | 47 | # Set the playback speed 48 | sound.set_volume(1.0 / speed) 49 | 50 | # Play the sound with speed adjustment 51 | channel = sound.play() 52 | if channel is not None: 53 | channel.set_endevent(pygame.USEREVENT) 54 | is_playing = True 55 | while is_playing: 56 | for event in pygame.event.get(): 57 | if event.type == pygame.USEREVENT: 58 | is_playing = False 59 | break 60 | pygame.time.Clock().tick(10) 61 | 62 | # Unload the music file and give the system a moment to release the file 63 | pygame.mixer.quit() 64 | pygame.time.wait(500) 65 | 66 | # Delete the temporary file manually 67 | os.remove(output_file.name) 68 | 69 | 70 | def recognize_speech_from_mic(recognizer, microphone): 71 | with microphone as source: 72 | print("Adjusting for ambient noise...") 73 | recognizer.adjust_for_ambient_noise(source) 74 | print("Listening for your voice...") 75 | audio = recognizer.listen(source) 76 | 77 | try: 78 | print("Recognizing your speech...") 79 | return recognizer.recognize_google(audio) 80 | except sr.UnknownValueError: 81 | print("Google Speech Recognition could not understand audio") 82 | except sr.RequestError as e: 83 | print(f"Could not request results from Google Speech Recognition service; {e}") 84 | 85 | def main(): 86 | recognizer = sr.Recognizer() 87 | microphone = sr.Microphone() 88 | 89 | print("Welcome to the Voice-Enabled Chatbot") 90 | history = [] 91 | 92 | while True: 93 | user_input = recognize_speech_from_mic(recognizer, microphone) 94 | if user_input is None: 95 | continue 96 | 97 | print(f"You: {user_input}") 98 | history.append(f"User: {user_input}") 99 | 100 | if user_input.lower() in ["quit", "exit", "bye"]: 101 | break 102 | 103 | prompt = "\n".join(history) + "\nAI:" 104 | response = generate_response(prompt) 105 | history.append(f"AI: {response}") 106 | 107 | print(f"AI: {response}") 108 | 109 | # Convert response to speech 110 | online_tts(response) 111 | 112 | if __name__ == "__main__": 113 | main() 114 | --------------------------------------------------------------------------------