与小兔同学一起欢度春节

我正在参加「兔了个兔」创意投稿大赛,详情请看:「兔了个兔」创意投稿大赛

小兔智能机器人

先放效果:

image.png

Screenshot_20230202_124755.jpg

该智能聊天机器人的功能有如下几部分:

- 前端GUI欢迎界面
- 和“小兔兔”机器人进行互动
- 打开俄罗斯方块游戏
- 支持语音唤醒
- 接入微信小程序API

小兔智能音箱实现过程

语音助手——小兔 项目目录结构

├── README.md
├── aip                   //SDK目录     
│   ├── background.gif         
│   ├── weixinAPI.py         
│   └── game.py
│   └── Sounce.py
└── main.py        

第一部分——GUI实现

部分代码展示:

ini
复制代码
#欢迎界面GUI def GUI_welcome(self):     welcome = ![]()tkinter.Tk()     welcome.title("小兔兔智能音响")     welcome.geometry("540x400")     canvas = tk.Canvas(welcome, width=540, height=400, bd=0, highlightthickness=0)     imgpath = 'background.gif'     img = Image.open(imgpath)     photo = ImageTk.PhotoImage(img)     canvas.create_image(270, 200, image=photo)     canvas.pack()     button = tkinter.Button(welcome, text = '开始',command = lambda : welcome.destroy(),width = 5, height = 1)     text = tkinter.Label(welcome, bd=10, fg='white', bg='blue', text='HI 我是小兔兔')     text.place(relx=0.38, rely=0.3)  # 相对位置,放置文本     button.place(x290,y = 300, anchor='ne')     welcome.mainloop()

即出现以下效果
image.png

即利用python的tkinker库

生成一个简单的按钮界面,按下开始后,即可以语音唤醒“小兔同学”

第二部分——与小兔同学进行交流

即通过注册ChatGPT,获得一个OpenAI账号,并将其接入python的API调用接口

  • 使用GPT-3在 Python中,首先需要安装OpenAl的openai库。
  • pip install openai
  • 安装完成后,使用以下代码片段来访问GPT-3:
import
复制代码
#set your API keyopenai.api _key = "YOUR_API_K # Use the GPT-3 modelcompletion = openai.Completion. create( engine="text-davinci-002", prompt="Once upon a time,in a land far, far away,there was a princesswho. . .", max_tokens=1024,temperature=0.5,# Print the generated textprint(completion. choices[o].text)

(chat.openai.com/auth)
完成一个较为简单的聊天小助手项目。
步骤:
- 首先将对方的文字存入文本形式下,存为.txt文件. - 然后将txt文件进行识别处理
- 通过OpenAI的ChatGpt识别后进行反馈

这里注意,在接入微信小程序时:可以考虑用这种部署方式 打开gitee.com/shtml/wecha…

参考大佬的笔记——OpenAI ChatGPT 接入微信,与AI互动! - 掘金 (juejin.cn)

在OpenAI申请这里,之前还需要做以下准备:
申请属于自己的AI开发者账号,安装SDK 2、SDK的下载与安装  
应用创建完成后下载相应的开发SDK,这里采用python开发,下载python-SDK即可

第三部分——通过小兔同学助手打开俄罗斯方块这一游戏功能

image.png

ini
复制代码
import pygame import sys import time import random color_red = pygame.Color(25500) color_gray = pygame.Color(230230230) color_green = pygame.Color(02550) 。。。         if dire == 1:             x += 1         if dire == 2:             x -1         if dire == 3:             y -1         if dire == 4:             y += 1         if (x > 80) or (y > 60) or (x < 1) or (y < 1) or (arr[x][y] > 0):             time.sleep(5)             pygame.quit()             sys.exit()         arr[x][y] = snake_lon         for a, b in enumerate(arr, 1):             for c, d in enumerate(b, 1):                 if (d > 0):                     arr[a - 1][c - 1] = arr[a - 1][c - 1] - 1                     pygame.draw.rect(screen, color_green, ((a - 1) * 10, (c - 1) * 10, 10, 10))                 if (d < 0):                     pygame.draw.rect(screen, color_red, ((a - 1) * 10, (c - 1) * 10, 10, 10))         if (x == foodx) and (y == foody):             snake_lon += 1              while (arr[foodx][foody] != 0):                 foodx = random.randint(180)                 foody = random.randint(160)             arr[foodx][foody] = -1         pygame.display.flip() if __name__ == "__main__":     openGame()

部分主要代码如上:
根据俄罗斯方块游戏的特点:

  1. 方块下降
  2. 方块旋转
  3. 方块的左右移动
  4. 方块的填充

再结合用小兔助手打开后调用pygame的接口即可。

结语

兔年就来和小兔同学一起欢度春节吧,相信在不断训练该模型之后,小兔会越来越智能,并且附加更多的功能。

posted @ 2023-02-02 13:10  Luciferpluto  阅读(2)  评论(0)    收藏  举报  来源