-
Notifications
You must be signed in to change notification settings - Fork 0
/
CharlieAI.py
133 lines (110 loc) · 3.88 KB
/
CharlieAI.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import os.path
import speech_recognition as sr
import webbrowser
import openai
from config import apikey, sites, apps, name, say, speaker
import datetime
import wikipedia
# Function Area
# Chat with AI
chatStr = ""
def chat(quary):
global chatStr
openai.api_key = apikey
chatStr += f"{name} : {quary}\n Charlie : "
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
)
try:
say(response["choices"][0]["text"])
except:
pass
chatStr += f'{response["choices"][0]["text"]}\n'
return response["choices"][0]["text"]
# Open Website
def openWebsite(siteName):
url = f"https://www.{siteName}"
say(f'opening {siteName}')
webbrowser.open(url)
# Open YouTube Channel
def openYoutubeChannel(channelName):
say(f'opening {channelName} youtube channel')
url = f"https://www.youtube.com/@{channelName}"
webbrowser.open(url)
# Audio Capture
def takecommand():
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
try:
query = r.recognize_google(audio, language='en')
print(f'{name} : {query}')
return query.lower()
except:
return ""
# Main Area
def TaskExecution():
hlw = "Hi I'm Charlie. How can I help you?"
speaker.Speak(hlw)
while True:
print('Listening...')
quary = takecommand()
# Open Listed Website
for site in sites:
if f'open {site[0]}' in quary:
say(f'opening {site[0]}')
webbrowser.open(site[1])
# Open Listed app
for app in apps:
if f'open {app[0]}' in quary:
say(f'opening {app[0]}')
os.startfile(app[1])
# Time
if "the time" in quary:
time = datetime.datetime.now().strftime("%H:%M:%S")
say(time)
elif "today date" in quary:
date = datetime.date.today().strftime("%d/%m/%Y")
say(date)
#Open Any Website
elif 'open' in quary and 'website' in quary:
QuaryOne = quary.replace("open", "").strip()
QuaryTwo = QuaryOne.replace("website", "").strip()
siteName = QuaryTwo.replace(" ", "").strip()
openWebsite(siteName)
# Open Any YouTube Channel
elif "youtube channel" in quary:
newQuary = quary.replace("open", "").strip()
newQuaryTwo = newQuary.replace("youtube channel", "").strip()
channelName = newQuaryTwo.replace(" ", "").strip()
openYoutubeChannel(channelName)
# Wikipedia
elif "according to wikipedia" in quary:
say("Searching Wikipedia...")
quary = quary.replace("wikipedia","")
result = wikipedia.summary(quary,sentences=3)
say(f"according to wikipedia {result}")
elif "made you" in quary or "create you" in quary:
say("I am an AI assistant created by Mahbub Alam")
elif "your parents" in quary:
say("My parents name is Mahbub Alam")
elif "develop you" in quary or "your developer" in quary:
say("My Developer name is Mahbub Alam")
elif "mahbub alam" in quary or "mahbub" in quary or "mahbub alom" in quary:
say("Mahbub Alam is a Machine Learning Engineer. He created me. Find him by username m a s m a h b u b a l o m")
elif "reset chat" in quary:
chatStr = ""
elif "charlie quit" in quary or "bye" in quary:
say("ok, bye")
exit()
else:
print("Chatting...")
chat(quary)
TaskExecution()
# The End