-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVoice Assistant.py
113 lines (95 loc) · 3.32 KB
/
Voice Assistant.py
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
import win32com.client
import speech_recognition as sr
import webbrowser
import datetime
import openai
import os
import requests
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
chatStr=""
def ai(prompt):
openai.api_key = os.getenv("OPENAI_API_KEY")
text = f"OpenAI response for Prompt: {''.join(prompt.split('intelligence')[1:]).strip()} \n"
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
temperature=0.7,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
text += response["choices"][0]["text"]
if not os.path.exists("OpenAI"):
os.mkdir("OpenAI")
with open(f"OpenAI/{''.join(prompt.split('intelligence')[1:]).strip()}.txt","w") as f:
f.write(text)
def chat(query):
global chatStr
openai.api_key = os.getenv("OPENAI_API_KEY")
chatStr += f"Adnan:{query} \n Assistant: "
response = openai.Completion.create(
model="text-davinci-003",
prompt=chatStr,
temperature=0.7,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
say(response['choices'][0]['text'])
chatStr += f"{response['choices'][0]['text']}\n"
return response["choices"][0]["text"]
def say(text):
speaker = win32com.client.Dispatch("SAPI.SpVoice")
speaker.Speak(text)
def listen():
r = sr.Recognizer()
with sr.Microphone() as source:
r.pause_threshold =1
audio = r.listen(source)
try:
query = r.recognize_google(audio,language="en-in")
return query
except Exception as e:
return f"Some Error Occured: {e}"
def get_news_headlines():
news_api_key = os.getenv("NEWS_API_KEY")
url = f"https://newsapi.org/v2/top-headlines?country=in&category=sports&apiKey={news_api_key}"
response = requests.get(url)
data = response.json()
articles = data["articles"]
headlines = []
for article in articles[0:10]:
title = article["title"]
headlines.append(title)
return headlines
if __name__ == '__main__':
print("Welcome Adnan")
say("Hello I am your Desktop Assistant")
while True:
print("Listening...")
query = listen()
sites = [["youtube","https://youtube.com"],["google" , "https://google.com"],["wikipedia","https://wikipedia.com"],["chat gpt","chat.openai.com"],["aqua treat","https://aquatreatsystems.netlify.app"]]
for site in sites:
if f"Open {site[0]}".lower() in query.lower():
say(f"Opening {site[0]}...")
webbrowser.open(site[1])
if "the time" in query:
strTime = datetime.datetime.now().strftime("%H:%M:%S")
say(f"Sir the time is {strTime}")
elif "Using Artificial Intelligence".lower() in query.lower():
ai(prompt=query)
elif "news" in query.lower() or "headlines" in query.lower():
headlines = get_news_headlines()
news_response = "\n".join(headlines)
say(news_response)
elif "stop talking".lower() in query.lower():
exit()
elif "reset chat".lower() in query.lower():
chatStr = ""
else:
print("Chatting...")
chat(query)