G

Untitled

public
Guest Mar 16, 2026 Never 449
Clone
This code is a Python program that is implementing a simple AI assistant named Raz. The program uses various libraries for speech recognition, text-to-speech conversion, and AI generation. Here is an analysis of the code: 1. The code imports necessary libraries such as `speech_recognition`, `gTTS` (Google Text-to-Speech), `os`, `pygame`, and `google.generativeai`. 2. It defines some configuration settings such as `AI_NAME`, `USER_NAME`, and `API_KEY`. 3. It sets up a generative AI model using the Gemini AI platform with the provided API key. 4. It defines functions for the AI assistant: - `speak(text)`: Converts text to speech using gTTS and plays the audio using pygame. - `listen()`: Listens to the user's voice input using the microphone and Google's speech recognition. - `get_ai_response(prompt)`: Connects to the Gemini AI model to generate responses based on the provided prompt and context. 5. The main program loop interacts with the user by: - Greeting the user and informing them that the AI assistant Raz is ready to help. - Continuously listening to the user input using the `listen()` function. - Handling specific user commands like "goodbye", "stop", or "exit" to end the program. - Generating responses from the AI model based on the user's query using the `get_ai_response()` function. - Speaking out the AI's response to the user. Overall, this code implements a basic voice-controlled AI assistant that can engage in conversations with the user by generating responses using a generative AI model.
1
import speech_recognition as sr
2
from gtts import gTTS
3
import os
4
import pygame
5
import google.generativeai as genai
6
7
# --- Configuration ---
8
AI_NAME = "Raz"
9
USER_NAME = "Sami"
10
11
# Replace 'YOUR_GEMINI_API_KEY_HERE' with your actual API Key
12
API_KEY = "YOUR_GEMINI_API_KEY_HERE"
13
14
# AI Setup
15
genai.configure(api_key=API_KEY)
16
model = genai.GenerativeModel('gemini-1.5-flash')
17
18
def speak(text):
19
"""Function to make Raz speak"""
20
print(f"{AI_NAME}: {text}")
21
tts = gTTS(text=text, lang='en') # Language set to English
22
tts.save("voice.mp3")
23
24
pygame.mixer.init()
25
pygame.mixer.music.load("voice.mp3")
26
pygame.mixer.music.play()
27
28
while pygame.mixer.music.get_busy():
29
continue
30
31
pygame.mixer.quit()
32
os.remove("voice.mp3")
33
34
def listen():
35
"""Function to listen to Sami's voice"""
36
r = sr.Recognizer()
37
with sr.Microphone() as source:
38
print(f"Listening for {USER_NAME}...")
39
r.pause_threshold = 1
40
r.adjust_for_ambient_noise(source)
41
audio = r.listen(source)
42
try:
43
query = r.recognize_google(audio, language='en-US')
44
print(f"{USER_NAME} said: {query}")
45
return query
46
except Exception:
47
return ""
48
49
def get_ai_response(prompt):
50
"""Connects to Gemini AI for powerful answers"""
51
try:
52
# Giving Raz his personality
53
context = f"Your name is {AI_NAME}. Your owner's name is {USER_NAME}. Be a helpful, smart, and witty assistant. Answer briefly: {prompt}"
54
response = model.generate_content(context)
55
return response.text
56
except Exception as e:
57
return "I'm sorry Sami, I'm having trouble connecting to my brain right now."
58
59
# --- Main Program Loop ---
60
speak(f"Hello {USER_NAME}! I am {AI_NAME}, your personal AI assistant. I am online and ready to help.")
61
62
while True:
63
user_query = listen().lower()
64
65
if user_query:
66
# Exit Command
67
if "goodbye" in user_query or "stop" in user_query or "exit" in user_query:
68
speak(f"Goodbye {USER_NAME}! Have a great day. I'll be here if you need me.")
69
break
70
71
# Process query with AI
72
ai_reply = get_ai_response(user_query)
73
speak(ai_reply)