如何使用Python创建AI虚拟助手

发布时间:2025-09-21 23:57

利用科技辅助,如AI和虚拟现实 #生活技巧# #自我提升技巧# #创造力激发方法#

作者|Dipesh Pal
编译|Flin
来源|analyticsvidhya

介绍

虚拟助手(也称为AI助手或数字助手)是一款应用程序,可以理解自然语言的语音命令并为用户完成任务。

我们应该都知道什么是虚拟助手。打开手机并说“ Ok Google”或“ Hey Siri”。Google助手,Siri,Alexa都是虚拟助手的示例。

演示和编码YouTube视频:

https://youtu.be/LliTjuxDw_o

内容

我们要做什么代码说明完整的代码GitHub储存库你如何贡献参考文献

1.我们要做什么

我们的虚拟助手将能够执行以下操作

天气预报,启动游戏,启动Windows应用程序,打开网站,告诉你几乎你所要求的一切,告诉你日期和时间,问候,新闻等。

你可以与笔记本电脑的麦克风/控制台进行交互。助手生成的响应将显示在控制台上,或者通过扬声器直接说出来。

未来的可能:自拍,与人聊天更多,等等。

2. 代码说明

让我们一起来创建自己的虚拟助手。

所有代码都可以在我的GitHub上找到。我的频道上还提供了演示YouTube视频和代码YouTube视频。所需的链接和软件包如下所述。如果你愿意分享,我将不胜感激。2.1 所需的软件包和库

pip install JarvisAI

这是我创建的最新虚拟助手模块。它提供任何虚拟助手的基本功能。前提条件是Python版本 > 3.6。

用法和功能

安装库后,你可以导入模块

import JarvisAI obj = JarvisAI.JarvisAssistant() response = obj.mic_input() print(response)

功能通过方法名称清除。例如,你可以检查代码。

mic_inputtext2speechshutdownwebsite_openersend_mailtell_me_datetell_me_timelaunch_any_appweathernewstell_me

在这里阅读更多关于它的信息

https://pypi.org/project/Jarv...

你也可以在这里为这个存储库做贡献。

https://github.com/Dipeshpal/...2.2 编码

导包

import JarvisAI import re import pprint import random

根据文档创建 JarvisAI的对象

obj = JarvisAI.JarvisAssistant()

我们已经创建了这个“t2s(text)”函数。这会将任何文本转换为语音。我们将使用(调用)此函数的整个程序从文本产生语音。

def t2s(text): obj.text2speech(text)

我们希望不断听取用户的输入,因此此“ mic_input() ”将尝试从计算机的麦克风中连续获取音频。它将处理音频并在“ res”变量中返回文本。我们可以使用此“ res”变量根据用户输入执行某些操作。

while True: res = obj.mic_input()

天气预报:我们使用正则表达式来匹配用户输入中的查询。如果在用户输入“ res”中找到“天气”或“温度”,则我们要进行天气预报。无需从头开始编写东西,只需调用“ obj.weather(city = city)”即可。

你只需要从用户输入中获取城市并将其传递给天气功能即可。它会告诉你你所在城市的天气预报。

我们可以将此返回的“ weather_res”传递到“ t2s(weather_res)”,以从“ weather_res”字符串中产生语音。

while True: res = obj.mic_input() if re.search('weather|temperature', res): city = res.split(' ')[-1] weather_res = obj.weather(city=city) print(weather_res) t2s(weather_res)

新闻:与上述类似,匹配用户输入“ res”中的“新闻”一词。如果匹配,则调用“ obj.news”。

它将返回15条新闻作为字符串列表。因此,我们可以将新闻作为“ news_res [0]”来获取,并将其传递给“ t2s(news_res [0])”。

while True: res = obj.mic_input() if 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])

讲述几乎所有内容:它将从维基百科中获取前500个字符,并将它们作为字符串返回。你可以使用'obj.tell_me(topic)'。

你需要将“主题”传递给“ tell_me(topic = topic)”。主题是你想知道的关键字。

while True: res = obj.mic_input() if re.search('tell me about', res): topic = res.split(' ')[-1] wiki_res = obj.tell_me(topic) print(wiki_res) t2s(wiki_res)

日期和时间:它将告诉你系统的当前日期和时间。

while True: res = obj.mic_input() if re.search('date', res): date = obj.tell_me_date() print(date) print(t2s(date)) if re.search('time', res): time = obj.tell_me_time() print(time) t2s(time)

打开任何网站:此'obj.website_opener(domain)'将为你打开任何网站。你只需要从用户输入中获取domain,然后传递给'obj.website_opener(domain)'。它将在你的默认浏览器中打开网站。

while True: res = obj.mic_input() if re.search('open', res): domain = res.split(' ')[-1] open_result = obj.website_opener(domain) print(open_result)

