-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
147 lines (131 loc) · 4.84 KB
/
Copy pathmain.py
File metadata and controls
147 lines (131 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import speech_recognition as sr
import time
import webbrowser
import music_libary
import requests
import google.generativeai as genai
from gtts import gTTS
import os
import pygame
google_api_key = "AIzaSyCueyvoOAwg07LjwB-AvxwtkRjkIz7kKEI"
genai.configure(api_key=google_api_key)
def speak(text):
tts = gTTS(text=text, lang="en")
tts.save("temp.mp3")
pygame.mixer.init()
pygame.mixer.music.load("temp.mp3")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
pygame.mixer.music.unload()
os.remove("temp.mp3")
def ai_response(prompt):
try:
# ✅ Create model directly (no client)
model = genai.GenerativeModel("gemini-2.5-flash")
# ✅ Ask for short response
response = model.generate_content(f"{prompt}\nRespond in one short sentence.")
short_reply = response.text.strip()
print("🧠 Gemini:", short_reply)
speak(short_reply)
return short_reply
except Exception as e:
print("⚠️ Gemini Error:", e)
speak("Sorry, I couldn't generate a response.")
return None
def processcommand(voice):
voice = voice.lower().strip()
if "open google" in voice:
speak("Opening Google")
webbrowser.open("https://www.google.com/")
elif "open youtube" in voice:
speak("Opening YouTube")
webbrowser.open("https://www.youtube.com/")
elif "open brave" in voice:
speak("Opening Brave Browser")
webbrowser.open("brave://newtab")
elif "open" in voice:
parts = voice.split(" ", 1) # Split only once
if len(parts) < 2:
speak(
"Please say the website name after 'open'. For example, 'open github'."
)
return
if ".com" in parts[1] or ".org" in parts[1] or ".net" in parts[1]:
site = parts[1].strip().replace(" ", "")
speak(f"Opening {site}")
webbrowser.open(site)
return
else:
site = parts[1].strip().replace(" ", "")
url = f"https://www.{site}.com"
speak(f"Opening {site}")
webbrowser.open(url)
elif "what is your name" in voice or "who are you" in voice:
speak("I am Agentis, your personal assistant.")
elif "time" in voice:
current_time = time.strftime("%I:%M %p")
speak(f"The current time is {current_time}")
elif "date" in voice:
current_date = time.strftime("%B %d, %Y")
speak(f"Today's date is {current_date}")
elif "stop" in voice or "exit" in voice or "quit" in voice:
speak("Deactivating Agentis. Goodbye!")
exit()
elif voice.startswith("play"):
parts = voice.split(" ", 1) # Split only once
if len(parts) < 2:
speak(
"Please say the song name after 'play'. For example, 'play despacito'."
)
return
song = parts[1].strip()
link = music_libary.music.get(song)
if link:
speak(f"Playing {song}")
webbrowser.open(link)
else:
speak(f"Sorry, I couldn't find {song} in your music library.")
elif voice.startswith("search for") or voice.startswith("search"):
parts = voice.split(" ", 2) # Split only twice
if len(parts) < 3:
speak("Please say what you want to search for after 'search for'.")
return
query = parts[2].strip()
speak(f"Searching for {query}")
webbrowser.open(f"https://www.google.com/search?q={query}")
elif "thank you" in voice or "thanks" in voice:
speak("You're welcome!")
elif "news" in voice:
r = requests.get(
"https://newsapi.org/v2/top-headlines?country=us&apiKey=9cc4ccab1af74f8bb384d9c1235ee135"
)
news = r.json()
articles = news.get("articles", [])
if articles:
speak("Here are the top news headlines:")
for article in articles:
title = article.get("title", "No title")
speak(f" - {title}")
else:
speak("Sorry, I couldn't find any news articles.")
else:
try:
ai_response(voice)
except Exception as e:
print("I couldn't process that command. Please try again.", e)
if __name__ == "__main__":
speak("Welcome back, User")
r = sr.Recognizer()
while True:
try:
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source, duration=1)
print("\nListening...")
audio = r.listen(source) # waits until silence
print("Recognizing...")
command = r.recognize_google(audio)
processcommand(command.lower())
except Exception as e:
# speak("Could not understand audio. Please speak clearly.")
continue