使用阿里云ECS创建聊天机器人

本文涉及的产品
云服务器 ECS,每月免费额度280元 3个月
云服务器ECS,u1 2核4GB 1个月
简介:


初次发布于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/WeiboSliderCodePython3WebSpider/CrackWeiboSlidebone_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).

 

相关实践学习
一小时快速掌握 SQL 语法
本实验带您学习SQL的基础语法,快速入门SQL。
7天玩转云服务器
云服务器ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,可降低 IT 成本,提升运维效率。本课程手把手带你了解ECS、掌握基本操作、动手实操快照管理、镜像管理等。了解产品详情:&nbsp;https://www.aliyun.com/product/ecs
目录
相关文章
|
28天前
|
存储 弹性计算 数据可视化
要将ECS中的文件直接传输到阿里云网盘与相册(
【2月更文挑战第31天】要将ECS中的文件直接传输到阿里云网盘与相册(
415 4
|
1月前
|
弹性计算 监控 数据安全/隐私保护
阿里云ECS云监控界面
阿里云ECS云监控界面
792 2
|
1月前
|
Arthas 弹性计算 运维
阿里云ECS监控服务
阿里云ECS监控服务
401 2
|
1月前
|
机器学习/深度学习 弹性计算 运维
ECS阿里云监控服务
ECS阿里云监控服务
73 3
|
1月前
|
存储 弹性计算 安全
创建阿里云ECS实例
创建阿里云ECS实例
172 4
|
14天前
|
弹性计算
阿里云ECS使用体验
在申请高校学生免费体验阿里云ECS云服务器后的一些使用体验和感受。
|
1月前
|
弹性计算 NoSQL Redis
阿里云ECS使用docke搭建redis服务
阿里云ECS使用docke搭建redis服务
150 1
|
1月前
|
弹性计算 关系型数据库 MySQL
阿里云ECS使用docker搭建mysql服务
阿里云ECS使用docker搭建mysql服务
152 1
|
3天前
|
弹性计算 运维 安全
阿里云ecs使用体验
整了台服务器部署项目上线
|
7天前
|
弹性计算
阿里云ECS的使用心得
本文主要讲述了我是如何了解到ECS,使用ECS的一些经验,以及自己的感悟心得