启动任何应用程序游戏等

这有点棘手,在“ obj.launch_any_app(path_of_app = path)”中,你需要传递“ .exe”文件路径的函数。

因此,我们创建了“ dict_app”字典,其中以“应用名称”作为键,以“路径”作为值。我们可以使用此“ dict_app”进行查找。如果字典中存在用户输入的应用程序,那么我们将通过获取路径来打开它。

以下示例仅适用于Chrome和Epic Games。

while True: res = obj.mic_input() if re.search('launch', res): 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(' ', 1)[1] 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)

问候和聊天,你现在可以像这样创建问候和聊天。

我正在 https://pypi.org/project/Jarv... 上使用Tensorflow添加聊天功能。你可以为使其更好而做出贡献。

while True: res = obj.mic_input() if re.search('hello', res): print('Hi') t2s('Hi') if re.search('how are you', res): li = ['good', 'fine', 'great'] response = random.choice(li) print(f"I am {response}") t2s(f"I am {response}") if re.search('your name|who are you', res): print("My name is Jarvis, I am your personal assistant") t2s("My name is Jarvis, I am your personal assistant")

你能做什么?”:在这里,我们只是使用“ obj.t2s()”来发表讲话。如果你了解python,则可以轻松理解以下代码

while True: res = obj.mic_input() if re.search('what can you do', res): 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": "Example: 'news for today' ", } ans = """I can do lots of things, for example you can ask me time, date, weather in your city, I can open websites for you, launch application and more. See the list of commands-""" print(ans) pprint.pprint(li_commands) t2s(ans)

3.完整的代码

import JarvisAI import re import pprint import random obj = JarvisAI.JarvisAssistant() def t2s(text): obj.text2speech(text) while True: res = obj.mic_input() if re.search('weather|temperature', res): city = res.split(' ')[-1] weather_res = obj.weather(city=city) print(weather_res) t2s(weather_res) if 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', res): topic = res.split(' ')[-1] wiki_res = obj.tell_me(topic) print(wiki_res) t2s(wiki_res) if re.search('date', res): date = obj.tell_me_date() print(date) print(t2s(date)) if re.search('time', res): time = obj.tell_me_time() print(time) t2s(time) if re.search('open', res): domain = res.split(' ')[-1] open_result = obj.website_opener(domain) print(open_result) if re.search('launch', res): 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(' ', 1)[1] 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', res): print('Hi') t2s('Hi') if re.search('how are you', res): li = ['good', 'fine', 'great'] response = random.choice(li) print(f"I am {response}") t2s(f"I am {response}") if re.search('your name|who are you', res): print("My name is Jarvis, I am your personal assistant") t2s("My name is Jarvis, I am your personal assistant") if re.search('what can you do', res): 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": "Example: 'news for today' ", } ans = """I can do lots of things, for example you can ask me time, date, weather in your city, I can open websites for you, launch application and more. See the list of commands-""" print(ans) pprint.pprint(li_commands) t2s(ans)

4. Github仓库

你可以随意使用我的代码。如果你喜欢我的作品,请为其点亮star;如果你喜欢,请在YouTube上订阅。

只需克隆存储库

https://github.com/Dipeshpal/...

然后运行pip install -r requirements.txt

它将自动安装所有内容。

5. 如何贡献

只需打开此GitHub存储库,阅读该书,你将了解你如何做出贡献。

https://github.com/Dipeshpal/...

你的贡献将反映在这个项目上。

https://pypi.org/project/Jarv...

6. 参考

GitHub存储库和代码

https://github.com/Dipeshpal/...

贡献的GitHub Pypi存储库

https://github.com/Dipeshpal/...

JarvisAI库

https://pypi.org/project/Jarv...

YouTube频道

https://www.youtube.com/DIPES...

演示和代码(YouTube)

https://youtu.be/LliTjuxDw_o

原文链接:https://www.analyticsvidhya.c...

欢迎关注磐创AI博客站:
http://panchuang.net/

sklearn机器学习中文官方文档:
http://sklearn123.com/

欢迎关注磐创博客资源汇总站:
http://docs.panchuang.net/

网址:如何使用Python创建AI虚拟助手 https://www.yuejiaxmz.com/news/view/1322427

相关内容

使用Python+JarvisAI实现AI虚拟助手
如何利用Python构建智能虚拟助手
使用Python构建虚拟个人助手教程
使用Python开发基于Python的虚拟助手
Python虚拟助手与自然语言理解
如何使用 Python 构建您的个人 AI 助手
揭秘Python虚拟助手:与未来的对话
虚拟助手:使用Python开发虚拟助手程序,实现语音识别、智能对话等功能
使用 ChatGPT 构建虚拟个人助理
Python编程实现ChatGPT虚拟老婆智能理财助手

随便看看