您现在的位置是:首页 > 文章详情

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

日期:2018-09-17点击:599


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

 

原文链接:https://yq.aliyun.com/articles/641518
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章