如何使用Python创建AI虚拟助手
我们的虚拟助手将能够执行以下操作:
天气预报,启动游戏,启动Windows应用程序,打开网站,告诉您几乎您所要求的一切,告诉您日期和时间,问候,新闻等。
您可以与笔记本电脑的麦克风/控制台进行交互。助手生成的响应将显示在控制台上,或通过扬声器显示为语音。
未来更新:自拍,与人聊天更多,等等。您告诉我您想要什么。或贡献https://pypi.org/project/JarvisAI
2.代码说明-
因此,让我们创建自己的虚拟助手。
笔记-
所有代码都可以在我的GitHub上找到。
我的频道上还提供了演示YouTube视频和代码YouTube视频。
所需的链接和软件包如下所述。
分享将不胜感激。
让代码-
2.1。所需的软件包和库-
点安装JarvisAI
这是我创建的最新虚拟助手模块。它提供任何虚拟助手的基本功能。前提条件是只有Python(> 3.6)。
用法和功能-
安装库后,您可以导入模块-
进口JarvisAI
obj = JarvisAI.JarvisAssistant()
响应= obj.mic_input()
打印(响应)
该功能通过方法名称清除。例如,您可以检查代码。
mic_input
text2speech
关掉
website_opener
发邮件
tell_me_date
tell_me_time
launch_any_app
天气
新闻
告诉我
在这里阅读更多关于它的信息,您也可以在这里为这个回购做贡献。
2.2。码-
进口
导入JarvisAI
导入重新
导入pprint
导入随机
根据文档创建JarvisAI的对象–
obj = JarvisAI.JarvisAssistant()
我们已经创建了这个“ t2s(文本)”功能。这会将任何文本转换为语音。我们将使用(调用)此功能的整个程序从文本产生语音。
def t2s(文字):
obj.text2speech(文字)
我们希望连续收听用户的输入,因此此“ mic_input()”将尝试从计算机的麦克风中连续获取音频。它将处理音频并在“ res”变量中返回文本。我们可以使用此“ res”变量根据用户输入执行某些操作。
而True:
res = obj.mic_input()
天气预报:我们使用正则表达式来匹配用户输入中的查询。如果在用户输入“ res”中找到“天气”或“温度”,则我们要进行天气预报。无需从头开始编写东西,只需调用“ obj.weather(city = city)”即可。
您只需要从用户输入中获取城市并将其传递给天气功能即可。它会告诉您您所在城市的天气预报。
我们可以将此返回的“ weather_res”传递到“ t2s(weather_res)”,以从“ weather_res”字符串中产生语音。
而True:
res = obj.mic_input()
如果re.search('天气|温度',res):
城市= res.split('')[-1]
weather_res = obj.weather(city = city)
打印(weather_res)
t2s(weather_res)
新闻:与上述类似,匹配用户输入“ res”中的“新闻”一词。如果匹配,则调用“ obj.news”。
它将返回15条新闻作为字符串列表。因此,我们可以将新闻作为“ news_res [0]”获取,并将其传递给“ t2s(news_res [0])”。
而True:
res = obj.mic_input()
如果re.search('news',res):
news_res = obj.news()
pprint.pprint(news_res)
t2s(f“我发现了{len(news_res)}条新闻。您可以阅读。让我告诉您其中的前2条新闻”)
t2s(news_res [0])
t2s(news_res [1])
讲述几乎所有内容:它将从Wikipedia中获取前500个字符,并将它们作为字符串返回。您可以使用'obj.tell_me(topic)'。
您需要将“主题”传递给“ tell_me(topic = topic)”。主题是您想了解的关键字。
而True:
res = obj.mic_input()
如果re.search(“告诉我有关”,res):
主题= res.split('')[-1]
wiki_res = obj.tell_me(主题)
打印(wiki_res)
t2s(wiki_res)
日期和时间:它将告诉您系统的当前日期和时间。
而True:
res = obj.mic_input()
如果re.search('date',res):
日期= obj.tell_me_date()
打印(日期)
打印(t2s(日期))
如果re.search('time',res):
时间= obj.tell_me_time()
打印(时间)
t2s(时间)
打开任何网站:此“ obj.website_opener(域)”将为您打开任何网站。您只需要从用户输入中获取域,然后传递给'obj.website_opener(domain)'。它将在您的默认浏览器中打开网站。
而True:
res = obj.mic_input()
如果re.search('open',res):
域= res.split('')[-1]
open_result = obj.website_opener(域)
打印(open_result)
启动任何应用程序,游戏等:
这有点棘手,在“ obj.launch_any_app(path_of_app = path)”中,您需要传递“ .exe”文件路径的函数。
因此,我们创建了“ dict_app”字典,其中以“应用名称”作为键,以“路径”作为值。我们可以使用此“ dict_app”进行查找。如果字典中存在用户输入应用程序,那么我们将通过获取路径来打开它。
以下示例仅适用于Chrome和Epic Games。
而True:
res = obj.mic_input()
如果re.search('launch',res):
dict_app = {
'chrome':'C:\ Program Files(x86)\ Google \ Chrome \ Application \ chrome.exe',
'史诗游戏':'C:\ Program Files(x86)\ Epic Games \ Launcher \ Portal \ Binaries \ Win32 \ EpicGamesLauncher.exe'
}
app = res.split('',1)[1]
路径= dict_app.get(app)
如果path为None:
t2s('找不到应用程序路径')
打印(“找不到应用程序路径”)
其他:
t2s('启动中:'+应用)
obj.launch_any_app(path_of_app = path)
问候和聊天,您现在可以像这样创建问候和聊天。
我正在https://pypi.org/project/JarvisAI/上使用Tensorflow添加聊天功能。您可以为使其更好而做出贡献。
而True:
res = obj.mic_input()
如果re.search('hello',res):
打印('嗨')
t2s('嗨')
如果re.search('你好吗,res):
li = ['好','好','好']
响应= random.choice(li)
打印(f“我是{响应}”)
t2s(f“我是{response}”)
如果re.search(“您的名字|您是谁”,res):
打印(“我叫贾维斯,我是你的私人助理”)
t2s(“我叫Jarvis,我是您的私人助理”)
问-“您能做什么?”:在这里,我们只是使用“ obj.t2s()”来发表讲话。如果您了解python,则可以轻松理解以下代码-
而True:
res = obj.mic_input()
如果是re.search(“您能做什么”,res):
li_commands = {
“开放网站”:“示例:'open youtube.com”,
“ time”:“示例:“现在几点?”,
“ date”:“示例:“现在是几号?”,
“启动应用程序”:“示例:'启动chrome'”,
“ tell me”:“例如:“告诉我印度”',
“ weather”:“示例:'孟买的天气/温度是多少?”,
“ news”:“示例:“今天的新闻””,
}
ans =“”“我可以做很多事情,例如,您可以问我时间,日期,您所在城市的天气,
我可以为您打开网站,启动应用程序等等。请参阅命令列表-“”“
打印(ans)
pprint.pprint(li_commands)
t2s(ans)
3.完整的代码-
进口JarvisAI
汇入
导入pprint
随机导入
obj = JarvisAI.JarvisAssistant()
def t2s(文字):
obj.text2speech(文字)
而True:
res = obj.mic_input()
如果re.search('天气|温度',res):
城市= res.split('')[-1]
weather_res = obj.weather(city = city)
打印(weather_res)
t2s(weather_res)
如果re.search('news',res):
news_res = obj.news()
pprint.pprint(news_res)
t2s(f"I have found {len(news_res)} news. You can read it. Let me tell you first 2 of them")
t2s(news_res[0])
t2s(news_res[1])
if re.search('tell me about'
topic = res.split(' ')[-1]
wiki_res = obj.tell_me(topic)
print(wiki_res)
t2s(wiki_res)
if re.search('date'
date = obj.tell_me_date()
print(date)
print(t2s(date))
if re.search('time'
time = obj.tell_me_time()
print(time)
t2s(time)
if re.search('open'
domain = res.split(' ')[-1]
open_result = obj.website_opener(domain)
print(open_result)
if re.search('launch'
dict_app = {
'chrome': 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe'
'epic games': 'C:\Program Files (x86)\Epic Games\Launcher\Portal\Binaries\Win32\EpicGamesLauncher.exe'
}
app = res.split(' '
path = dict_app.get(app)
if path is None:
t2s('Application path not found')
print('Application path not found')
else:
t2s('Launching: ' + app)
obj.launch_any_app(path_of_app=path)
if re.search('hello'
print('Hi')
t2s('Hi')
if re.search('how are you'
li = ['good'
response = random.choice(li)
print(f"I am {response}")
t2s(f"I am {response}")
if re.search('your name|who are you'
print("My name is Jarvis
t2s("My name is Jarvis
if re.search('what can you do'
li_commands = {
"open websites": "Example: 'open youtube.com"
"time": "Example: 'what time it is?'"
"date": "Example: 'what date it is?'"
"launch applications": "Example: 'launch chrome'"
"tell me": "Example: 'tell me about India'"
"weather": "Example: 'what weather/temperature in Mumbai?'"
“ news”:“示例:“今天的新闻””,
}
ans =“”“我可以做很多事情,例如,您可以问我时间,日期,您所在城市的天气,
我可以为您打开网站,启动应用程序等等。请参阅命令列表-“”“
打印(ans)
pprint.pprint(li_commands)
t2s(ans)
4. Github仓库
您可以随意使用我的代码。如果您喜欢我的作品,请为其加注星号;如果您喜欢,请在YouTube上订阅。
只需克隆存储库-https://github.com/Dipeshpal/Jarvis-Assisant.git
然后运行pip install -r requirements.txt
它将自动安装所有内容。
题库