使用阿里云ECS创建聊天机器人
初次发布于2018年1月
前期考虑使用了图灵机器人,有两种实现方式绑定公众号和加入群聊。图灵机器人可以直接绑定公众号,使用过程还是不错的,不用明确@,可以直接进行对话,上下文逻辑比微软小冰要好,有1000条/天的限制。第二种方案则是使用第三方插件,绑定微信号,用机器人运行微信号,缺点就是需要一直运行微信不能退出,逻辑不明显。好吧,逻辑都不是很明显。
接下来考虑了Sam Gu调用itchat的方式[1]。Sam 使用谷歌云平台集成ItChat微信聊天机器人并集成深度人工智能应用,首先安装Jupyter Notebook。
登陆服务器,可以通过控制台,或者putty, mobaxterm等工具,前者简洁后者提供文件操作界面。
mkdir anaconda #
创建安装目录
cd anaconda #
将进入安装目录
wget https://repo.continuum.io/archive/Anaconda3-5.0.1-Linux-x86_64.sh #
下载安装包
bash Anaconda3-5.0.1-Linux-x86_64.sh #
安装另外卸载用
rm -rf anaconda3
jupyter notebook --generate-config --allow-root
ipython
from notebook.auth import passwd
passwd() #
设置密码会生成
hashed password
,复制
vi ~/.jupyter/jupyter_notebook_config.py
复制粘贴以下几行,然后运行。具体操作可以参翼起小飞在社区的博文,即[2]。
c.NotebookApp.ip='*'
c.NotebookApp.password = u'
把上面的文本粘贴到这里
'
c.NotebookApp.open_browser = False
c.NotebookApp.port =8888
nohup jupyter notebook --allow-root
然后运行Jupyter Notebook,并在浏览器登录。Sam是在谷歌云台(第一年免费,赠送300美元,当然阿里云也有学生优惠)上创建的,基本原理是一致的,他的视频里讲的也是非常清楚,这里把这些内容搬运过来。登陆Jupyter Notebook后,创建一个文件夹存放相关文件,然后创建Python3文件,在其命令行里复制并运行以下命令,第二条运行两次。
!git clone https://github.com/telescopeuser/workshop_blog.git
%load workshop_blog/setup_cloud.py
运行lesson 1就可以开始了。当然这些命令也可以在服务器直接调用,Jupyter Notebook在这里更多的是交互界面的作用。接下来的图形识别要调用机器学习,Sam使用了Google Cloud Platform's Machine Learning APIs,直接调用就可以,复制粘贴API,然后再运行lesson 2。Lesson3 主要讲了语音的识别,翻译。Lesson4就高深一点,涉及到情感语义分析,接下来的lesson5涉及到从视频中提取信息。按照参考文献[1]的知识进行操作即可。但是,这样调用谷歌API,和之前挂用图灵机器人,最大的区别就是多了几行代码,Well,很多行代码,和我预想的还是有差距的。
然后在warmheartli的项目里面发现了些有趣的内容,不过我要准备去挤火车了……
更新于2018年9月17日星期一:收集聊天语料
莫名其妙的更新被删除掉,好在以前的都还留着,从新再发语料的收集不再考虑使用字幕,从微博爬取数据,作为日后的语料和机器学习用,爬虫的方法参考nghuyong,nghuyong的方法在验证登录的时候存在无法识别。因此滑块验证的方法参考LiuXingMing/WeiboSliderCode;Python3WebSpider/CrackWeiboSlide;bone_ace/article/details/71056741,当然他们的方法也并不是有效,尝试了很多方法后,最简单粗暴地是把验证图形的矩阵直接放在cookies.py里面。
#!/usr/bin/env python
# encoding: utf-8
import datetime
import json
import base64
from time import sleep
import os
import time
import random
import io
from PIL import Image
from math import sqrt
import pymongo
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.common.exceptions import TimeoutException
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.remote.command import Command
WeiBoAccounts = [
{'username': '登陆账号', 'password': ‘密码’},
]
cookies = []
client = pymongo.MongoClient("localhost", 27017)
db = client["Sina"]
userAccount = db["userAccount"]
PIXELS = []
def getExactly(im):
""" Precise cutting """
imin = -1
imax = -1
jmin = -1
jmax = -1
row = im.size[0]
col = im.size[1]
for i in range(row):
for j in range(col):
if im.load()[i, j] != 255:
imax = i
break
if imax == -1:
imin = i
for j in range(col):
for i in range(row):
if im.load()[i, j] != 255:
jmax = j
break
if jmax == -1:
jmin = j
return (imin + 1, jmin + 1, imax + 1, jmax + 1)
def getType(browser):
""" Identifying the graphic path """
ttype = ''
time.sleep(3.5)
im0 = Image.open(io.BytesIO(browser.get_screenshot_as_png()))
box = browser.find_element_by_id('patternCaptchaHolder')
im = im0.crop((int(box.location['x']) + 10, int(box.location['y']) + 100, int(box.location['x']) + box.size['width'] - 10, int(box.location['y']) + box.size['height'] - 10)).convert('L')
newBox = getExactly(im)
im = im.crop(newBox)
width = im.size[0]
height = im.size[1]
for png in ims.keys():
isGoingOn = True
for i in range(width):
for j in range(height):
if ((im.load()[i, j] >= 245 and ims[png][i][j] < 245) or (im.load()[i, j] < 245 and ims[png][i][j] >= 245)) and abs(ims[png][i][j] - im.load()[i, j]) > 10: # With 245 as the threshold, about 245 as the blank, less than 245 as the line; the difference between two pixels is about 10 to remove the error on the 245 boundary
isGoingOn = False
break
if isGoingOn is False:
ttype = ''
break
else:
ttype = png
else:
break
px0_x = box.location['x'] + 40 + newBox[0]
px1_y = box.location['y'] + 130 + newBox[1]
PIXELS.append((px0_x, px1_y))
PIXELS.append((px0_x + 100, px1_y))
PIXELS.append((px0_x, px1_y + 100))
PIXELS.append((px0_x + 100, px1_y + 100))
return ttype
def move(browser, coordinate, coordinate0):
""" Move from coordinate0 to coordinate """
time.sleep(0.05)
length = sqrt((coordinate[0] - coordinate0[0]) ** 2 + (coordinate[1] - coordinate0[1]) ** 2) # Two point line distance
if length < 4: # If the distance between two points is less than 4PX, go straight ahead.
ActionChains(browser).move_by_offset(coordinate[0] - coordinate0[0], coordinate[1] - coordinate0[1]).perform()
return
else: # Recursion, sliding to the end
step = random.randint(3, 5)
x = int(step * (coordinate[0] - coordinate0[0]) / length) # In proportion
y = int(step * (coordinate[1] - coordinate0[1]) / length)
ActionChains(browser).move_by_offset(x, y).perform()
move(browser, coordinate, (coordinate0[0] + x, coordinate0[1] + y))
def draw(browser, ttype):
""" Slide """
if len(ttype) == 4:
px0 = PIXELS[int(ttype[0]) - 1]
login = browser.find_element_by_id('loginAction')
ActionChains(browser).move_to_element(login).move_by_offset(px0[0] - login.location['x'] - int(login.size['width'] / 2), px0[1] - login.location['y'] - int(login.size['height'] / 2)).perform()
browser.execute(Command.MOUSE_DOWN, {})
px1 = PIXELS[int(ttype[1]) - 1]
move(browser, (px1[0], px1[1]), px0)
px2 = PIXELS[int(ttype[2]) - 1]
move(browser, (px2[0], px2[1]), px1)
px3 = PIXELS[int(ttype[3]) - 1]
move(browser, (px3[0], px3[1]), px2)
browser.execute(Command.MOUSE_UP, {})
else:
print('Sorry! Failed! Maybe you need to update the code.')
def get_cookie_from_weibo(username, password):
browser = webdriver.Chrome()
browser.set_window_size(1050, 840)
browser.get('https://weibo.cn/')
time.sleep(1)
assert "微博" in browser.title
login_link = browser.find_element_by_link_text('登录')
ActionChains(browser).move_to_element(login_link).click().perform()
login_name = WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.ID, "loginName")))
login_password = browser.find_element_by_id("loginPassword")
login_name.send_keys(username)
login_password.send_keys(password)
login_button = browser.find_element_by_id("loginAction")
login_button.click()
try:
img = WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.CLASS_NAME, 'patt-shadow')))
except TimeoutException:
print('No verification codes')
self.open()
# Here you stay for 10 seconds to see if the Chrome is successfully logged in.
ttype = getType(browser) # Identifying the graphic path
print('Result: %s!' % ttype)
draw(browser, ttype) # Slide
time.sleep(10)
cookie = browser.get_cookies()
browser.close()
return cookie
def init_cookies():
for cookie in userAccount.find():
cookies.append(cookie['cookie'])
if __name__ == "__main__":
try:
userAccount.drop()
except Exception as e:
pass
for account in WeiBoAccounts:
cookie = get_cookie_from_weibo(account["username"], account["password"])
userAccount.insert_one({"_id": account["username"], "cookie": cookie})
TOBECONTINUED
参考文献:
[1] Telescopeuser, workshop_blog, (n.d.). https://github.com/telescopeuser/workshop_blog.
[2] 翼起小飞, 如何在阿里ECS云端运行Jupyter Notebook进行机器/深度学习?-博客-云栖社区-阿里云, (n.d.). https://yq.aliyun.com/articles/98527 (accessed February 5, 2018).

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
阿里云容器服务kubernetes发布竞价实例支持
竞价实例优化运营成本 竞价实例(Spot Instance)也叫抢占式实例是一种按需实例,旨在降低部分场景下使用ECS的成本,创建竞价实例时,必须为指定的实例规格设置一个价格上限,当指定的实例规格的当前市场价格低于出价时,就能成功创建竞价实例,并按当前市场价格计费。默认能稳定持有实例一小时。之后,当市场价格高于出价或者资源供需关系变化时,实例会被自动释放。 合理的使用阿里云ECS竞价实例,最高可以降低50% – 90% 的运营成本(相比按量付费的实例),可以用相同的预算,将计算容量提升 2 – 10 倍。为了保证尽可能高概率的弹出竞价实例,可以设置多个AZ多个规格进行竞价,大大提升了竞价实例的创建成功率。 竞价实例助力Cluster-Autoscaler弹性升级 上周,阿里云容器服务发布了Cluster-Autoscaler的支持,初次
- 下一篇
容器服务kubernetes弹性伸缩高级用法
前言 近期,阿里云容器服务kubernetes发布了cluster-autoscaler的支持,开发者可以通过页面简单快捷的配置节点的弹性伸缩,支持普通实例、GPU实例以及竞价实例帮助开发者实现架构弹性和运营成本之间的博弈。阿里云容器服务kubernetes的cluster-autoscaler的能力还有很多会陆续通过控制台开放出来,对于cluster-autoscaler高级功能有强需求的开发者,也提供手动配置的方式进行实现,那么接下来我们一起来看一下cluster-autoscaler支持的高级功能。 高级功能解析 在解析高级特性之前,我们首先要了解的是弹性伸缩的主要原理,在阿里云容器服务kubernetes中,通过页面配置的伸缩规则会转换为ESS(弹性伸缩服务)中的伸缩组,主动下发组件cluster-autoscaler,并将组I
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- Windows10,CentOS7,CentOS8安装Nodejs环境
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- 2048小游戏-低调大师作品
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- 设置Eclipse缩进为4个空格,增强代码规范